:root {
  color-scheme: light dark;
  --bg: #f5f5f7;
  --card: rgba(255, 255, 255, 0.92);
  --card-strong: #ffffff;
  --surface: #f0f0f4;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --soft: #8e8e93;
  --line: rgba(0, 0, 0, 0.14);
  --line-strong: rgba(0, 0, 0, 0.2);
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.14);
  --green-soft: rgba(52, 199, 89, 0.14);
  --shadow: 0 18px 46px rgba(0, 0, 0, 0.08);
}

body.dark {
  --bg: #111114;
  --card: rgba(28, 28, 32, 0.92);
  --card-strong: #202025;
  --surface: #2a2a30;
  --text: #f5f5f7;
  --muted: #a1a1a6;
  --soft: #8e8e93;
  --line: rgba(255, 255, 255, 0.14);
  --line-strong: rgba(255, 255, 255, 0.22);
  --accent-soft: rgba(10, 132, 255, 0.2);
  --green-soft: rgba(52, 199, 89, 0.18);
  --shadow: 0 18px 54px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:active {
  transform: scale(0.985);
}

h1,
h2,
h3,
h4,
p {
  margin: 0;
}

.app-shell {
  width: min(1220px, 100%);
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) clamp(14px, 3vw, 28px) 34px;
}

.topbar,
.brand-lockup,
.top-actions,
.workspace,
.panel-heading,
.detail-header,
.section-heading,
.story-card,
.character-top,
.revision-button {
  display: flex;
}

