:root {
  color-scheme: dark;
  --bg: #101214;
  --surface: #171a1d;
  --surface-raised: #1d2125;
  --surface-hover: #24292e;
  --line: #30363c;
  --line-strong: #46505a;
  --text: #f2f4f5;
  --text-secondary: #a8b0b7;
  --text-tertiary: #89939c;
  --brand: #e3ae56;
  --brand-hover: #f0bd69;
  --inner: #4fd1b2;
  --people: #86a8ff;
  --scene: #ff8b77;
  --danger: #ff7d88;
  --radius: 8px;
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body {
  margin: 0;
  min-width: 320px;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; }
button, select { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }
button.is-busy { cursor: wait; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.hidden { display: none !important; }
.container { width: min(100% - 40px, 1240px); margin-inline: auto; }

/* Header */
.header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(16, 18, 20, .94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}
.header-top {
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.brand { display: inline-flex; align-items: center; gap: 11px; text-decoration: none; }
.brand-logo { width: 40px; height: 40px; flex: 0 0 40px; }
.brand-logo img { width: 40px; height: 40px; display: block; border-radius: var(--radius); }
.brand-text { display: flex; flex-direction: column; gap: 1px; }
.brand-text strong { font-size: 16px; line-height: 1.35; }
.brand-text span { color: var(--text-tertiary); font-size: 12px; line-height: 1.35; }
.back-link {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 13px;
}
.back-link:hover { color: var(--text); border-color: var(--line-strong); background: var(--surface); }

/* Mode navigation */
.page-shell { padding-block: 20px 48px; }
.mode-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 16px;
}
.tabs {
  display: inline-grid;
  grid-template-columns: repeat(3, auto);
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
.tab {
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 650;
}
.tab:hover { color: var(--text); background: var(--surface-hover); }
.tab.active { color: #17130d; background: var(--brand); }
.workflow {
  display: flex;
  align-items: center;
  gap: 18px;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--text-tertiary);
  font-size: 12px;
}
.workflow li { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.workflow span {
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--text-secondary);
  font-size: 10px;
  font-weight: 700;
}
.tab-panel, .tab-panel[hidden] { display: none; }

/* Shared surfaces and fields */
.card {
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}
.card-head h2, .subhead h3 { margin: 0; letter-spacing: 0; }
.card-head h2 { font-size: 17px; line-height: 1.35; }
.subhead h3 { font-size: 14px; }
.section-label {
  margin: 0 0 3px;
  color: var(--text-tertiary);
  font-size: 10px;
  font-weight: 750;
  letter-spacing: 0;
  text-transform: uppercase;
}
.input-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 230px;
  align-items: end;
  gap: 14px;
}
.field { min-width: 0; display: flex; flex-direction: column; gap: 7px; }
.field label, .custom-tpl label { color: var(--text-secondary); font-size: 12px; font-weight: 650; }
.field-action { min-height: 42px; align-self: end; }
input[type="text"], select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-raised);
  color: var(--text);
  outline: 0;
}
input[type="text"], select { height: 42px; padding: 0 12px; }
textarea { padding: 10px 12px; line-height: 1.55; resize: vertical; }
input::placeholder, textarea::placeholder { color: #76818b; }
input[type="text"]:hover, select:hover, textarea:hover { border-color: var(--line-strong); }
input[type="text"]:focus, select:focus, textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(227, 174, 86, .12);
}
.custom-tpl {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 170px minmax(0, 1fr);
  align-items: center;
  gap: 8px 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.input-hint { margin: 10px 0 0; color: var(--text-tertiary); font-size: 12px; line-height: 1.6; }

/* Buttons */
.btn {
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 700;
  transition: background-color .16s, border-color .16s, color .16s;
}
.btn.primary { background: var(--brand); color: #17130d; }
.btn.primary:hover { background: var(--brand-hover); }
.btn.secondary { border-color: var(--line); background: var(--surface-raised); color: var(--text-secondary); }
.btn.secondary:hover { border-color: var(--line-strong); background: var(--surface-hover); color: var(--text); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-tertiary); }
.btn.ghost:hover { color: var(--text); background: var(--surface-hover); }
.icon-btn {
  width: 32px;
  height: 32px;
  flex: 0 0 32px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 15px;
}
.icon-btn:hover { color: var(--text); border-color: var(--line-strong); background: var(--surface-hover); }
.icon-btn.faved { color: var(--brand); border-color: rgba(227, 174, 86, .45); }

/* Mandala workspace */
#tab-mandala.active {
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(320px, .8fr);
  gap: 16px;
  align-items: start;
}
#tab-mandala .input-card { grid-column: 1 / -1; }
.board-head { margin-bottom: 10px; }
.pick-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-raised);
  color: var(--text-tertiary);
  font-size: 11px;
  white-space: nowrap;
}
.pick-status i { color: var(--line-strong); font-style: normal; }
.pick-status .status-inner { color: var(--inner); }
.pick-status .status-people { color: var(--people); }
.pick-status .status-scene { color: var(--scene); }
.pick-status b { color: var(--text); }
.legend { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; color: var(--text-tertiary); font-size: 11px; }
.legend span::before { content: ""; width: 7px; height: 7px; border-radius: 2px; background: currentColor; }
.legend-inner { color: var(--inner) !important; }
.legend-people { color: var(--people) !important; }
.legend-scene { color: var(--scene) !important; }
.legend .board-help {
  margin-left: auto;
  color: var(--text-tertiary) !important;
}
.legend .board-help::before { display: none; }
.brand-help {
  margin: -3px 0 12px;
  color: var(--text-tertiary);
  font-size: 11px;
  line-height: 1.5;
}
.mandala-grid, .brand-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
}
.mandala-cell, .brand-cell {
  position: relative;
  min-width: 0;
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-raised);
  color: var(--text-secondary);
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.42;
  overflow-wrap: anywhere;
  user-select: none;
  transition: border-color .15s, background-color .15s, color .15s, box-shadow .15s;
}
.mandala-cell.clickable, .brand-cell.clickable { cursor: pointer; }
.mandala-cell.clickable:hover, .brand-cell.clickable:hover { border-color: var(--line-strong); background: var(--surface-hover); color: var(--text); }
.mandala-cell:focus-visible, .brand-cell:focus-visible { position: relative; z-index: 3; }
.mandala-cell.inner, .brand-cell.inner { border-color: rgba(79, 209, 178, .25); color: #8fe3cf; background: rgba(79, 209, 178, .055); }
.mandala-cell.people { border-color: rgba(134, 168, 255, .22); color: #adc3ff; background: rgba(134, 168, 255, .05); }
.mandala-cell.scene { border-color: rgba(255, 139, 119, .22); color: #ffb0a3; background: rgba(255, 139, 119, .05); }
.mandala-cell.center, .brand-cell.center {
  grid-column: 3;
  grid-row: 3;
  border-color: var(--brand);
  background: var(--brand);
  color: #18130b;
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 0 0 3px rgba(227, 174, 86, .10);
}
.mandala-cell.selected, .brand-cell.selected {
  color: var(--text);
  border-color: currentColor;
  background: var(--surface-hover);
  box-shadow: inset 0 0 0 1px currentColor;
}
.mandala-cell.selected::after, .brand-cell.selected::after {
  content: "✓";
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 11px;
  font-weight: 800;
}
.mandala-cell.inner.selected, .brand-cell.inner.selected { color: var(--inner); }
.mandala-cell.people.selected { color: var(--people); }
.mandala-cell.scene.selected { color: var(--scene); }
.mandala-cell.editable, .brand-cell.editable { cursor: text; }
.mandala-cell.editing, .brand-cell.editing {
  user-select: text;
  outline: 0;
  color: var(--text) !important;
  border-color: var(--brand) !important;
  background: var(--surface-hover) !important;
  box-shadow: 0 0 0 3px rgba(227, 174, 86, .12) !important;
}
.mandala-cell.rolling {
  z-index: 2;
  border-color: var(--brand) !important;
  background: rgba(227, 174, 86, .16) !important;
  color: var(--text) !important;
  box-shadow: 0 0 0 3px rgba(227, 174, 86, .12);
}
.board-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.secondary-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.board-toolbar .primary { min-width: 112px; }

/* Output lists */
.topics-card {
  position: sticky;
  top: 80px;
  max-height: calc(100vh - 100px);
  overflow: auto;
  scrollbar-color: var(--line-strong) transparent;
}
.count-badge {
  min-width: 28px;
  height: 24px;
  display: inline-grid;
  place-items: center;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--surface-raised);
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
}
.empty-tip {
  margin: 0;
  padding: 28px 12px;
  border: 1px dashed var(--line);
  border-radius: 6px;
  color: var(--text-tertiary);
  text-align: center;
  font-size: 12px;
}
.topic-list { display: flex; flex-direction: column; }
.topic-item {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) auto;
  align-items: start;
  gap: 10px;
  padding: 13px 0;
  border-top: 1px solid var(--line);
}
.topic-item:first-child { border-top: 0; padding-top: 2px; }
.topic-num {
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 5px;
  background: var(--surface-hover);
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 800;
}
.topic-body { min-width: 0; }
.topic-title { color: var(--text); font-size: 13px; font-weight: 650; line-height: 1.55; }
.topic-tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 7px; }
.tag {
  padding: 2px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--text-tertiary);
  font-size: 10px;
  line-height: 1.4;
}
.topic-actions { display: flex; gap: 5px; }
.topic-actions .icon-btn { width: 28px; height: 28px; flex-basis: 28px; }
.favs-block { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.subhead { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }

/* Brand mode */
#tab-brand.active {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(340px, .9fr);
  gap: 16px;
  align-items: start;
}
#tab-brand .input-card { grid-column: 1 / -1; }
#tab-brand .brand-angles { grid-column: 2; grid-row: 2 / span 2; }
#tab-brand .structure-card { grid-column: 1; }
.brand-cell.outer { color: var(--text-secondary); }
.structure-list { display: flex; flex-direction: column; gap: 10px; margin: 0; padding-left: 24px; }
.structure-list li { padding-left: 4px; color: var(--text-secondary); font-size: 13px; line-height: 1.65; }
.structure-list li::marker { color: var(--brand); font-weight: 800; }

