:root {
  color: #18211f;
  background: #f5f1e9;
  --ink: #18211f;
  --muted: #66736e;
  --paper: #fffdf8;
  --paper-strong: #ffffff;
  --line: rgba(28, 45, 39, 0.12);
  --line-strong: rgba(18, 109, 102, 0.28);
  --teal: #0f766e;
  --teal-deep: #0b4f49;
  --amber: #c6613f;
  --gold: #c9a96a;
  --sage: #e9f1ec;
  --shadow: 0 18px 50px rgba(33, 43, 38, 0.08);
  font-family:
    Inter, 'Noto Sans TC', 'PingFang TC', 'Microsoft JhengHei', system-ui, sans-serif;
  font-synthesis: none;
  line-height: 1.5;
  text-rendering: optimizeLegibility;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: flex;
  min-height: 100vh;
  background:
    linear-gradient(90deg, rgba(15, 118, 110, 0.12), transparent 31rem),
    radial-gradient(circle at 78% 0%, rgba(201, 169, 106, 0.16), transparent 28rem),
    linear-gradient(180deg, #fbf8f1 0%, #eef6f1 100%);
  position: relative;
}

.app-shell::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.22;
  background-image:
    linear-gradient(rgba(28, 45, 39, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(28, 45, 39, 0.035) 1px, transparent 1px);
  background-size: 44px 44px;
}

.sidebar {
  width: 380px;
  flex: 0 0 380px;
  height: 100vh;
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px 22px;
  border-right: 1.5px solid var(--line);
  background: rgba(255, 253, 248, 0.9);
  backdrop-filter: saturate(160%) blur(18px);
  transition: margin-left 0.2s ease;
}

.sidebar.is-closed {
  margin-left: -380px;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
}

.brand-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: #ffffff;
  background: linear-gradient(145deg, var(--teal), var(--teal-deep));
  box-shadow: 0 12px 26px rgba(15, 118, 110, 0.22);
}

.brand h1 {
  margin: 0;
  color: var(--ink);
  font-size: 20px;
  line-height: 1.2;
  font-weight: 900;
  letter-spacing: 0;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 46px;
  padding: 0 13px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  color: #73807a;
  background: rgba(255, 255, 255, 0.74);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
}

.search-box:focus-within {
  border-color: var(--line-strong);
  box-shadow: 0 0 0 4px rgba(15, 118, 110, 0.08);
}

.search-box input {
  width: 100%;
  border: 0;
  outline: 0;
  color: #17211f;
  background: transparent;
}

.episode-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px 0 7px;
}

.episode-row button {
  flex: 0 0 auto;
  height: 36px;
  padding: 0 14px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  color: #53615c;
  background: rgba(255, 255, 255, 0.7);
  font-weight: 800;
}

.episode-row button.active {
  color: #ffffff;
  border-color: var(--teal);
  background: var(--teal);
  box-shadow: 0 10px 20px rgba(15, 118, 110, 0.18);
}

.script-list {
  display: flex;
  flex: 1;
  min-height: 0;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding-right: 2px;
}

.script-item {
  display: grid;
  gap: 6px;
  width: 100%;
  padding: 15px 15px 15px 16px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  text-align: left;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.78);
  box-shadow: 0 10px 28px rgba(33, 43, 38, 0.04);
  transition:
    transform 0.18s ease,
    border-color 0.18s ease,
    box-shadow 0.18s ease,
    background 0.18s ease;
}

.script-item:hover {
  transform: translateY(-1px);
  border-color: rgba(15, 118, 110, 0.24);
  background: var(--paper-strong);
  box-shadow: 0 16px 34px rgba(33, 43, 38, 0.08);
}

.script-item.selected {
  border-color: rgba(15, 118, 110, 0.62);
  background:
    linear-gradient(90deg, rgba(15, 118, 110, 0.08), transparent 42%),
    var(--paper-strong);
  box-shadow:
    inset 4px 0 0 var(--teal),
    0 18px 40px rgba(15, 118, 110, 0.12);
}

.script-item strong {
  font-size: 15px;
  line-height: 1.35;
  letter-spacing: 0;
}

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

.script-meta {
  color: var(--amber) !important;
  font-weight: 700;
}

