:root {
  --bg: #14161c;
  --surface: #1d2029;
  --surface-2: #262a35;
  --text: #e9ebf1;
  --muted: #9aa1b2;
  --accent: #7c9cff;
  --accent-text: #0d1020;
  --highlight: rgba(124, 156, 255, 0.18);
  --border: #323746;
  --danger: #ff7c7c;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7fb;
    --surface: #ffffff;
    --surface-2: #eceff6;
    --text: #1a1d26;
    --muted: #5d6475;
    --accent: #3d63e0;
    --accent-text: #ffffff;
    --highlight: rgba(61, 99, 224, 0.13);
    --border: #d9dde8;
    --danger: #c73a3a;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(150px + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.brand { display: flex; gap: 8px; align-items: center; font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 8px; }
.topbar nav a.active { color: var(--text); background: var(--surface-2); }

main { max-width: 820px; margin: 0 auto; padding: 16px; }
h2 { font-size: 18px; margin: 20px 0 10px; }

.banner { background: var(--highlight); border: 1px solid var(--accent); padding: 12px; border-radius: 10px; }

.add-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.add-btn {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 14px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-align: left;
}
.add-btn:hover { border-color: var(--accent); }
.add-btn .ico { font-size: 24px; }
.add-btn small { color: var(--muted); font-weight: 400; }
.hint { color: var(--muted); font-size: 14px; }
kbd { background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; font-size: 13px; }
@media (pointer: coarse) { .desktop-only { display: none; } }
@media (pointer: fine) { .mobile-only { display: none; } }

.doc-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.doc-item {
  display: flex; align-items: center; gap: 10px; padding: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.doc-item .doc-main { flex: 1; min-width: 0; cursor: pointer; }
.doc-item .doc-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-item .doc-sub { font-size: 13px; color: var(--muted); }
.doc-item .bar { height: 4px; background: var(--surface-2); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.doc-item .bar > i { display: block; height: 100%; background: var(--accent); }
.icon-btn { background: none; border: none; color: var(--muted); font-size: 20px; cursor: pointer; padding: 6px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.row { display: flex; gap: 8px; align-items: center; margin: 10px 0; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.btn {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  padding: 9px 14px; border-radius: 10px; font: inherit; font-size: 15px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 10px; width: 100%;
}
.row input, .row select { flex: 1; min-width: 0; }
.title-input { font-size: 20px; font-weight: 600; }
#ed-text { min-height: 55vh; resize: vertical; line-height: 1.6; }

.field { display: block; margin: 18px 0; }
.field > span { display: block; font-weight: 600; margin-bottom: 6px; }
.check { display: flex; gap: 10px; align-items: flex-start; margin: 18px 0; }
.check input { width: auto; margin-top: 4px; }
input[type=range] { padding: 0; accent-color: var(--accent); }

.reader-head h2 { font-size: 22px; margin-top: 4px; }
.reader-text { font-size: 18px; line-height: 1.75; }
.chunk { padding: 6px 10px; margin: 0 -10px; border-radius: 8px; white-space: pre-wrap; cursor: pointer; transition: background .2s; }
.chunk:hover { background: var(--surface); }
.chunk.current { background: var(--highlight); }
.chunk.pause { color: var(--muted); font-size: 14px; font-style: italic; }

.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-body {
  position: absolute; z-index: 5; top: calc(100% + 6px); left: 0; min-width: 270px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 6px;
  box-shadow: 0 10px 30px rgba(0,0,0,.3);
}
.menu-body button {
  display: block; width: 100%; text-align: left; padding: 10px 12px; background: none; border: none;
  color: var(--text); font: inherit; border-radius: 8px; cursor: pointer;
}
.menu-body button:hover { background: var(--surface-2); }

.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 8px 16px calc(10px + env(safe-area-inset-bottom));
}
.player > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.p-info { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; }
.p-title { color: var(--text); font-weight: 600; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-status { color: var(--muted); white-space: nowrap; }
.p-status.error { color: var(--danger); white-space: normal; }
#p-progress { display: block; width: 100%; margin-top: 6px; margin-bottom: 6px; }
.p-controls { display: flex; align-items: center; justify-content: center; gap: 6px; }
.p-controls button {
  background: none; border: none; color: var(--text); font: inherit; font-size: 18px; cursor: pointer;
  min-width: 48px; height: 48px; border-radius: 24px;
}
.p-controls button:hover { background: var(--surface-2); }
.p-controls .p-play { background: var(--accent); color: var(--accent-text); font-size: 22px; width: 58px; height: 58px; border-radius: 29px; }
.p-controls .p-play:hover { background: var(--accent); }
#p-rate { width: auto; flex: 0 0 auto; padding: 6px 8px; margin-left: 6px; }

.busy { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); display: grid; place-items: center; padding: 20px; }
.busy-box { background: var(--surface); border-radius: 16px; padding: 24px; text-align: center; max-width: 360px; width: 100%; }
.spinner { width: 36px; height: 36px; border: 4px solid var(--surface-2); border-top-color: var(--accent); border-radius: 50%; margin: 0 auto 12px; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(170px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; max-width: min(90vw, 560px);
  box-shadow: 0 8px 24px rgba(0,0,0,.3); font-size: 15px;
}
.toast.error { background: var(--danger); color: #fff; }
