:root {
  color-scheme: light;
  --bg: oklch(97.5% 0.004 25);
  --surface: oklch(100% 0 0);
  --surface-muted: oklch(96% 0.007 25);
  --surface-hover: oklch(94.5% 0.012 25);
  --surface-selected: oklch(94% 0.028 25);
  --line: oklch(87% 0.012 25);
  --line-strong: oklch(76% 0.02 25);
  --text: oklch(24% 0.016 25);
  --muted: oklch(43% 0.018 25);
  --subtle: oklch(51% 0.014 25);
  --accent: oklch(50% 0.185 25);
  --accent-hover: oklch(44% 0.175 25);
  --accent-soft: oklch(94% 0.035 25);
  --success: oklch(40% 0.12 150);
  --success-soft: oklch(95% 0.025 150);
  --warning: oklch(43% 0.11 75);
  --warning-soft: oklch(95% 0.04 75);
  --danger-soft: oklch(95% 0.035 25);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --focus: oklch(57% 0.18 25);
  --topbar-height: 72px;
  --tabs-height: 52px;
  --z-dialog: 40;
  --z-toast: 50;
  --z-lightbox: 55;
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
  font-size: 16px;
}

body {
  display: grid;
  grid-template-rows: var(--topbar-height) var(--tabs-height) minmax(0, 1fr);
  overflow: hidden;
}

button,
input,
textarea { font: inherit; }

button {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background-color 180ms cubic-bezier(.22, 1, .36, 1), border-color 180ms cubic-bezier(.22, 1, .36, 1), color 180ms cubic-bezier(.22, 1, .36, 1), transform 120ms ease-out;
}

button:hover { color: var(--text); }
button:active { transform: translateY(1px); }
button:disabled { cursor: not-allowed; opacity: .52; transform: none; }
button:focus { outline: none; }
button:focus-visible,
input:focus-visible,
textarea:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.hidden { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand { min-width: 0; display: flex; align-items: center; gap: 11px; }
.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 9px;
  background: var(--accent);
  color: white;
  font-size: .8rem;
  font-weight: 800;
  letter-spacing: -.01em;
}
.brand h1 { margin: 0; font-size: 1.25rem; line-height: 1.2; font-weight: 750; }
.brand p { margin: 3px 0 0; color: var(--muted); font-size: .78rem; }

.topbar-actions { min-width: 0; display: flex; align-items: center; justify-content: flex-end; gap: 7px; }
.topbar-link {
  min-height: 34px;
  padding: 7px 10px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: .78rem;
  font-weight: 650;
  text-decoration: none;
  white-space: nowrap;
}
.topbar-link:hover { border-color: var(--line-strong); background: var(--surface-hover); }

.connection {
  min-height: 34px;
  padding: 7px 10px;
  display: flex;
  align-items: center;
  gap: 7px;
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
  color: var(--muted);
  font-size: .8rem;
  font-weight: 650;
}
.connection-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warning); }
.connection.ready .connection-dot { background: var(--success); }
.connection.error .connection-dot { background: var(--accent); }