.reader {
  flex: 1;
  min-width: 0;
}

.topbar {
  position: sticky;
  z-index: 3;
  top: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  height: 78px;
  padding: 0 30px;
  border-bottom: 1.5px solid var(--line);
  background: rgba(255, 253, 248, 0.78);
  backdrop-filter: saturate(180%) blur(20px);
}

.current-script {
  display: grid;
  gap: 2px;
  min-width: 0;
}

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

.topbar strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button,
.sync-button,
.stepper button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.82);
  transition:
    transform 0.16s ease,
    border-color 0.16s ease,
    box-shadow 0.16s ease,
    color 0.16s ease;
}

.icon-button:hover,
.sync-button:hover,
.stepper button:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: rgba(15, 118, 110, 0.36);
  color: var(--teal-deep);
  box-shadow: 0 12px 24px rgba(33, 43, 38, 0.08);
}

.icon-button {
  width: 40px;
  height: 40px;
}

.sync-button {
  gap: 8px;
  height: 40px;
  margin-left: auto;
  padding: 0 14px;
}

.stepper {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.stepper button {
  width: 36px;
  height: 36px;
}

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

.stepper span {
  min-width: 56px;
  text-align: center;
  color: #53615c;
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stepper + .sync-button {
  margin-left: 0;
}

.reader-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 350px;
  gap: 22px;
  padding: 30px;
}

.script-detail,
.side-panel {
  min-width: 0;
}

.title-area {
  position: relative;
  overflow: hidden;
  padding: 32px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.96), rgba(255, 251, 243, 0.92)),
    var(--paper-strong);
  box-shadow: var(--shadow);
}

.title-area::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 7px;
  height: 100%;
  background: linear-gradient(180deg, var(--teal), var(--gold), var(--amber));
}

.title-area::after {
  content: '';
  position: absolute;
  right: 24px;
  top: 22px;
  width: 88px;
  height: 88px;
  border: 1px solid rgba(201, 169, 106, 0.28);
  border-radius: 999px;
  opacity: 0.55;
}

.status-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.status-row span {
  min-height: 28px;
  padding: 6px 10px;
  border-radius: 7px;
  color: #596762;
  background: rgba(233, 241, 236, 0.92);
  font-size: 13px;
  font-weight: 700;
}

.status-row .todo-chip {
  color: #955019;
  background: #fff0df;
}

.status-row .done-chip {
  color: #126d66;
  background: #e2f4ef;
}

.title-area h2 {
  margin: 0;
  max-width: 900px;
  color: var(--ink);
  font-size: clamp(32px, 4.3vw, 56px);
  line-height: 1.12;
  letter-spacing: 0;
  font-weight: 950;
}

.title-area p {
  margin: 16px 0 0;
  max-width: 920px;
  color: #4f5f59;
  font-size: 16px;
  line-height: 1.85;
  white-space: pre-wrap;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 16px 0 24px;
}

.info-pill {
  display: grid;
  grid-template-columns: auto 54px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-height: 58px;
  padding: 11px 13px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.82);
  box-shadow: 0 10px 26px rgba(33, 43, 38, 0.045);
}

.info-pill svg {
  color: var(--teal);
}

.info-pill span {
  color: #71807a;
  font-size: 12px;
}

.info-pill strong {
  overflow: hidden;
  color: var(--ink);
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: pre-wrap;
}

.segments {
  display: grid;
  gap: 18px;
}

.segment,
.panel-card,
.text-block,
.ai-panel {
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.86);
  box-shadow: 0 14px 38px rgba(33, 43, 38, 0.055);
}

.segment {
  overflow: hidden;
  position: relative;
}

.segment-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1.5px solid var(--line);
  background:
    linear-gradient(90deg, rgba(15, 118, 110, 0.08), transparent 52%),
    rgba(255, 253, 248, 0.9);
}

.segment-head span {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 7px;
  color: #ffffff;
  background: var(--teal);
  font-size: 12px;
  font-weight: 800;
  box-shadow: 0 8px 18px rgba(15, 118, 110, 0.22);
}

.segment-head h3,
.panel-card h3,
.text-block h3 {
  margin: 0;
  font-size: 16px;
  color: var(--ink);
}

