:root {
  --bg: #f3f4f6;
  --bg2: #ffffff;
  --bg3: #eef0f4;
  --bg-hover: #e8ebf0;
  --border: #e2e5ea;
  --text: #1c2230;
  --muted: #69707d;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --green: #16a34a;
  --yellow: #d97706;
  --red: #dc2626;
  --shadow: 0 10px 30px rgba(15, 23, 42, .14);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
  display: flex;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Sidebar ---------- */
#sidebar {
  width: 330px;
  min-width: 330px;
  display: flex;
  flex-direction: column;
  background: var(--bg2);
  border-right: 1px solid var(--border);
}

.brand { padding: 16px 16px 10px; }
.brand h1 { font-size: 18px; margin-bottom: 10px; }
#story-select {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}

#search {
  margin: 0 16px 12px;
  padding: 10px 12px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  outline: none;
}
#search::placeholder { color: var(--muted); }
#search:focus { border-color: var(--accent); background: #fff; }

#chapter-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.ch-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13.5px;
  line-height: 1.35;
}
.ch-item:hover { background: var(--bg-hover); }
.ch-item.active { background: var(--accent); color: #fff; }
.ch-item .num { min-width: 46px; color: var(--muted); font-variant-numeric: tabular-nums; }
.ch-item.active .num { color: rgba(255,255,255,.8); }
.ch-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot.summarized { background: var(--green); }
.dot.fetched { background: var(--yellow); }
.dot.pending { background: #c3c9d4; }
.dot.failed { background: var(--red); }

/* ---------- Worker panel ---------- */
#worker-panel {
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  max-height: 42vh;
  overflow-y: auto;
}
#worker-panel h2 {
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.badge {
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 700;
}
.badge.on { background: #dcfce7; color: var(--green); }
.badge.off { background: #fee2e2; color: var(--red); }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  margin-bottom: 8px;
}
.stats-grid div {
  background: var(--bg3);
  border-radius: 8px;
  padding: 6px 4px;
  text-align: center;
}
.stats-grid b { display: block; font-size: 15px; }
.stats-grid span { font-size: 10px; color: var(--muted); }

.progress {
  height: 6px;
  background: var(--bg3);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 8px;
}
#progress-bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--green));
  transition: width .4s ease;
}

.muted { color: var(--muted); }
.small { font-size: 11px; }

#worker-log {
  margin-top: 8px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font-family: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  font-size: 10.5px;
  line-height: 1.5;
  color: #475569;
  max-height: 160px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ---------- Backdrop (mobile) ---------- */
#backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .45);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
  z-index: 40;
}

/* ---------- Main ---------- */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#mobile-topbar { display: none; }

#chapter-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
}
#chapter-header h2 {
  flex: 1;
  font-size: 17px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#chapter-header button {
  padding: 8px 16px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
}
#chapter-header button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
#chapter-header button:disabled { opacity: .35; cursor: default; }

#tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 22px 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
}
.tab {
  padding: 9px 16px;
  background: transparent;
  color: var(--muted);
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: 13.5px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
#chapter-meta { margin-left: auto; padding-bottom: 8px; }

#content {
  flex: 1;
  overflow-y: auto;
  padding: 26px clamp(20px, 6vw, 90px) 60px;
  font-size: 16.5px;
  line-height: 1.85;
  background: var(--bg2);
}
#content p { margin-bottom: 1.05em; }
#content p.muted { font-size: 14px; }

/* ---------- Mobile (iPhone) ---------- */
@media (max-width: 760px) {
  #backdrop { display: block; }
  #backdrop.show { opacity: 1; pointer-events: auto; }

  #worker-panel { display: none; }

  #sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(84vw, 340px);
    min-width: 0;
    transform: translateX(-105%);
    transition: transform .28s ease;
    box-shadow: var(--shadow);
    z-index: 50;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #sidebar.open { transform: translateX(0); }
  .brand { padding-top: calc(14px + env(safe-area-inset-top)); }

  #mobile-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
    background: var(--bg2);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
  }
  #btn-menu {
    width: 42px;
    height: 42px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg3);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 19px;
    cursor: pointer;
  }
  #btn-menu:active { background: var(--bg-hover); }
  .topbar-title {
    font-size: 16.5px;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #chapter-header {
    gap: 8px;
    padding: 10px 12px;
    position: sticky;
    top: calc(62px + env(safe-area-inset-top));
    z-index: 15;
  }
  #chapter-header h2 {
    font-size: 15px;
    white-space: normal;
    line-height: 1.3;
    max-height: 2.6em;
    overflow: hidden;
  }
  #chapter-header button {
    padding: 12px 13px;
    font-size: 13.5px;
    min-width: 68px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #tabs { padding: 6px 12px 0; }
  .tab { padding: 11px 14px; font-size: 14.5px; }
  #chapter-meta { display: none; }

  .ch-item { padding: 13px 12px; font-size: 15.5px; }
  .ch-item .num { min-width: 52px; font-size: 14.5px; }
  .ch-item .dot { width: 10px; height: 10px; }

  #search { margin: 0 14px 10px; padding: 12px 14px; font-size: 16px; }
  #story-select { padding: 10px 12px; font-size: 15px; }
  .brand h1 { font-size: 17px; }

  #content {
    padding: 18px 18px calc(70px + env(safe-area-inset-bottom));
    font-size: 17px;
    line-height: 1.9;
  }
}
