:root {
  color-scheme: light;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-muted: #eef3fb;
  --text: #171923;
  --muted: #647084;
  --border: #dce2ea;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #b42318;
  --shadow: 0 18px 50px rgba(30, 41, 59, 0.1);
}

body.dark-mode {
  --bg: #0f172a;
  --surface: #111827;
  --surface-muted: #1e293b;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --border: #334155;
  --primary: #60a5fa;
  --primary-dark: #3b82f6;
  --danger: #fca5a5;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(37, 99, 235, 0.12), transparent 32rem),
    var(--bg);
}

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

.enhanced-select {
  cursor: pointer;
}

.enhanced-select:hover {
  border-color: var(--primary);
  background: #f8fbff;
}

.dark-mode .enhanced-select:hover {
  background: #172554;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1220px, calc(100% - 32px));
  margin: 0 auto;
  padding: 40px 0;
}

.app-header {
  margin-bottom: 24px;
}

.header-row {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-start;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--primary);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.15;
}

h1 {
  max-width: 760px;
  font-size: clamp(2.15rem, 5vw, 4.1rem);
}

h2 {
  font-size: 1.35rem;
}

h3 {
  font-size: 1.05rem;
}

.intro-text {
  max-width: 660px;
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 1.03rem;
  line-height: 1.7;
}

.app-grid {
  display: grid;
  grid-template-columns: 340px 320px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: var(--shadow);
}

.create-panel,
.list-panel,
.detail-panel {
  padding: 22px;
}

.panel-header {
  margin-bottom: 18px;
}

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

.field-group {
  display: grid;
  gap: 8px;
}

label {
  font-size: 0.92rem;
  font-weight: 700;
}

label span {
  color: var(--muted);
  font-weight: 500;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

input,
select {
  min-height: 46px;
  padding: 0 14px;
}

textarea {
  min-height: 168px;
  padding: 14px;
  resize: vertical;
  line-height: 1.6;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
}

.primary-button,
.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  gap: 10px;
  border: 0;
  border-radius: 8px;
  font-weight: 800;
}

.primary-button {
  color: #ffffff;
  background: var(--primary);
  transition: background 160ms ease, transform 160ms ease;
}

.primary-button:hover:not(:disabled) {
  background: var(--primary-dark);
  transform: translateY(-1px);
}

.secondary-button {
  padding: 0 16px;
  color: var(--primary);
  background: var(--surface-muted);
}

.small-button {
  min-height: 36px;
  padding: 0 12px;
  font-size: 0.86rem;
}

.compact {
  min-height: 40px;
  padding: 0 14px;
}

.primary-button:disabled,
.secondary-button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.spinner {
  display: none;
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.38);
  border-top-color: #ffffff;
  border-radius: 999px;
  animation: spin 800ms linear infinite;
}

.secondary-button .spinner {
  border-color: rgba(37, 99, 235, 0.22);
  border-top-color: var(--primary);
}

.dark-mode .secondary-button .spinner {
  border-color: rgba(96, 165, 250, 0.28);
  border-top-color: var(--primary);
}

.loading .spinner {
  display: inline-block;
}

.story-list {
  display: grid;
  max-height: 560px;
  gap: 10px;
  overflow: auto;
  padding-right: 4px;
}

.story-item {
  display: grid;
  gap: 7px;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  color: var(--text);
  background: var(--surface);
  text-align: left;
}

.story-item:hover,
.story-item.active {
  border-color: var(--primary);
  background: #f5f8ff;
}

.dark-mode .story-item:hover,
.dark-mode .story-item.active {
  background: #172554;
}

.story-title {
  font-weight: 800;
}

.story-meta {
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.45;
}

.detail-panel {
  min-height: 620px;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--border);
}

.chapter-list {
  display: grid;
  max-height: 670px;
  gap: 16px;
  overflow: auto;
  padding: 20px 4px 0 0;
}

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

.chapter-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
  color: var(--muted);
  font-size: 0.9rem;
  align-items: flex-start;
}

.chapter-header h3 {
  color: var(--text);
}

.chapter-content {
  color: #263142;
  line-height: 1.8;
  white-space: pre-wrap;
}

.dark-mode .chapter-content {
  color: #dbeafe;
}

.chapter-summary {
  margin: 0 0 14px;
  border-left: 3px solid var(--primary);
  padding: 10px 12px;
  color: #334155;
  background: #f5f8ff;
  line-height: 1.6;
}

.dark-mode .chapter-summary {
  color: #dbeafe;
  background: #172554;
}

.bookmark-button {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--muted);
  background: var(--surface-muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.bookmark-button.active {
  border-color: var(--primary);
  color: var(--primary);
  background: #e6edff;
}

.dark-mode .bookmark-button.active {
  background: #172554;
}

.reader-mode .create-panel,
.reader-mode .list-panel,
.reader-mode .app-header .intro-text {
  display: none;
}

.reader-mode .app-grid {
  display: block;
}

.reader-mode .detail-panel {
  max-width: 820px;
  min-height: auto;
  margin: 0 auto;
}

.reader-mode .chapter-list {
  max-height: none;
  overflow: visible;
}

.reader-mode .chapter-card {
  border-color: transparent;
  box-shadow: none;
}

.empty-state {
  margin: 0;
  color: var(--muted);
  line-height: 1.6;
}

.field-hint-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.field-hint {
  color: var(--muted);
  font-size: 0.86rem;
}

.idea-options {
  display: grid;
  gap: 10px;
}

.idea-option {
  display: grid;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface-muted);
}

.idea-option p {
  margin: 6px 0 0;
  color: var(--muted);
  line-height: 1.55;
  font-size: 0.92rem;
}

.error-message {
  margin: 0 0 18px;
  border: 1px solid #f3b7b0;
  border-radius: 8px;
  padding: 12px 14px;
  color: var(--danger);
  background: #fff4f2;
  font-size: 0.94rem;
  font-weight: 700;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 1080px) {
  .app-grid {
    grid-template-columns: 1fr 1fr;
  }

  .detail-panel {
    grid-column: 1 / -1;
  }
}

@media (max-width: 720px) {
  .app-shell {
    width: min(100% - 24px, 680px);
    padding: 28px 0;
  }

  .header-row,
  .detail-header {
    display: grid;
  }

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

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

  .chapter-list,
  .story-list {
    max-height: none;
  }
}

@media print {
  body {
    background: #ffffff;
    color: #111827;
  }

  .app-header,
  .create-panel,
  .list-panel,
  .detail-header button,
  .bookmark-button,
  .error-message {
    display: none;
  }

  .app-shell {
    width: 100%;
    padding: 0;
  }

  .app-grid {
    display: block;
  }

  .detail-panel,
  .chapter-card {
    border: 0;
    box-shadow: none;
    padding: 0;
  }

  .chapter-list {
    max-height: none;
    overflow: visible;
  }

  .chapter-card {
    break-inside: avoid;
    margin-bottom: 24px;
  }

  .chapter-content,
  .chapter-summary {
    color: #111827;
  }
}