.segment pre {
  margin: 0;
  padding: 22px 24px;
  color: #22302c;
  font-family: inherit;
  font-size: 17.5px;
  line-height: 2;
  white-space: pre-wrap;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.72), rgba(255, 253, 248, 0.58));
}

.note-editor {
  display: grid;
  gap: 10px;
  padding: 14px 18px 18px;
  border-top: 1.5px solid var(--line);
  background: rgba(248, 250, 247, 0.88);
}

.note-editor span {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #596762;
  font-size: 14px;
  font-weight: 800;
}

.note-editor textarea {
  width: 100%;
  min-height: 86px;
  resize: vertical;
  border: 1.5px solid rgba(28, 45, 39, 0.14);
  border-radius: 8px;
  padding: 12px;
  outline: none;
  color: var(--ink);
  background: #fffefa;
  line-height: 1.7;
}

.note-editor textarea:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 4px rgba(15, 118, 110, 0.11);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.ai-panel {
  display: grid;
  gap: 14px;
  padding: 18px;
  background:
    linear-gradient(145deg, rgba(232, 246, 242, 0.78), rgba(255, 253, 248, 0.9)),
    #ffffff;
}

.ai-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.ai-panel-head > div {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ai-panel-head svg {
  color: var(--teal);
}

.ai-panel-head h3 {
  margin: 0;
  color: var(--ink);
  font-size: 16px;
}

.ai-panel-head span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.quick-prompts {
  display: grid;
  gap: 8px;
}

.quick-prompts button {
  width: 100%;
  min-height: 36px;
  padding: 8px 10px;
  border: 1.5px solid rgba(15, 118, 110, 0.18);
  border-radius: 8px;
  color: var(--teal-deep);
  text-align: left;
  background: rgba(255, 255, 255, 0.78);
  font-size: 13px;
  font-weight: 800;
}

.quick-prompts button:hover:not(:disabled) {
  border-color: rgba(15, 118, 110, 0.36);
  background: #ffffff;
}

.quick-prompts button:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.chat-window {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 360px;
  overflow-y: auto;
  padding: 12px;
  border: 1.5px solid rgba(28, 45, 39, 0.1);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.58);
}

.chat-bubble {
  width: fit-content;
  max-width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  color: #25332f;
  font-size: 14px;
  line-height: 1.7;
  white-space: pre-wrap;
}

.chat-bubble.assistant {
  border: 1px solid rgba(15, 118, 110, 0.16);
  background: #ffffff;
}

.chat-bubble.user {
  align-self: flex-end;
  color: #ffffff;
  background: var(--teal);
}

.chat-bubble.loading {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--teal-deep);
  font-weight: 800;
}

.chat-bubble.loading svg,
.chat-input button svg {
  animation: spin 0.9s linear infinite;
}

.chat-input button svg:not(.lucide-loader-circle) {
  animation: none;
}

.chat-error {
  padding: 10px 12px;
  border: 1.5px solid rgba(198, 97, 63, 0.24);
  border-radius: 8px;
  color: #8c3c24;
  background: #fff2eb;
  font-size: 13px;
  font-weight: 800;
}

.chat-input {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 42px;
  gap: 8px;
  align-items: stretch;
}

.chat-input textarea {
  width: 100%;
  resize: vertical;
  min-height: 72px;
  border: 1.5px solid rgba(28, 45, 39, 0.14);
  border-radius: 8px;
  padding: 11px 12px;
  outline: none;
  color: var(--ink);
  background: #fffefa;
  line-height: 1.6;
}

.chat-input textarea:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 4px rgba(15, 118, 110, 0.1);
}

.chat-input button {
  display: grid;
  place-items: center;
  border: 1.5px solid var(--teal);
  border-radius: 8px;
  color: #ffffff;
  background: var(--teal);
}

.chat-input button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

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

.panel-card,
.text-block {
  padding: 19px;
}