.tabs {
  padding: 5px 18px;
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs button { min-height: 40px; padding: 7px 15px; flex: 0 0 auto; font-size: .9rem; font-weight: 680; }
.tabs button:hover { background: var(--surface-hover); }
.tabs button.active { background: var(--accent-soft); color: var(--accent-hover); }

.app-shell,
.view { min-width: 0; min-height: 0; }

.story-view { height: 100%; display: grid; grid-template-columns: 300px minmax(0, 1fr); }
.navigation-pane,
.asset-navigation {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border-right: 1px solid var(--line);
  background: var(--surface-muted);
}
.pane-heading {
  min-height: 68px;
  padding: 12px 14px 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
}
.pane-heading > div { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.pane-heading strong { font-size: .92rem; }
.pane-heading span { color: var(--muted); font-size: .78rem; }
.compact-action { min-height: 32px; padding: 4px 7px; border-color: var(--line); background: var(--surface); font-size: .72rem; }

.story-tree { min-height: 0; padding: 10px; overflow-y: auto; scrollbar-gutter: stable; }
.story-arc + .story-arc { margin-top: 8px; }
.tree-button { width: 100%; min-height: 40px; padding: 7px 8px; display: flex; align-items: center; gap: 7px; text-align: left; }
.tree-button:hover { background: var(--surface-hover); }
.tree-button .chevron { flex: 0 0 auto; width: 14px; color: var(--subtle); font-size: .7rem; }
.tree-button .label { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-button .count { flex: 0 0 auto; color: var(--subtle); font-size: .72rem; font-variant-numeric: tabular-nums; }
.arc-button { color: var(--text); font-size: .9rem; font-weight: 720; }
.chapter-group { margin-left: 12px; }
.chapter-button { color: var(--text); font-size: .83rem; font-weight: 650; }
.node-button { padding-left: 29px; color: var(--muted); font-size: .79rem; }
.node-button.active { background: var(--surface-selected); color: var(--accent-hover); }

.content-pane { min-width: 0; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); background: var(--surface); }
.content-toolbar {
  min-height: 74px;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  border-bottom: 1px solid var(--line);
}
.toolbar-copy { min-width: 0; }
.toolbar-copy h2 { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 1.06rem; line-height: 1.25; }
.toolbar-copy p { margin: 5px 0 0; color: var(--muted); font-size: .8rem; }
.button { min-height: 42px; padding: 8px 13px; font-size: .84rem; font-weight: 700; white-space: nowrap; }
.button.primary { border-color: var(--accent); background: var(--accent); color: white; }
.button.primary:hover { border-color: var(--accent-hover); background: var(--accent-hover); }
.button.secondary { border-color: var(--line); background: var(--surface); color: var(--text); }
.button.secondary:hover { border-color: var(--line-strong); background: var(--surface-hover); }

.story-lines { min-height: 0; padding: 0 26px 60px; overflow-y: auto; scrollbar-gutter: stable; }
.story-line {
  --scene-height: 148px;
  max-width: 1120px;
  margin: 0 auto;
  padding: 21px 4px;
  display: grid;
  grid-template-columns: 36px minmax(340px, 1fr) 230px;
  gap: 12px;
  align-items: start;
  border-bottom: 1px solid var(--line);
  content-visibility: auto;
  contain-intrinsic-size: 192px;
}
.story-line.changed { background: var(--warning-soft); }
.line-number { padding-top: 3px; color: var(--subtle); font-size: .78rem; font-variant-numeric: tabular-nums; text-align: right; }
.line-copy { min-width: 0; }
.line-heading { min-height: 32px; margin-bottom: 7px; display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.line-heading strong { font-size: .84rem; line-height: 1.4; }
.save-record {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: .72rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.4;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.save-record.pending { color: var(--warning); font-weight: 700; }
.line-copy textarea {
  width: 100%;
  height: var(--scene-height);
  min-height: var(--scene-height);
  padding: 10px 11px;
  resize: none;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: .96rem;
  line-height: 1.55;
}
.line-copy textarea:focus { border-color: var(--accent); }
.original-copy { margin-top: 7px; }
.original-copy summary { width: max-content; cursor: pointer; color: var(--muted); font-size: .8rem; }
.original-copy p { max-width: 72ch; margin: 7px 0 0; padding: 8px 10px; border-radius: var(--radius-sm); background: var(--surface-muted); font-size: .85rem; line-height: 1.5; }
.scene-column { min-width: 0; }
.scene-label { height: 32px; margin-bottom: 7px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: .72rem; line-height: 32px; }
.scene-frame { position: relative; width: 100%; height: var(--scene-height); overflow: hidden; border-radius: var(--radius-sm); background: oklch(18% .01 25); }
.scene-frame.expandable { cursor: zoom-in; }
.scene-frame.expandable:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.scene-background,
.scene-layer,
.scene-character { position: absolute; display: block; max-width: none; }
.scene-background { inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scene-layer { inset: 0; z-index: 1; width: 100%; height: 100%; object-fit: cover; }
.scene-character { z-index: 2; object-fit: contain; object-position: center bottom; }
.scene-character.centered { left: 50%; bottom: 0; height: 92%; transform: translateX(-50%); }
.scene-character.reflected { transform: scaleX(-1); }
.scene-character.centered.reflected { transform: translateX(-50%) scaleX(-1); }
.scene-empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 12px; color: oklch(77% .01 25); font-size: .72rem; text-align: center; }

.scene-lightbox {
  position: fixed;
  inset: 0;
  z-index: var(--z-lightbox);
  display: grid;
  place-items: center;
  padding: 24px;
  background: oklch(12% .01 25 / .78);
  cursor: zoom-out;
}
.scene-lightbox:focus { outline: none; }
.scene-lightbox-frame {
  width: min(960px, calc(100vw - 48px), calc((100vh - 48px) * 1.5));
  height: auto;
  aspect-ratio: 3 / 2;
  border-radius: var(--radius-md);
  cursor: default;
  box-shadow: 0 4px 8px oklch(8% .01 25 / .38);
}

.skeleton-list { max-width: 1120px; margin: 0 auto; padding-top: 20px; }
.skeleton-list div { height: 176px; margin-bottom: 12px; border-radius: var(--radius-sm); background: linear-gradient(90deg, var(--surface-muted), var(--surface-hover), var(--surface-muted)); background-size: 220% 100%; animation: skeleton 1.2s ease-in-out infinite; }
@keyframes skeleton { to { background-position: -220% 0; } }

.asset-view { height: 100%; display: grid; grid-template-columns: 330px minmax(0, 1fr); }
.asset-navigation { grid-template-rows: auto minmax(0, 1fr) auto; }
.asset-search { padding: 14px; border-bottom: 1px solid var(--line); }
.asset-search label { display: block; margin-bottom: 7px; color: var(--text); font-size: .8rem; font-weight: 680; }
.asset-search input { width: 100%; min-height: 42px; padding: 8px 10px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); }
.asset-search input::placeholder { color: var(--muted); }
.asset-search span { display: block; margin-top: 7px; color: var(--muted); font-size: .75rem; }
.asset-list { min-height: 0; padding: 8px; overflow-y: auto; scrollbar-gutter: stable; }
.asset-item { width: 100%; min-height: 48px; padding: 8px 9px; display: block; text-align: left; }
.asset-item:hover { background: var(--surface-hover); }
.asset-item.active { background: var(--surface-selected); color: var(--accent-hover); }
.asset-item strong,
.asset-item span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.asset-item strong { color: inherit; font-size: .82rem; }
.asset-item span { margin-top: 3px; color: var(--subtle); font-size: .7rem; }
.load-more { margin: 8px; border-color: var(--line); background: var(--surface); color: var(--text); }
.asset-preview { min-width: 0; min-height: 0; padding: 28px; overflow-y: auto; background: var(--surface); }
.empty-state { min-height: 100%; display: grid; place-content: center; justify-items: center; padding: 30px; color: var(--muted); text-align: center; }
.empty-mark { width: 62px; height: 62px; display: grid; place-items: center; margin-bottom: 13px; border-radius: var(--radius-lg); background: var(--surface-muted); color: var(--accent-hover); font-size: 1rem; font-weight: 800; }
.empty-state h2 { margin: 0; color: var(--text); font-size: 1rem; }
.empty-state p { max-width: 48ch; margin: 7px 0 0; font-size: .82rem; line-height: 1.5; }
.asset-detail { max-width: 940px; margin: 0 auto; }
.asset-detail-header { margin-bottom: 18px; display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.asset-detail-header h2 { margin: 0; overflow-wrap: anywhere; font-size: 1.05rem; }
.asset-detail-header p { margin: 6px 0 0; color: var(--muted); font-size: .76rem; overflow-wrap: anywhere; }
.asset-actions { flex: 0 0 auto; display: flex; gap: 7px; }
.asset-stage { min-height: 420px; display: grid; place-items: center; padding: 20px; border-radius: var(--radius-md); background: var(--surface-muted); }
.asset-stage img,
.asset-stage video { display: block; max-width: 100%; max-height: min(62vh, 650px); object-fit: contain; }
.asset-stage audio { width: min(560px, 100%); }
.asset-facts { margin: 16px 0 0; display: flex; flex-wrap: wrap; gap: 8px 18px; color: var(--muted); font-size: .78rem; }

.flow-view { height: 100%; display: grid; grid-template-rows: auto minmax(0, 1fr); background: var(--surface); }
.flow-intro { min-height: 78px; padding: 14px 20px; display: flex; align-items: center; justify-content: space-between; gap: 18px; border-bottom: 1px solid var(--line); }
.flow-intro h2 { margin: 0; font-size: 1.06rem; }
.flow-intro p { max-width: 72ch; margin: 5px 0 0; color: var(--muted); font-size: .8rem; line-height: 1.45; }
.read-only-badge { padding: 5px 8px; border-radius: var(--radius-sm); background: var(--surface-muted); color: var(--muted); font-size: .72rem; font-weight: 700; }
.flow-layout { min-width: 0; min-height: 0; display: grid; grid-template-columns: 300px minmax(0, 1fr); }
.flow-sections { min-height: 0; padding: 9px; overflow-y: auto; border-right: 1px solid var(--line); background: var(--surface-muted); }
.flow-section-button { width: 100%; min-height: 46px; padding: 8px 9px; text-align: left; }
.flow-section-button:hover { background: var(--surface-hover); }
.flow-section-button.active { background: var(--surface-selected); color: var(--accent-hover); }
.flow-section-button strong,
.flow-section-button span { display: block; }
.flow-section-button strong { font-size: .82rem; }
.flow-section-button span { margin-top: 3px; color: var(--subtle); font-size: .7rem; }
.flow-nodes { min-height: 0; padding: 20px 24px 60px; overflow-y: auto; }
.flow-node { max-width: 860px; margin: 0 auto; padding: 16px 4px; border-bottom: 1px solid var(--line); }
.flow-node h3 { margin: 0; font-size: .93rem; }
.flow-node p { max-width: 72ch; margin: 7px 0 0; color: var(--muted); font-size: .82rem; line-height: 1.5; }
.flow-tags { margin-top: 9px; display: flex; flex-wrap: wrap; gap: 5px; }
.flow-tag { padding: 3px 6px; border-radius: 4px; background: var(--surface-muted); color: var(--muted); font-size: .68rem; }

.toast { position: fixed; right: 18px; bottom: 18px; z-index: var(--z-toast); max-width: min(420px, calc(100vw - 36px)); padding: 11px 13px; border-radius: var(--radius-md); background: var(--text); color: white; font-size: .82rem; line-height: 1.45; box-shadow: 0 4px 8px oklch(20% .01 25 / .18); }
.toast.error { background: var(--accent-hover); }
.toast.success { background: var(--success); }

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 22px;
  background: var(--bg);
}
.login-panel {
  width: min(390px, 100%);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface);
}
.login-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  margin-bottom: 18px;
  border-radius: 11px;
  background: var(--accent);
  color: white;
  font-size: .88rem;
  font-weight: 800;
}
.login-panel h2 { margin: 0; font-size: 1.2rem; }
.login-panel > p { margin: 8px 0 20px; color: var(--muted); font-size: .84rem; line-height: 1.5; }
.login-panel label { display: block; margin: 13px 0 6px; color: var(--text); font-size: .8rem; font-weight: 680; }
.login-panel input { width: 100%; min-height: 44px; padding: 9px 10px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); }
.login-panel .button { width: 100%; margin-top: 18px; }
.login-panel .login-error { margin: 10px 0 0; padding: 9px 10px; border-radius: var(--radius-sm); background: var(--danger-soft); color: var(--accent-hover); font-size: .78rem; }

