/* Deterministic Generator Studio — design system + shell layout */

:root {
  --bg-0: #0d0e12;
  --bg-1: #15171e;
  --bg-2: #1b1e27;
  --bg-3: #232631;
  --border: #2a2d3a;
  --border-strong: #383c4c;
  --text-primary: #e9eaef;
  --text-secondary: #a6abbb;
  --text-tertiary: #6d7285;
  --accent: #7c9eff;
  --accent-strong: #99b4ff;
  --accent-contrast: #0b1020;
  --accent-2: #35d0ba;
  --danger: #ff6b6b;
  --warning: #f5b95a;
  --success: #4fd67a;
  --focus-ring: #7c9eff;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.35);
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace;
  --sidebar-w: 264px;
  --controls-w: 320px;
  --topbar-h: auto;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg-0: #f3f4f7;
  --bg-1: #ffffff;
  --bg-2: #f3f4f7;
  --bg-3: #e9ebf0;
  --border: #dcdfe6;
  --border-strong: #cdd0da;
  --text-primary: #14161d;
  --text-secondary: #565c6d;
  --text-tertiary: #888ea3;
  --accent: #3f63e8;
  --accent-strong: #2c4fd6;
  --accent-contrast: #ffffff;
  --accent-2: #0f9c88;
  --danger: #d8433c;
  --warning: #b06d0f;
  --success: #1f9d51;
  --focus-ring: #3f63e8;
  --shadow-1: 0 1px 2px rgba(20, 22, 29, 0.06);
  --shadow-2: 0 8px 24px rgba(20, 22, 29, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg-0: #f3f4f7;
    --bg-1: #ffffff;
    --bg-2: #ffffff;
    --bg-3: #f0f1f5;
    --border: #e1e3ea;
    --border-strong: #cdd0da;
    --text-primary: #14161d;
    --text-secondary: #565c6d;
    --text-tertiary: #888ea3;
    --accent: #3f63e8;
    --accent-strong: #2c4fd6;
    --accent-contrast: #ffffff;
    --accent-2: #0f9c88;
    --danger: #d8433c;
    --warning: #b06d0f;
    --success: #1f9d51;
    --focus-ring: #3f63e8;
    --shadow-1: 0 1px 2px rgba(20, 22, 29, 0.06);
    --shadow-2: 0 8px 24px rgba(20, 22, 29, 0.12);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg-0);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; }
p { margin: 0; }
button { font-family: inherit; }
a { color: var(--accent); }

::selection { background: var(--accent); color: var(--accent-contrast); }

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ---------- Shell layout ---------- */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background: var(--bg-1);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  position: sticky;
  top: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px;
  border-bottom: 1px solid var(--border);
}

.brand-mark {
  width: 28px; height: 28px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  flex-shrink: 0;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-title { font-weight: 700; font-size: 14px; letter-spacing: 0.2px; }
.brand-subtitle { font-size: 11px; color: var(--text-tertiary); }

.nav {
  flex: 1;
  overflow-y: auto;
  padding: 10px 8px 16px;
}

.nav-category {
  margin-top: 14px;
}
.nav-category:first-child { margin-top: 4px; }

.nav-category-title {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-tertiary);
  padding: 4px 10px 6px;
  font-weight: 600;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  margin-bottom: 2px;
}
.nav-item:hover { background: var(--bg-2); color: var(--text-primary); }
.nav-item.active {
  background: var(--bg-3);
  color: var(--text-primary);
  font-weight: 600;
  box-shadow: inset 2px 0 0 var(--accent);
}
.nav-item-icon {
  width: 18px; height: 18px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
  color: var(--text-tertiary);
}
.nav-item.active .nav-item-icon { color: var(--accent); }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  display: flex;
  gap: 8px;
}

/* ---------- Main / workspace ---------- */

.main {
  min-width: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg-1);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.topbar-title-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.generator-title { font-size: 18px; font-weight: 700; }
.generator-desc { font-size: 12.5px; color: var(--text-secondary); margin-top: 2px; max-width: 60ch; }

.topbar-actions { display: flex; gap: 8px; align-items: center; }
.suite-nav { display: flex; align-items: center; gap: 6px; }
.suite-link {
  display: inline-flex;
  min-height: 34px;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-2);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
  text-decoration: none;
}
.suite-link-primary { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); color: var(--text-primary); }
.suite-link:hover, .suite-link:focus-visible { border-color: var(--accent); background: var(--bg-3); color: var(--accent); }

.seed-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.seed-input-wrap {
  display: flex;
  align-items: center;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0 4px 0 10px;
  gap: 4px;
  flex: 1 1 220px;
  min-width: 160px;
}
.seed-input-wrap:focus-within { border-color: var(--accent); }
.seed-label { font-size: 11px; color: var(--text-tertiary); white-space: nowrap; }
.seed-input {
  border: none; background: transparent; color: var(--text-primary);
  font-family: var(--font-mono); font-size: 13px; padding: 8px 4px; flex: 1; min-width: 90px;
}
.seed-input:focus { outline: none; }