.panel-card h3 {
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-card h3 svg,
.text-block h3 svg {
  color: var(--teal);
}

.panel-card p,
.text-block p {
  margin: 12px 0 0;
  color: #43524d;
  font-size: 14px;
  line-height: 1.75;
  white-space: pre-wrap;
}

.save-hint {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px;
  border: 1.5px solid rgba(15, 118, 110, 0.25);
  border-radius: 8px;
  color: var(--teal-deep);
  background: linear-gradient(135deg, #e8f6f2, #fff8eb);
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 12px 30px rgba(15, 118, 110, 0.08);
}

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

  .side-panel {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

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

@media (max-width: 760px) {
  .app-shell {
    display: block;
  }

  .sidebar {
    position: relative;
    width: 100%;
    height: auto;
    max-height: 60vh;
    padding: 20px 22px 16px;
    border-right: 0;
    border-bottom: 1px solid #dce4df;
  }

  .sidebar.is-closed {
    display: none;
    margin-left: 0;
  }

  .topbar {
    height: auto;
    min-height: 68px;
    padding: 12px;
  }

  .sync-button {
    width: 42px;
    padding: 0;
    font-size: 0;
  }

  .stepper {
    margin-left: auto;
  }

  .stepper span {
    display: none;
  }

  .reader-grid {
    padding: 14px;
  }

  .info-grid,
  .side-panel {
    grid-template-columns: 1fr;
  }

  .title-area {
    padding: 22px 18px 18px;
  }

  .title-area h2 {
    font-size: clamp(28px, 8.2vw, 36px);
    line-height: 1.16;
  }

  .title-area p {
    font-size: 15px;
    line-height: 1.75;
  }

  .status-row span {
    font-size: 12px;
  }

  .title-area::after {
    display: none;
  }

  .segment pre {
    padding: 16px;
    font-size: 16px;
  }
}

/* Chat assistant refinements */
.chat-window {
  min-height: 240px;
  max-height: 430px;
}

.chat-bubble {
  max-width: 88%;
}

.chat-reset {
  justify-self: start;
  border: 1px solid rgba(15, 118, 110, 0.18);
  border-radius: 8px;
  padding: 7px 10px;
  color: var(--teal-deep);
  background: rgba(255, 255, 255, 0.76);
  font-size: 12px;
  font-weight: 800;
}

.chat-reset:hover {
  border-color: rgba(15, 118, 110, 0.34);
  background: #ffffff;
}

/* Script edit mode */
.sync-button.is-active { color: #ffffff; background: var(--teal); border-color: var(--teal); }
.edit-panel { display: grid; gap: 12px; margin-top: 14px; padding: 14px; border: 1.5px solid rgba(15,118,110,.18); border-radius: 8px; background: rgba(244,253,254,.78); }
.edit-panel label, .panel-card .side-edit { display: grid; gap: 7px; color: #43524d; font-size: 13px; font-weight: 800; }
.edit-panel input, .edit-panel textarea, .side-edit { width: 100%; border: 1.5px solid rgba(28,45,39,.14); border-radius: 8px; padding: 10px 12px; outline: none; color: var(--ink); background: #fffefa; font: inherit; line-height: 1.65; }
.edit-panel input:focus, .edit-panel textarea:focus, .side-edit:focus { border-color: var(--teal); box-shadow: 0 0 0 4px rgba(15,118,110,.1); }
.edit-grid { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 12px; }
.edit-actions { display: flex; align-items: center; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }
.edit-actions span { margin-right: auto; color: var(--muted); font-size: 13px; font-weight: 800; }
.edit-actions strong { color: #9a3e22; font-size: 13px; }
.edit-actions button { border: 1.5px solid var(--teal); border-radius: 8px; padding: 9px 13px; color: #ffffff; background: var(--teal); font-weight: 900; }
.script-editor textarea { min-height: 520px; resize: vertical; }
.side-edit { margin-top: 12px; resize: vertical; }
@media (max-width: 760px) { .edit-grid { grid-template-columns: 1fr; } }

/* Edit layout polish */
.title-editor { width: 100%; max-width: 920px; }
.script-textarea { min-height: 720px; white-space: pre-wrap; font-size: 18px; }
.sticky-edit-actions { position: sticky; bottom: 12px; z-index: 5; padding: 10px; border: 1px solid rgba(15,118,110,.14); border-radius: 8px; background: rgba(255,254,250,.94); backdrop-filter: blur(8px); }
.edit-panel input, .edit-panel textarea, .side-edit { box-sizing: border-box; }
.script-editor { grid-column: 1 / -1; }