/* POV mode */
#tab-pov.active { display: block; }
.pov-input-card { grid-template-columns: minmax(220px, 1fr) minmax(220px, 1fr) 200px auto; }
.custom-style-fields {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.pov-results { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.pov-out {
  min-height: 300px;
  max-height: 500px;
  margin: 0;
  padding: 14px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-raised);
  color: var(--text-secondary);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Microsoft YaHei", monospace;
  font-size: 12px;
  line-height: 1.75;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Footer and transient status */
.footer { padding: 22px 0 32px; border-top: 1px solid var(--line); }
.footer p { margin: 0; color: var(--text-tertiary); text-align: center; font-size: 11px; }
.footer a { color: var(--text-secondary); text-decoration: none; }
.toast {
  position: fixed;
  z-index: 100;
  bottom: 24px;
  left: 50%;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #f3f4f5;
  color: #16181a;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .35);
  font-size: 12px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 18px);
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 980px) {
  .mode-bar { align-items: stretch; flex-direction: column; gap: 12px; }
  .tabs { align-self: flex-start; }
  .workflow { justify-content: space-between; }
  #tab-mandala.active, #tab-brand.active { grid-template-columns: minmax(0, 1fr); }
  #tab-mandala .input-card, #tab-brand .input-card,
  #tab-brand .brand-angles, #tab-brand .structure-card { grid-column: 1; grid-row: auto; }
  .topics-card { position: static; max-height: none; }
  .pov-input-card { grid-template-columns: 1fr 1fr; }
  .pov-input-card .style-field { grid-column: 1; }
  .pov-input-card .field-action { grid-column: 2; }
}

@media (max-width: 700px) {
  .container { width: min(100% - 24px, 1240px); }
  .header-top { min-height: 58px; }
  .brand-logo, .brand-logo img { width: 36px; height: 36px; flex-basis: 36px; }
  .brand-text strong { font-size: 15px; }
  .brand-text span { display: none; }
  .page-shell { padding-top: 12px; }
  .mode-bar { gap: 10px; margin-bottom: 12px; }
  .tabs { width: 100%; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tab { min-width: 0; padding-inline: 6px; font-size: 12px; white-space: nowrap; }
  .workflow { gap: 4px; }
  .workflow li { flex: 1 1 0; flex-direction: column; gap: 3px; font-size: 10px; text-align: center; white-space: normal; }
  .workflow span { width: 18px; height: 18px; }
  .card { padding: 12px; }
  .card-head { margin-bottom: 11px; }
  .card-head h2 { font-size: 15px; }
  .input-card, .pov-input-card { grid-template-columns: minmax(0, 1fr); gap: 11px; }
  .field-action, .pov-input-card .style-field, .pov-input-card .field-action { grid-column: 1; }
  .custom-tpl { grid-template-columns: minmax(0, 1fr); gap: 6px; }
  .custom-tpl textarea { margin-bottom: 5px; }
  .custom-style-fields { grid-template-columns: 1fr; }
  .mandala-grid, .brand-grid { gap: 4px; }
  .mandala-cell, .brand-cell {
    min-height: 68px;
    padding: 7px 4px;
    border-radius: 5px;
    font-size: 11px;
    line-height: 1.35;
  }
  .mandala-cell.center, .brand-cell.center { font-size: 12px; }
  .pick-status { padding: 6px 7px; font-size: 10px; }
  .legend { gap: 9px; }
  .legend span { font-size: 10px; }
  .board-toolbar { align-items: stretch; flex-direction: column; }
  .board-toolbar .primary { order: -1; min-height: 42px; width: 100%; }
  .secondary-actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .secondary-actions .btn { min-width: 0; padding-inline: 5px; font-size: 11px; }
  .topic-item { grid-template-columns: 24px minmax(0, 1fr); }
  .topic-actions { grid-column: 2; }
  .pov-results { grid-template-columns: 1fr; }
  .pov-out { min-height: 240px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