.dialog { z-index: var(--z-dialog); width: min(480px, calc(100vw - 32px)); padding: 0; border: 0; border-radius: var(--radius-lg); color: var(--text); }
.dialog::backdrop { background: oklch(12% .01 25 / .46); }
.dialog form { padding: 22px; }
.dialog h2 { margin: 0; font-size: 1.06rem; }
.dialog p { margin: 10px 0 0; color: var(--muted); font-size: .86rem; line-height: 1.55; white-space: pre-line; }
.dialog-actions { margin-top: 20px; display: flex; justify-content: flex-end; gap: 8px; }

@media (max-width: 860px) {
  body { height: auto; min-height: 100%; display: block; overflow: auto; }
  .topbar { min-height: var(--topbar-height); position: sticky; top: 0; z-index: 10; }
  .tabs { min-height: var(--tabs-height); position: sticky; top: var(--topbar-height); z-index: 9; }
  .app-shell { min-height: calc(100vh - var(--topbar-height) - var(--tabs-height)); }
  .story-view,
  .asset-view,
  .flow-layout { height: auto; min-height: inherit; display: block; }
  .navigation-pane,
  .asset-navigation,
  .flow-sections { border-right: 0; border-bottom: 1px solid var(--line); }
  .navigation-pane.collapsed .story-tree { display: none; }
  .story-tree,
  .asset-list,
  .flow-sections { max-height: none; overflow: visible; }
  .content-pane { display: block; }
  .content-toolbar { position: sticky; top: calc(var(--topbar-height) + var(--tabs-height)); z-index: 8; background: var(--surface); }
  .story-lines,
  .asset-preview,
  .flow-nodes { overflow: visible; }
  .story-line { grid-template-columns: 30px minmax(0, 1fr); }
  .scene-column { grid-column: 2; }
  .asset-stage { min-height: 280px; }
}

