/* --- Design tokens --- */
:root {
  --bg: #0b0c10;
  --surface: #111217;
  --elevated: #171923;
  --text: #e8eaed;
  --muted: #9aa0a6;
  --border: #2a2d36;
  --primary: #4f8cff;
  --primary-600: #3a74ee;
  --success: #22c55e;
  --warning: #f59e0b;
  --error: #ef4444;
  --accent: #7c3aed;
  --shadow: 0 10px 30px rgba(0,0,0,.35);
  --radius: 14px;
  --radius-sm: 10px;
  --radius-xs: 8px;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f8fa;
    --surface: #ffffff;
    --elevated: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --border: #e5e7eb;
    --primary: #2563eb;
    --primary-600: #1d4ed8;
    --shadow: 0 10px 25px rgba(0,0,0,.08);
  }
}

/* --- Base / layout --- */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 100% -20%, rgba(79,140,255,.12), transparent 50%),
    radial-gradient(1000px 500px at -10% 110%, rgba(124,58,237,.14), transparent 50%),
    var(--bg);
  padding: 28px clamp(16px, 2.2vw, 32px);
}
body.modal-open { overflow: hidden; }

h1, h2, h3 { margin: 0 0 10px; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: clamp(20px, 2.6vw, 28px); }
p { color: var(--muted); }

.container {
  max-width: 1100px;
  margin: 0 auto;
}

/* Responsive two-column layout */
.layout { display: grid; grid-template-columns: 1.15fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; } }

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

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  border-radius: var(--radius);
  padding: clamp(16px, 2vw, 22px);
}

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

/* --- Inputs & buttons --- */
textarea, input[type="url"], input[type="text"], input[type="checkbox"], select, button { font: inherit; }

textarea, input[type="url"], input[type="text"], select {
  width: 100%;
  color: var(--text);
  background: var(--elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  padding: 12px 14px;
  outline: none;
  transition: border-color .2s ease, box-shadow .2s ease, background .2s ease;
}
/* Input with inline clear (×) icon */
.input-with-clear { position: relative; }
.input-with-clear input[type="url"],
.input-with-clear input[type="text"] { padding-right: 32px; }
.input-with-clear .input-clear {
  position: absolute;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--muted);
  line-height: 1;
  font-size: 14px;
  padding: 0;
  cursor: pointer;
}
.input-with-clear .input-clear:hover { color: var(--text); }
.input-with-clear .input-clear:active { transform: translateY(-50%) scale(.96); }
textarea { min-height: 120px; resize: vertical; }
textarea:focus, input[type="url"]:focus, input[type="text"]:focus, select:focus {
  border-color: color-mix(in oklab, var(--primary) 70%, white);
  box-shadow: 0 0 0 6px color-mix(in oklab, var(--primary) 20%, transparent);
}
textarea.is-disabled {
  opacity: .6;
  filter: grayscale(.1);
}

.help { font-size: 12px; color: var(--muted); }

button {
  appearance: none;
  border: none;
  border-radius: 999px;
  padding: 10px 14px;
  font-weight: 600;
  color: white;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  box-shadow: 0 6px 18px rgba(79,140,255,.25);
  transition: transform .06s ease, filter .2s ease, box-shadow .2s ease, opacity .2s ease;
  cursor: pointer;
}
button:hover { filter: brightness(1.05); }
button:active { transform: translateY(1px); }