.topbar {
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.brand-lockup {
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-icon,
.story-thumb,
.character-thumb {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  background: var(--card-strong);
  border: 1px solid var(--line-strong);
  color: var(--accent);
  font-weight: 800;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.06);
}

.brand-icon {
  width: 50px;
  height: 50px;
  border-radius: 16px;
}

.eyebrow {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  font-size: clamp(30px, 4vw, 48px);
  line-height: 1.02;
}

h2 {
  font-size: clamp(24px, 3vw, 38px);
  line-height: 1.08;
}

h3 {
  font-size: 20px;
}

h4 {
  font-size: 17px;
}

.top-actions {
  align-items: center;
  gap: 10px;
}

.primary-button,
.icon-button {
  min-height: 46px;
  border-radius: 15px;
  padding: 0 16px;
  font-weight: 750;
  background: var(--surface);
  border: 1px solid var(--line);
}

.primary-button {
  background: var(--accent);
  color: #ffffff;
  border-color: transparent;
}

.primary-button.compact {
  min-width: 76px;
}

.status-row {
  min-height: 38px;
  display: flex;
  align-items: center;
  margin-bottom: 14px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  color: var(--muted);
  font-size: 14px;
}

.workspace {
  align-items: flex-start;
  gap: 16px;
  min-width: 0;
}

.library-panel,
.detail-panel,
.summary-card,
.revision-card,
.read-card,
.character-card,
.stat-card,
.mini-empty {
  background: var(--card);
  border: 1px solid var(--line-strong);
  box-shadow: var(--shadow);
}

.library-panel {
  width: 330px;
  min-width: 280px;
  position: sticky;
  top: 16px;
  padding: 16px;
  border-radius: 24px;
}

.panel-heading {
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.count-pill {
  min-width: 34px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 800;
}

.search-input {
  width: 100%;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 15px;
  padding: 0 14px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  margin-bottom: 12px;
}

.story-list {
  display: grid;
  gap: 10px;
}

.story-card {
  width: 100%;
  min-height: 82px;
  align-items: center;
  gap: 11px;
  padding: 10px;
  text-align: left;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: var(--surface);
}

.story-card.active {
  border-color: color-mix(in srgb, var(--accent) 72%, var(--line-strong));
  background: var(--accent-soft);
}

.story-thumb,
.character-thumb {
  width: 50px;
  height: 50px;
  border-radius: 15px;
  background: color-mix(in srgb, var(--accent-soft) 70%, var(--card-strong));
}

.story-thumb.image-thumb {
  overflow: hidden;
  padding: 0;
  background: var(--card-strong);
}

.story-thumb img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.story-copy {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.story-copy strong,
.story-copy span,
.story-copy small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.story-copy span {
  color: var(--muted);
  font-size: 13px;
}

.story-copy small {
  color: var(--soft);
  font-size: 12px;
}

.detail-panel {
  flex: 1;
  min-width: 0;
  width: 100%;
  border-radius: 28px;
  padding: clamp(15px, 2.5vw, 24px);
}

.empty-state {
  display: grid;
  gap: 9px;
  min-height: 420px;
  align-content: center;
  text-align: center;
  color: var(--muted);
  padding: 24px;
}

.empty-state h2 {
  color: var(--text);
}

.story-detail {
  display: grid;
  gap: 16px;
}

.detail-header {
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.updated-line {
  margin-top: 7px;
  color: var(--muted);
  font-size: 14px;
  overflow-wrap: anywhere;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(82px, 1fr));
  gap: 8px;
  min-width: min(310px, 100%);
}

.stat-card {
  display: grid;
  gap: 2px;
  padding: 12px;
  border-radius: 18px;
  text-align: center;
  box-shadow: none;
}

.stat-card strong {
  font-size: 22px;
}

.stat-card span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.summary-card,
.revision-card {
  display: grid;
  gap: 10px;
  padding: 16px;
  border-radius: 22px;
  box-shadow: none;
}

.summary-card p:last-child {
  color: var(--text);
  line-height: 1.5;
}

.compact-revision {
  grid-template-columns: minmax(0, 1fr) minmax(220px, 340px);
  align-items: end;
  gap: 14px;
}

.section-heading {
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.revision-select-wrap {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.revision-picker-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.revision-select {
  width: 100%;
  min-width: 0;
  height: 48px;
  border: 1px solid var(--line-strong);
  border-radius: 15px;
  padding: 0 40px 0 12px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  font-weight: 650;
}

.revision-select:focus {
  border-color: color-mix(in srgb, var(--accent) 70%, var(--line-strong));
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.revision-button {
  width: 100%;
  min-height: 56px;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-radius: 16px;
  border: 1px solid var(--line);
  padding: 10px 12px;
  background: var(--surface);
  text-align: left;
}

.revision-button.active {
  border-color: color-mix(in srgb, var(--accent) 78%, var(--line-strong));
  background: var(--accent-soft);
}

.revision-button span {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.revision-button strong,
.revision-button small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.revision-button small {
  color: var(--muted);
  font-size: 12px;
}

.revision-button em {
  flex: 0 0 auto;
  padding: 5px 8px;
  border-radius: 999px;
  background: var(--green-soft);
  color: #30a14e;
  font-size: 12px;
  font-style: normal;
  font-weight: 800;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 340px);
  gap: 16px;
  min-width: 0;
}

.main-column,
.side-column,
.section-stack,
.character-stack {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  align-content: start;
  min-width: 0;
}

.read-card,
.character-card,
.mini-empty {
  width: 100%;
  min-width: 0;
  border-radius: 22px;
  padding: 15px;
  box-shadow: none;
}

.read-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.read-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.read-card-header span {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.body-copy {
  color: var(--text);
  line-height: 1.58;
  white-space: pre-wrap;
}

.body-copy.small {
  color: var(--muted);
  font-size: 14px;
}

.scene-notes {
  display: grid;
  gap: 7px;
  padding: 12px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.scene-notes p {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.42;
}

.scene-notes strong {
  color: var(--text);
}

.character-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.character-top {
  align-items: center;
  gap: 10px;
  min-width: 0;
  width: 100%;
}

.character-top div {
  min-width: 0;
}

.character-top h4,
.character-top p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.character-top p {
  color: var(--muted);
  font-size: 13px;
}

dl {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 7px;
  margin: 0;
  min-width: 0;
}

dl div {
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr);
  gap: 8px;
  min-width: 0;
}

dt {
  color: var(--soft);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

dd {
  margin: 0;
  color: var(--text);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.mini-empty {
  color: var(--muted);
  display: grid;
  gap: 4px;
  box-shadow: none;
}

.mini-empty span {
  font-size: 13px;
}

@media (max-width: 860px) {
  .app-shell {
    padding-inline: 12px;
  }

  .topbar,
  .workspace,
  .detail-header,
  .content-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    width: 100%;
    min-width: 0;
  }

  .topbar {
    grid-template-columns: 1fr;
  }

  .top-actions {
    justify-content: stretch;
  }

  .top-actions button {
    flex: 1;
  }

  .library-panel {
    position: static;
    width: 100%;
    min-width: 0;
  }

  .story-list {
    display: flex;
    overflow-x: auto;
    padding-bottom: 2px;
    scroll-snap-type: x mandatory;
  }

  .story-card {
    min-width: min(310px, 86vw);
    scroll-snap-align: start;
  }

  .detail-panel {
    border-radius: 24px;
    min-width: 0;
    overflow: hidden;
  }

  .stat-grid {
    grid-template-columns: repeat(3, 1fr);
    width: 100%;
    min-width: 0;
  }

  .content-grid {
    grid-template-columns: 1fr;
  }

  .compact-revision {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 480px) {
  .brand-icon {
    width: 44px;
    height: 44px;
    border-radius: 14px;
  }

  h1 {
    font-size: 28px;
  }

  .detail-panel,
  .library-panel {
    padding: 13px;
  }

  .stat-grid {
    gap: 6px;
  }

  .stat-card {
    padding: 10px 8px;
    border-radius: 15px;
  }

  .stat-card strong {
    font-size: 19px;
  }
}