.icon-btn {
  width: 32px; height: 32px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 15px;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-3); color: var(--text-primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg-2);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  min-height: 36px;
}
.btn:hover { background: var(--bg-3); }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg-2); color: var(--text-primary); }
.btn-sm { padding: 5px 10px; min-height: 28px; font-size: 12px; }
.btn-danger { color: var(--danger); border-color: var(--border-strong); background: var(--bg-2); }
.btn-danger:hover { background: rgba(255,107,107,0.12); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.workspace {
  display: grid;
  grid-template-columns: var(--controls-w) 1fr;
  gap: 0;
  flex: 1;
  min-height: 0;
}

.controls-panel {
  border-right: 1px solid var(--border);
  background: var(--bg-1);
  padding: 16px;
  overflow-y: auto;
  min-height: 0;
}

.preview-panel {
  padding: 20px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  min-height: 0;
}

.preview-surface {
  flex: 1;
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  min-height: 320px;
  overflow: auto;
  position: relative;
}

.preview-surface canvas, .preview-surface svg { max-width: 100%; height: auto; }

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.export-menu { display: flex; gap: 8px; flex-wrap: wrap; }

.empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; color: var(--text-tertiary); text-align: center; padding: 40px 20px;
}
.empty-state-icon { font-size: 28px; opacity: 0.6; }

/* ---------- Form fields ---------- */

.control-group { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 12px; font-weight: 600; color: var(--text-secondary); }
.field-label-row { display: flex; justify-content: space-between; align-items: baseline; }
.field-value { font-family: var(--font-mono); font-size: 11.5px; color: var(--text-tertiary); }
.field-help { font-size: 11px; color: var(--text-tertiary); }
.field-checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }

.input {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 13px;
  font-family: var(--font-ui);
}
.input:focus { outline: none; border-color: var(--accent); }
.input-number { font-family: var(--font-mono); }
.input-select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--text-tertiary) 50%), linear-gradient(135deg, var(--text-tertiary) 50%, transparent 50%); background-position: calc(100% - 16px) center, calc(100% - 11px) center; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; padding-right: 28px; }
.input-textarea { font-family: var(--font-mono); resize: vertical; min-height: 70px; }
.input-checkbox { width: 16px; height: 16px; accent-color: var(--accent); }
.input-range { -webkit-appearance: none; appearance: none; height: 4px; background: var(--bg-3); border-radius: 999px; border: none; padding: 0; }
.input-range::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: var(--accent); cursor: pointer; border: 2px solid var(--bg-1); }
.input-range::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); cursor: pointer; border: 2px solid var(--bg-1); }
.input-color { width: 40px; height: 32px; padding: 2px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg-2); cursor: pointer; }
.input-color-sm { width: 32px; height: 28px; padding: 2px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg-2); cursor: pointer; }

.segmented { display: flex; flex-wrap: wrap; gap: 4px; background: var(--bg-2); padding: 3px; border-radius: var(--radius-md); border: 1px solid var(--border); }
.segmented-btn { flex: 1 1 auto; border: none; background: transparent; color: var(--text-secondary); padding: 6px 10px; border-radius: var(--radius-sm); font-size: 12px; cursor: pointer; min-width: 44px; }
.segmented-btn.active { background: var(--accent); color: var(--accent-contrast); font-weight: 600; }

.color-list { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.color-list-item { display: flex; align-items: center; gap: 2px; background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 2px; }
.chip-remove { border: none; background: transparent; color: var(--text-tertiary); cursor: pointer; width: 22px; height: 22px; border-radius: 4px; font-size: 14px; }
.chip-remove:hover { background: var(--bg-3); color: var(--danger); }

.control-advanced { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 10px; }
.control-advanced summary { cursor: pointer; font-size: 12px; font-weight: 600; color: var(--text-secondary); list-style: none; display: flex; align-items: center; gap: 6px; }
.control-advanced summary::-webkit-details-marker { display: none; }
.control-advanced summary::before { content: '▸'; font-size: 10px; color: var(--text-tertiary); transition: transform 0.15s; }
.control-advanced[open] summary::before { transform: rotate(90deg); }
.control-advanced .control-group { margin-top: 12px; }

/* ---------- Cards / misc ---------- */

.card { background: var(--bg-1); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 16px; }
.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 999px; font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px; }
.badge-common { background: rgba(166,171,187,0.16); color: var(--text-secondary); }
.badge-uncommon { background: rgba(79,214,122,0.16); color: var(--success); }
.badge-rare { background: rgba(124,158,255,0.16); color: var(--accent); }
.badge-epic { background: rgba(178,124,255,0.16); color: #b27cff; }
.badge-legendary { background: rgba(245,185,90,0.18); color: var(--warning); }

.notice { display: flex; gap: 8px; align-items: flex-start; padding: 10px 12px; border-radius: var(--radius-md); background: rgba(245,185,90,0.1); border: 1px solid rgba(245,185,90,0.3); color: var(--warning); font-size: 12px; }

.data-table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius-md); }
table.data-table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
table.data-table th, table.data-table td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
table.data-table th { background: var(--bg-2); color: var(--text-secondary); font-weight: 600; position: sticky; top: 0; }
table.data-table tr:last-child td { border-bottom: none; }

