:root {
  --bg: #0f1115;
  --bg-elev: #161922;
  --bg-soft: #1c2030;
  --fg: #e8eaf0;
  --fg-dim: #a1a7b8;
  --muted: #6c7384;
  --accent: #ffd166;
  --accent-soft: rgba(255, 209, 102, 0.18);
  --line: #262b39;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --radius: 12px;
  --max-w: 760px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  --font-read: "Iowan Old Style", "Charter", "Georgia", "Cambria", serif;
  --font-mono: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #fafaf7;
    --bg-elev: #ffffff;
    --bg-soft: #f1f1ec;
    --fg: #1a1d24;
    --fg-dim: #4b5161;
    --muted: #6c7384;
    --accent: #c47a00;
    --accent-soft: rgba(196, 122, 0, 0.18);
    --line: #e6e6df;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--line);
}

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

.logo {
  display: inline-grid;
  place-items: center;
  width: 36px; height: 36px;
  border-radius: 9px;
  background: var(--accent);
  color: #1a1d24;
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.5px;
}

.brand h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.brand p {
  margin: 0;
  font-size: 12px;
  color: var(--fg-dim);
}

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

.sep {
  width: 1px;
  height: 22px;
  background: var(--line);
  margin: 0 4px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--fg);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: transform 80ms ease, background 120ms ease, border-color 120ms ease;
}
.btn:hover:not(:disabled) { background: var(--bg-soft); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1d24;
}
.btn.primary:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 88%, white);
}

.btn svg { display: block; }

.select {
  appearance: none;
  -webkit-appearance: none;
  padding: 7px 28px 7px 10px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg-elev) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24'><path fill='%23a1a7b8' d='M7 10l5 5 5-5z'/></svg>") no-repeat right 8px center;
  color: var(--fg);
  font-size: 13px;
  max-width: 220px;
}

.rate {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  font-size: 12px;
  color: var(--fg-dim);
}
.rate input {
  width: 110px;
  accent-color: var(--accent);
}
.rate output {
  font-variant-numeric: tabular-nums;
  color: var(--fg);
  min-width: 34px;
  text-align: right;
}

/* ---------- reader ---------- */
.reader {
  flex: 1 1 auto;
  width: 100%;
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 48px 24px 120px;
  font-family: var(--font-read);
  font-size: 19px;
  line-height: 1.75;
  color: var(--fg);
  outline: none;
}

.reader h1, .reader h2, .reader h3, .reader h4, .reader h5, .reader h6 {
  font-family: var(--font-ui);
  letter-spacing: -0.015em;
  line-height: 1.25;
  margin: 1.6em 0 0.5em;
}
.reader h1 { font-size: 2.1em; margin-top: 0; }
.reader h2 { font-size: 1.55em; }
.reader h3 { font-size: 1.25em; }
.reader p, .reader li { margin: 0 0 0.9em; }
.reader a { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
.reader blockquote {
  margin: 1em 0;
  padding: 0.4em 1em;
  border-left: 3px solid var(--accent);
  color: var(--fg-dim);
  background: var(--bg-elev);
  border-radius: 6px;
}
.reader pre, .reader code {
  font-family: var(--font-mono);
}
.reader code {
  font-size: 0.88em;
  background: var(--bg-elev);
  padding: 1px 6px;
  border-radius: 5px;
  border: 1px solid var(--line);
}
.reader pre {
  font-size: 0.88em;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
  overflow-x: auto;
  line-height: 1.55;
}
.reader pre code {
  background: transparent;
  border: 0;
  padding: 0;
}
.reader table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
  font-size: 0.95em;
}
.reader th, .reader td {
  border: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
}
.reader hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }
.reader img { max-width: 100%; border-radius: 8px; }

/* sentence wrapping */
.sent {
  border-radius: 4px;
  padding: 1px 0;
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease;
}
.sent:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.sent.active {
  background: var(--accent-soft);
  box-shadow: 0 0 0 4px var(--accent-soft);
}
.sent.spoken { color: var(--fg-dim); }

/* welcome state */
.welcome {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 28px;
  text-align: center;
  color: var(--fg-dim);
  background: var(--bg-elev);
  font-family: var(--font-ui);
  font-size: 14px;
}
.welcome strong { color: var(--fg); font-weight: 600; }
.welcome kbd {
  display: inline-block;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  border-radius: 4px;
  padding: 0 6px;
  font-size: 12px;
  font-family: var(--font-mono);
}

/* ---------- editor drawer ---------- */
.editor {
  position: fixed;
  inset: auto 0 0 0;
  height: 50dvh;
  background: var(--bg-elev);
  border-top: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  z-index: 20;
}
.editor[hidden] { display: none; }
.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  color: var(--fg-dim);
}
.editor textarea {
  flex: 1;
  width: 100%;
  border: 0;
  outline: 0;
  resize: none;
  padding: 16px 20px;
  background: transparent;
  color: var(--fg);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
}

/* ---------- drop overlay ---------- */
.drop {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 75%, transparent);
  border: 3px dashed var(--accent);
  display: grid;
  place-items: center;
  font-size: 20px;
  color: var(--accent);
  z-index: 50;
  pointer-events: none;
}
.drop[hidden] { display: none; }

/* ---------- url dialog ---------- */
.dialog {
  width: min(520px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  background: var(--bg-elev);
  color: var(--fg);
  box-shadow: var(--shadow);
}
.dialog::backdrop {
  background: color-mix(in srgb, #000 55%, transparent);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.dialog form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px 20px 16px;
}
.dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.dialog header strong { font-size: 15px; }
.dialog .hint {
  margin: 0;
  color: var(--fg-dim);
  font-size: 13px;
}
.dialog .hint code {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12px;
}
.dialog .preview {
  font-size: 12px;
  color: var(--accent);
  font-family: var(--font-mono);
  word-break: break-all;
}
.dialog input[type="url"] {
  width: 100%;
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  font-size: 14px;
  font-family: var(--font-mono);
}
.dialog input[type="url"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

/* ---------- footer ---------- */
.foot {
  border-top: 1px solid var(--line);
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--fg-dim);
}
.foot .muted { color: var(--muted); }

/* ---------- responsive ---------- */
@media (max-width: 760px) {
  .topbar { gap: 10px; padding: 10px 14px; }
  .brand p { display: none; }
  .controls .sep { display: none; }
  .rate input { width: 80px; }
  .reader { padding: 28px 18px 120px; font-size: 18px; }
  .editor { height: 60dvh; }
}

@media (max-width: 520px) {
  .controls .btn span,
  .select option:nth-child(n+10) { display: none; }
  .select { max-width: 130px; font-size: 12px; }
  .controls { gap: 6px; }
}