@media (max-width: 560px) {
  :root { --topbar-height: 66px; --tabs-height: 48px; }
  .topbar { padding: 9px 12px; }
  .brand-mark { width: 34px; height: 34px; }
  .brand h1 { font-size: 1rem; }
  .connection { padding-inline: 8px; }
  .connection span:last-child { display: none; }
  .topbar-link { padding-inline: 8px; font-size: .72rem; }
  #trustDeviceButton,
  #logoutButton { display: none; }
  .tabs { padding-inline: 8px; }
  .tabs button { min-height: 38px; padding-inline: 11px; font-size: .82rem; }
  .content-toolbar { padding: 10px 12px; align-items: flex-start; }
  .toolbar-copy p { display: none; }
  .story-lines { padding: 0 10px 42px; }
  .story-line { --scene-height: 180px; grid-template-columns: 24px minmax(0, 1fr); gap: 8px; padding-block: 18px; }
  .line-number { font-size: .7rem; }
  .scene-column { grid-column: 2; }
  .scene-lightbox { padding: 12px; }
  .scene-lightbox-frame { width: min(960px, calc(100vw - 24px), calc((100vh - 24px) * 1.5)); }
  .asset-preview { padding: 18px 12px 42px; }
  .asset-detail-header { display: block; }
  .asset-actions { margin-top: 12px; }
  .flow-intro { align-items: flex-start; }
  .flow-nodes { padding-inline: 14px; }
}

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