.mono { font-family: var(--font-mono); }

/* ---------- Toasts ---------- */

.toast-host { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 200; align-items: center; }
.toast { background: var(--bg-3); border: 1px solid var(--border-strong); color: var(--text-primary); padding: 9px 16px; border-radius: var(--radius-md); font-size: 12.5px; box-shadow: var(--shadow-2); opacity: 0; transform: translateY(6px); transition: opacity 0.18s ease, transform 0.18s ease; }
.toast-in { opacity: 1; transform: translateY(0); }
.toast-out { opacity: 0; }
.toast-success { border-color: rgba(79,214,122,0.5); }
.toast-error { border-color: rgba(255,107,107,0.5); }

/* ---------- Drawer (presets / history) ---------- */

.drawer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 90; opacity: 0; pointer-events: none; transition: opacity 0.18s ease; }
.drawer-overlay.open { opacity: 1; pointer-events: auto; }

.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 92vw); background: var(--bg-1); border-left: 1px solid var(--border); z-index: 91; transform: translateX(100%); transition: transform 0.2s ease; display: flex; flex-direction: column; }
.drawer.open { transform: translateX(0); }
.drawer-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.drawer-body { flex: 1; overflow-y: auto; padding: 10px 16px 20px; }
.drawer-empty { color: var(--text-tertiary); font-size: 12.5px; padding: 30px 0; text-align: center; }

.list-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 8px; border-radius: var(--radius-sm); border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }
.list-item-main { display: flex; flex-direction: column; gap: 2px; cursor: pointer; min-width: 0; flex: 1; }
.list-item-title { font-size: 12.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item-sub { font-size: 11px; color: var(--text-tertiary); font-family: var(--font-mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item-actions { display: flex; gap: 4px; flex-shrink: 0; }

/* ---------- Mobile controls toggle ---------- */

.mobile-controls-toggle { display: none; }
.mobile-topbar-toggle { display: none; }

/* ---------- Responsive ---------- */

@media (max-width: 1080px) {
  :root { --controls-w: 280px; }
}

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; height: auto; min-height: 100vh; overflow: visible; }
  .main { height: auto; overflow: visible; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 95;
    width: min(280px, 84vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-2);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-scrim { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 94; opacity: 0; pointer-events: none; transition: opacity 0.2s ease; }
  .sidebar-scrim.open { opacity: 1; pointer-events: auto; }
  .mobile-topbar-toggle { display: inline-flex; }

  .workspace { grid-template-columns: 1fr; }
  .controls-panel {
    position: static;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
    display: none;
  }
  .controls-panel.open { display: block; }
  .mobile-controls-toggle { display: inline-flex; }
}

@media (max-width: 640px) {
  .topbar { padding: 10px 12px; }
  .preview-panel { padding: 12px; }
  .generator-title { font-size: 16px; }
  .btn { padding: 8px 10px; }
  .seed-bar { width: 100%; }
  .seed-input-wrap { flex: 1 1 100%; }
  .topbar-title-row { align-items: flex-start; }
  .topbar-actions { width: 100%; flex-wrap: wrap; }
  .suite-nav { order: -1; width: 100%; }
  .suite-link { flex: 1 1 auto; justify-content: center; }
}

@media (max-width: 380px) {
  .topbar-actions .btn span.btn-label { display: none; }
}

@media print {
  @page { margin: 0.5in; }

  /* Force legible print colors regardless of active theme: everything that
     reads a --text-*/--bg-* variable (labels, table cells, card chrome)
     resolves to dark-on-white here. Content that carries its OWN inline
     color — palette hex swatches, rarity badges, faction emblems — is
     untouched, since those never reference these variables. */
  :root, :root[data-theme="dark"], :root[data-theme="light"] {
    --bg-0: #ffffff; --bg-1: #ffffff; --bg-2: #f5f6f8; --bg-3: #eef0f3;
    --border: #d8dbe2; --border-strong: #c3c7d0;
    --text-primary: #14161d; --text-secondary: #4a505f; --text-tertiary: #767c8c;
    --accent: #3f63e8; --accent-contrast: #ffffff;
  }

  .sidebar, .topbar, .controls-panel, .preview-toolbar, .toast-host,
  .drawer, .drawer-overlay, .sidebar-scrim { display: none !important; }

  /* Undo the viewport-bounded app shell (needed on-screen so panes scroll
     independently) so print output flows across as many pages as it needs
     instead of being clipped to one screen's worth of content. */
  .app-shell, .main, .workspace, .preview-panel, .controls-panel {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
  }
  .app-shell { display: block; }
  .workspace { display: block; }

  .preview-surface { border: none; padding: 0; min-height: 0; overflow: visible; }
  .preview-surface canvas, .preview-surface svg, .preview-surface img { max-width: 100%; }

  /* Any generator using a fixed-height scroll region (log/JSON previews)
     should print its full content, not just the scrolled viewport. */
  pre, .data-table-wrap { max-height: none !important; overflow: visible !important; }

  body { background: #fff; color: #14161d; }
  .card, table.data-table, .list-item { break-inside: avoid; }
}