.btn-secondary { color: var(--text); background: linear-gradient(180deg, #2a2f3a, #21242d); box-shadow: 0 6px 16px rgba(0,0,0,.25); }
.btn-sm { padding: 6px 10px; font-size: 12px; border-radius: 10px; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-link { background: none; color: var(--muted); padding: 0; border-radius: 0; box-shadow: none; text-decoration: underline; text-underline-offset: 2px; }
.btn-link:hover { color: var(--text); text-decoration: underline; }
.btn-link.btn-sm { font-size: 12px; }

label { font-weight: 600; color: var(--text); display: inline-block; margin-bottom: 8px; }
.checkbox { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.checkbox input { width: 16px; height: 16px; accent-color: var(--primary); }

/* --- Progress --- */
#progressBar { width: 100%; height: 10px; background: #1a1c23; border-radius: 999px; margin: 12px 0 6px; overflow: hidden; display: none; position: relative; border: 1px solid var(--border); }
#progressBarFill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); width: 0%; transition: width .35s cubic-bezier(.22,1,.36,1); }
#progressText { font-size: 12px; color: var(--muted); display: none; text-align: left; }

/* --- Segments / word highlighting --- */
.seg { padding: 2px 3px; border-radius: 6px; transition: background-color .12s ease, color .12s ease; }
.seg.active { background: color-mix(in oklab, #fbbf24 28%, transparent); }
.seg.done { color: color-mix(in oklab, var(--success) 85%, var(--text)); }
.word { padding: 0 2px; border-radius: 4px; transition: background-color .12s ease, color .12s ease; cursor: pointer; }
.word:hover { background: color-mix(in oklab, var(--primary) 22%, transparent); }
.word.active { background: color-mix(in oklab, #fbbf24 38%, transparent); font-weight: 700; }
.word.done { color: color-mix(in oklab, var(--success) 85%, var(--text)); }

/* --- Audio & log --- */
#audio { display: none; width: 100%; margin-top: 10px; background: var(--elevated); border: 1px solid var(--border); border-radius: var(--radius-xs); }
pre#log { background: var(--elevated); border: 1px dashed var(--border); color: var(--muted); padding: 12px; border-radius: var(--radius-xs); overflow: auto; margin: 0; }
#log { display: block; }
#logToggle { color: var(--muted); font-size: 12px; cursor: pointer; }
#logToggle:hover { color: var(--text); }
/* Hide floating controls when any modal is open */
body.modal-open #themeToggle,
body.modal-open #logToggle,
body.modal-open #scrollToWord { display: none !important; }

/* --- Reconnection banner --- */
#reconnectionStatus { display: none; border-radius: var(--radius-xs); padding: 10px 12px; margin: 12px 0; border: 1px solid color-mix(in oklab, var(--warning) 40%, var(--border)); background: color-mix(in oklab, var(--warning) 12%, transparent); color: #fcd34d; text-align: center; font-size: 13px; }
#reconnectionStatus.error { background: color-mix(in oklab, var(--error) 16%, transparent); border-color: color-mix(in oklab, var(--error) 45%, var(--border)); color: #fecaca; }
#reconnectionStatus.success { background: color-mix(in oklab, var(--success) 16%, transparent); border-color: color-mix(in oklab, var(--success) 45%, var(--border)); color: #bbf7d0; }

/* --- URL preview modal --- */
#urlPreviewModal { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(4px); z-index: 1000; padding: 28px; }
#urlPreviewModal > div { position: relative; width: min(1200px, 95vw); height: min(85vh, 900px); background: var(--surface) !important; border: 1px solid var(--border) !important; border-radius: var(--radius) !important; box-shadow: var(--shadow) !important; margin: 0 auto; display: grid; grid-template-rows: auto 1fr auto; gap: 12px; padding: 16px !important; overflow: hidden; }
#urlPreviewModal h3 { margin: 0; font-size: 18px; }
#previewFrame { width: 100%; height: 100%; border: none; border-radius: var(--radius-xs); background: var(--elevated); }
#navigationHint { position: absolute; top: 16px; left: 16px; background: rgba(0,0,0,.7); color: #fff; padding: 8px 10px; border-radius: 8px; font-size: 12px; pointer-events: none; opacity: .95; transition: opacity .3s ease; }
#currentPageUrl { width: 100%; background: var(--elevated); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; font-size: 12px; }
#navigationStatus { color: var(--muted); font-size: 12px; }
/* Sticky, safe-area aware footer */
#urlPreviewFooter { position: sticky; bottom: 0; background: var(--surface); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
/* Compact option row overrides */
.methods .option-label { display: inline-flex; align-items: center; gap: 6px; border: none !important; background: transparent !important; padding: 0 !important; }
.methods .option-label .desc { display: inline; }
.methods .tip { background: transparent; border: 1px solid var(--border); border-radius: 999px; color: var(--muted); width: 24px; height: 24px; display: inline-flex; align-items: center; justify-content: center; }

/* Mobile optimizations for URL preview */
@media (max-width: 640px) {
  body { padding: 16px; }
  #urlPreviewModal { padding: 0; }
  #urlPreviewModal > div { position: absolute; inset: 0 !important; width: 100% !important; height: 100% !important; left: 0 !important; top: 0 !important; transform: none !important; border-radius: 0 !important; grid-template-rows: auto 1fr auto; padding: 12px !important; box-sizing: border-box; }
  #urlPreviewHeader { flex-direction: column; align-items: stretch; gap: 8px; }
  #urlPreviewHeader > button#closePreview { position: absolute; top: 8px; right: 8px; margin: 0; }
  #urlPreviewHeader h3 { font-size: 16px !important; }
  #urlPreviewHeader .url-row { gap: 8px; flex-wrap: wrap; }
  #urlPreviewHeader .url-row > span { flex: 1 0 100%; font-size: 12px !important; }
  #currentPageUrl { font-size: 14px; padding: 10px 12px; }
  #helpGetUrl, #refreshFrame { padding: 8px 10px !important; font-size: 14px !important; }
  #previewFrame { height: 100%; }
  #navigationHint { font-size: 11px; top: 8px; left: 8px; padding: 6px 8px; }
  #navigationStatus { font-size: 12px; }
  #urlPreviewFooter { flex-direction: column; align-items: stretch; gap: 10px; }
  #urlPreviewActions { display: grid !important; grid-template-columns: 1fr; gap: 8px; }
  #cancelFetch, #confirmFetch { width: 100%; padding: 14px 16px !important; font-size: 15px !important; }
  .methods { gap: 8px; }
  .methods .option-label .desc { display: none; }
  .methods .tip { width: 28px; height: 28px; }
}

/* Tooltip */
#simpleTooltip { position: fixed; z-index: 1300; background: rgba(0,0,0,.9); color: #fff; padding: 8px 10px; border-radius: 8px; font-size: 12px; max-width: 260px; box-shadow: 0 8px 20px rgba(0,0,0,.3); }

/* Log modal */
#logModal { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(4px); z-index: 1000; padding: 28px; }
#logModal .dialog { position: relative; width: min(900px, 95vw); height: min(70vh, 800px); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); margin: 0 auto; display: grid; grid-template-rows: auto 1fr; gap: 8px; padding: 16px; }
#logModal .dialog header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#logModal .dialog header h3 { margin: 0; font-size: 16px; }
#closeLog { background: none !important; color: var(--muted) !important; border: none !important; padding: 0 !important; font-size: 22px !important; box-shadow: none !important; }
:root.theme-light #closeLog { color: #475569 !important; }
#log { background: var(--elevated); border: 1px dashed var(--border); color: var(--muted); padding: 12px; border-radius: var(--radius-xs); max-height: 100%; height: 100%; overflow: auto; margin: 0; }

/* Voice modal */
#voiceModal { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(4px); z-index: 1000; padding: 28px; }
#voiceModal .dialog { position: relative; width: min(720px, 95vw); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); margin: 0 auto; display: grid; grid-template-rows: auto 1fr auto; gap: 12px; padding: 16px; }
#voiceModal .dialog header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#closeVoice { background: none !important; color: var(--muted) !important; border: none !important; padding: 0 !important; font-size: 22px !important; box-shadow: none !important; }

/* Playback and rendered text cards */
#playbackCard { display: none; }
#renderCard { display: none; }

/* Theme override classes */
:root.theme-dark {
  --bg: #0b0c10; --surface:#111217; --elevated:#171923; --text:#e8eaed; --muted:#9aa0a6; --border:#2a2d36; --primary:#4f8cff; --primary-600:#3a74ee; --success:#22c55e; --warning:#f59e0b; --error:#ef4444; --accent:#7c3aed; --shadow:0 10px 30px rgba(0,0,0,.35);
}
:root.theme-light {
  --bg:#f7f7fb; --surface:#ffffff; --elevated:#ffffff; --text:#0f172a; --muted:#374151; --border:#e2e8f0; --primary:#1d4ed8; --primary-600:#1e40af; --success:#15803d; --warning:#b45309; --error:#b91c1c; --accent:#7c3aed; --shadow:0 10px 25px rgba(0,0,0,.08);
}

.icon-toggle { width: 40px; height: 40px; border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; font-size: 18px; }
.icon-toggle.pulse { animation: spinScale .5s ease; }
#themeToggle { position: fixed; top: 16px; right: 16px; z-index: 1200; }
#logToggle { position: fixed; bottom: 16px; left: 16px; z-index: 1200; padding: 8px 10px !important; border-radius: 999px !important; background: linear-gradient(180deg, #2a2f3a, #21242d) !important; color: var(--text) !important; border: 1px solid var(--border) !important; box-shadow: 0 8px 20px rgba(0,0,0,.22) !important; font-size: 12px; }
:root.theme-light #logToggle { color: #ffffff !important; background: linear-gradient(180deg, #475569, #334155) !important; border-color: rgba(0,0,0,0.15) !important; }
@keyframes spinScale { 0% { transform: rotate(0) scale(1); } 60% { transform: rotate(180deg) scale(1.1); } 100% { transform: rotate(360deg) scale(1); } }

/* Floating center-on-word button */
#scrollToWord { position: fixed; bottom: 16px; right: 16px; z-index: 1200; padding: 8px 12px !important; border-radius: 999px !important; background: linear-gradient(180deg, #2a2f3a, #21242d) !important; color: var(--text) !important; border: 1px solid var(--border) !important; box-shadow: 0 8px 20px rgba(0,0,0,.22) !important; font-size: 12px; display: none; }
:root.theme-light #scrollToWord { color: #ffffff !important; background: linear-gradient(180deg, #475569, #334155) !important; border-color: rgba(0,0,0,0.15) !important; }

/* Links injected (download) */
a[download] { margin-left: 10px; padding: 8px 12px; border-radius: 999px; background: linear-gradient(180deg, #2a2f3a, #21242d); border: 1px solid var(--border); color: var(--text); text-decoration: none; box-shadow: 0 6px 16px rgba(0,0,0,.22); font-weight: 600; }
a[download]:hover { filter: brightness(1.04); }

/* Utility spacing */
#progress { margin-top: 14px; padding: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }

/* Targeted overrides for key controls */
#synth, #fetchUrl, #confirmFetch, #cancelFetch, #closePreview, #helpGetUrl, #refreshFrame {
  background: linear-gradient(135deg, var(--primary), var(--accent)) !important;
  color: #fff !important;
  border: none !important;
  border-radius: 999px !important;
  padding: 10px 14px !important;
  box-shadow: 0 6px 18px rgba(79,140,255,.25) !important;
}
#cancelFetch { background: linear-gradient(180deg, #2a2f3a, #21242d) !important; color: var(--text) !important; box-shadow: 0 6px 16px rgba(0,0,0,.25) !important; }
#closePreview { background: none !important; color: var(--muted) !important; border: none !important; padding: 0 !important; font-size: 24px !important; box-shadow: none !important; }
#helpGetUrl, #refreshFrame { background: color-mix(in oklab, var(--primary) 12%, var(--elevated)) !important; color: var(--text) !important; border: 1px solid var(--border) !important; box-shadow: 0 6px 16px rgba(0,0,0,.22) !important; }
#urlInput, #currentPageUrl { background: var(--elevated) !important; color: var(--text) !important; border: 1px solid var(--border) !important; border-radius: 10px !important; padding: 10px 12px !important; }

/* Rebuild layout helpers */
label[for="urlInput"] { display: block; margin-bottom: 6px; }
#urlInput + button#fetchUrl { margin-left: 10px; }
#fetchStatus { margin-top: 6px; font-size: 12px; color: var(--muted) !important; }
button#useTextarea { margin-left: auto; }
.field-row { display: flex; gap: 10px; align-items: center; }
.actions { display: flex; gap: 10px; align-items: center; justify-content: flex-start; }

/* Header and title area */
body > h2:first-of-type { margin-bottom: 8px; background: linear-gradient(90deg, var(--primary), var(--accent)); -webkit-background-clip: text; background-clip: text; color: transparent; }
body > p strong { color: var(--text); }

/* Fine-tune small controls in modal header */
#helpGetUrl, #refreshFrame { padding: 6px 8px; font-size: 12px; }
#cancelFetch { background: linear-gradient(180deg, #2a2f3a, #21242d); color: var(--text); }

/* Make checkboxes rows tidy */
#usePlainFetch + label,
#useCorsProxy + label,
#useJina + label,
#useReadability + label { display: inline-flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; border: 1px dashed var(--border); background: color-mix(in oklab, var(--elevated) 70%, transparent); }
/* When inside compact methods row, remove the dashed decoration */
.methods .option-label { border: none !important; background: transparent !important; padding: 0 !important; }

/* Rich content rendering (preserve layout) */
.rich-content {
  line-height: 1.6;
}
.rich-content h1, .rich-content h2, .rich-content h3,
.rich-content h4, .rich-content h5, .rich-content h6 { margin: 1em 0 .5em; }
.rich-content p { margin: .6em 0; color: var(--muted); }
.rich-content a { color: color-mix(in oklab, var(--primary) 85%, #fff); text-decoration: underline; }
.rich-content ul, .rich-content ol { margin: .6em 0 .6em 1.2em; }
.rich-content li { margin: .25em 0; }
.rich-content blockquote { border-left: 3px solid var(--border); padding-left: .8em; margin: .8em 0; color: color-mix(in oklab, var(--muted) 90%, #888); }
.rich-content code { background: color-mix(in oklab, var(--elevated) 85%, transparent); padding: 0 .25em; border-radius: 4px; }
.rich-content pre { background: var(--elevated); padding: .8em; border-radius: 8px; overflow: auto; }


