:root {
  --bg: #f7f3ed;
  --surface: #fffaf3;
  --surface-2: #f0e7da;
  --text: #241f1a;
  --muted: #756a5e;
  --border: #e4d7c6;
  --primary: #5b4636;
  --primary-contrast: #fff8ef;
  --danger: #8a2d2d;
  --danger-contrast: #fff7f7;
  --shadow: 0 18px 60px rgba(42, 33, 25, 0.10);
  --radius: 22px;
}

body.dark {
  --bg: #151311;
  --surface: #211d19;
  --surface-2: #2b251f;
  --text: #f6efe7;
  --muted: #b9ab9b;
  --border: #41372e;
  --primary: #d8bea1;
  --primary-contrast: #1c1713;
  --danger: #e58a8a;
  --danger-contrast: #261414;
  --shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: radial-gradient(circle at top left, rgba(255,255,255,0.7), transparent 28%), var(--bg);
  color: var(--text);
}

button, input, textarea, select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: .55;
}

.app-shell {
  width: min(1180px, calc(100% - 28px));
  margin: 0 auto;
  padding: 28px 0 48px;
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 22px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: .9rem;
  letter-spacing: .04em;
  text-transform: uppercase;
}

h1, h2, h3, p {
  margin-top: 0;
}

h1 {
  margin-bottom: 8px;
  font-size: clamp(2rem, 5vw, 4.2rem);
  line-height: .95;
  letter-spacing: -0.055em;
}

.subtitle {
  max-width: 760px;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 1.05rem;
}

.icon-button {
  min-width: 48px;
  height: 48px;
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text);
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px;
  margin-bottom: 18px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  position: sticky;
  top: 10px;
  z-index: 5;
  backdrop-filter: blur(14px);
}

.tab {
  flex: 1 1 auto;
  padding: 12px 16px;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
}

.tab.active {
  background: var(--primary);
  color: var(--primary-contrast);
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.grid {
  display: grid;
  gap: 14px;
}

.stats-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin-bottom: 18px;
}

.stat-card,
.panel,
.backup-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.stat-card {
  padding: 22px;
}

.stat-card span {
  display: block;
  margin-bottom: 4px;
  font-size: 2.35rem;
  line-height: 1;
  font-weight: 900;
  letter-spacing: -0.045em;
}

.stat-card p {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.content-grid {
  display: grid;
  grid-template-columns: 1.3fr .7fr;
  gap: 18px;
}

.panel {
  padding: 22px;
}

.panel.wide {
  min-height: 420px;
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
}

.panel-header h2 {
  margin-bottom: 4px;
  letter-spacing: -0.03em;
}

.stackable {
  align-items: center;
}

.muted {
  color: var(--muted);
  margin-bottom: 0;
}

.empty-state {
  padding: 24px;
  border: 1px dashed var(--border);
  border-radius: 18px;
  color: var(--muted);
  background: var(--surface-2);
}

.flow-list {
  margin: 0;
  padding-left: 22px;
  color: var(--muted);
  line-height: 1.7;
}

.chapter-form,
.reader-controls,
.filters {
  display: grid;
  gap: 14px;
}

label {
  display: grid;
  gap: 7px;
  font-weight: 800;
  color: var(--text);
}

input,
textarea,
select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 13px 14px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 16%, transparent);
}

textarea {
  min-height: 320px;
  resize: vertical;
  line-height: 1.55;
}

textarea.notes {
  min-height: 110px;
}

.two-col {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.primary,
.secondary,
.ghost,
.danger,
.small {
  padding: 12px 16px;
  border-radius: 14px;
  font-weight: 900;
}

.primary {
  background: var(--primary);
  color: var(--primary-contrast);
}

.secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.danger {
  background: var(--danger);
  color: var(--danger-contrast);
}

.small {
  padding: 9px 12px;
  font-size: .9rem;
}

.analysis-box {
  padding: 16px;
  border-radius: 18px;
  background: var(--surface-2);
  color: var(--text);
  line-height: 1.6;
}

.analysis-box strong {
  display: inline-block;
  min-width: 120px;
}

.filters {
  grid-template-columns: minmax(220px, 1fr) 170px 170px;
}

.chapter-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.chapter-card {
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg);
}

.chapter-card h3 {
  margin-bottom: 8px;
  letter-spacing: -0.025em;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 6px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: .82rem;
  font-weight: 800;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.reader-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.reader-controls {
  grid-template-columns: 1fr 140px 160px;
  margin-bottom: 14px;
}

.reader-buttons {
  margin-bottom: 10px;
}

.parts-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 16px 0;
}

.part-tab {
  padding: 9px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 900;
}

.part-tab.active {
  background: var(--primary);
  color: var(--primary-contrast);
}

.reader-text {
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg);
  padding: 20px;
}

.reader-text h3 {
  margin-bottom: 14px;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  line-height: 1.65;
  font-family: ui-serif, Georgia, Cambria, "Times New Roman", serif;
  font-size: 1.06rem;
}

.backup-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.backup-card {
  padding: 18px;
  box-shadow: none;
}

.backup-card p {
  color: var(--muted);
}

.warning-card {
  border-color: color-mix(in srgb, var(--danger) 55%, var(--border));
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  max-width: min(520px, calc(100% - 28px));
  padding: 14px 18px;
  border-radius: 999px;
  background: var(--primary);
  color: var(--primary-contrast);
  box-shadow: var(--shadow);
  z-index: 20;
  font-weight: 800;
}

.hidden {
  display: none !important;
}

@media (max-width: 860px) {
  .stats-grid,
  .content-grid,
  .chapter-list,
  .backup-grid,
  .reader-controls,
  .filters,
  .two-col {
    grid-template-columns: 1fr;
  }

  .tabs {
    border-radius: 24px;
  }

  .tab {
    flex-basis: calc(50% - 4px);
  }

  .panel-header.stackable,
  .panel-header {
    flex-direction: column;
  }
}

@media (max-width: 520px) {
  .app-shell {
    width: min(100% - 18px, 1180px);
    padding-top: 18px;
  }

  .app-header {
    align-items: flex-start;
  }

  .stat-card,
  .panel {
    padding: 16px;
  }

  .tab {
    flex-basis: 100%;
  }

  .button-row button {
    width: 100%;
  }
}
