:root {
  --bg: #f6f3ec;
  --panel-bg: #ffffff;
  --text: #22201c;
  --muted: #6f6a60;
  --border: #e6e0d3;
  --accent: #5b46e8;
  --accent-2: #8f5cf0;
  --accent-contrast: #ffffff;
  --danger: #d64545;
  --page-shadow: 0 2px 10px rgba(40, 30, 10, 0.10);
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16151c;
    --panel-bg: #201f28;
    --text: #ece9f2;
    --muted: #a29cb0;
    --border: #34313e;
    --accent: #8a72ff;
    --accent-2: #c084fc;
    --accent-contrast: #14131a;
    --page-shadow: 0 2px 14px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win over any component's own `display`
   rule (flex/grid/etc.) — without this, any class that sets `display` on a
   hideable element silently un-hides it, since attribute and class selectors
   have equal specificity and author CSS loads after the UA stylesheet. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Topbar ---------- */

#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.02), 0 2px 8px rgba(40, 30, 10, 0.04);
  flex-wrap: wrap;
  z-index: 20;
}

.brand {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
  margin-right: 4px;
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--bg) 65%, transparent);
  border: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
}

.spacer { flex: 1; }

.slider-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

#speed-readout, #page-readout {
  font-size: 12px;
  color: var(--muted);
  min-width: 60px;
}

.progress-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  max-width: 360px;
}

.progress-wrap input[type="range"] { flex: 1; }

/* ---------- Buttons / inputs ---------- */

.btn {
  border: 1px solid var(--border);
  background: var(--panel-bg);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s, transform 0.05s, box-shadow 0.15s;
}
.btn:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 10%, var(--panel-bg));
  box-shadow: 0 2px 6px rgba(91, 70, 232, 0.12);
}
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn.primary {
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  color: var(--accent-contrast);
  border-color: transparent;
}
.btn.primary:hover { filter: brightness(1.08); box-shadow: 0 3px 10px rgba(91, 70, 232, 0.28); }

.btn.small { padding: 5px 10px; font-size: 12.5px; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn.icon-btn { padding: 6px 9px; font-size: 15px; }

input[type="range"] { accent-color: var(--accent); }
input[type="text"], input[type="password"], select, textarea {
  font-family: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
}
select:focus, input:focus, textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 1px;
}

/* ---------- Layout ---------- */

#layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

#reader-pane {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  scroll-behavior: auto;
  padding: 24px 0 60vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  background:
    radial-gradient(ellipse 900px 500px at 15% -5%, color-mix(in srgb, var(--accent) 7%, transparent), transparent),
    radial-gradient(ellipse 700px 500px at 100% 0%, color-mix(in srgb, var(--accent-2) 6%, transparent), transparent);
}

#drop-zone {
  width: min(94%, 720px);
  margin-top: 6vh;
  padding: 48px 40px;
  text-align: center;
  color: var(--muted);
}
#drop-zone.drag-over .drop-hint {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 6%, var(--panel-bg));
}
.drop-hint {
  border: 2px dashed var(--border);
  border-radius: 20px;
  padding: 40px 32px;
  background: color-mix(in srgb, var(--panel-bg) 60%, transparent);
  transition: border-color 0.2s, background 0.2s;
}
.drop-icon { font-size: 44px; line-height: 1; margin-bottom: 6px; }
.drop-hint h1 {
  font-size: clamp(19px, 2.6vw, 25px);
  line-height: 1.35;
  margin: 8px 0 6px;
  color: var(--text);
  font-weight: 700;
}
.drop-sub { margin: 4px 0; font-size: 14.5px; }
.drop-sub strong { color: var(--text); }
.drop-formats { letter-spacing: 0.01em; margin-bottom: 22px; }

.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin: 8px 0 26px;
  text-align: left;
}
.feature-card {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 14px 14px;
  border-radius: var(--radius);
  background: var(--panel-bg);
  border: 1px solid var(--border);
}
.feature-card div { display: flex; flex-direction: column; gap: 2px; }
.feature-card strong { font-size: 13px; color: var(--text); }
.feature-icon { font-size: 20px; line-height: 1.2; }

.privacy-note {
  border-top: 1px solid var(--border);
  padding-top: 16px;
  margin-top: 4px !important;
}

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

#pages-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  width: 100%;
}
#pages-container:empty { display: none; }

.doc-page {
  position: relative;
  background: #fff;
  box-shadow: var(--page-shadow);
  border-radius: 2px;
}

.pdf-page canvas { display: block; }

.doc-flow-page {
  width: min(92%, 760px);
  background: var(--panel-bg);
  box-shadow: var(--page-shadow);
  border-radius: var(--radius);
  padding: 40px 48px;
  color: #1a1d21;
  line-height: 1.6;
}
.doc-flow-page img { max-width: 100%; }

.slide-page {
  width: min(92%, 900px);
  aspect-ratio: 16 / 9;
  background: linear-gradient(160deg, #ffffff, #eef0f5);
  box-shadow: var(--page-shadow);
  border-radius: var(--radius);
  padding: 36px 44px;
  color: #1a1d21;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.slide-page h2 { margin-top: 0; }
.slide-number {
  position: absolute;
  bottom: 10px;
  right: 14px;
  font-size: 12px;
  color: #9aa0aa;
}

.text-block.reading-active {
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  border-radius: 3px;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 30%, transparent);
}

/* selection floating toolbar */
#selection-toolbar {
  position: absolute;
  display: flex;
  gap: 6px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 5px;
  box-shadow: 0 8px 24px rgba(30, 20, 60, 0.18);
  z-index: 50;
}

/* ---------- Side panel ---------- */

#side-panel {
  width: 380px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: var(--panel-bg);
  display: flex;
  flex-direction: column;
  min-height: 0;
  box-shadow: -4px 0 16px rgba(20, 10, 40, 0.03);
}

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
}

.panel-header {
  padding: 16px 18px 10px;
  border-bottom: 1px solid var(--border);
}
.panel-header h2 {
  margin: 0 0 4px;
  font-size: 15.5px;
  display: flex;
  align-items: center;
  gap: 7px;
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

#notes-text {
  width: 100%;
  min-height: 140px;
  resize: vertical;
}

#ai-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: color-mix(in srgb, var(--accent) 4%, var(--panel-bg));
}
.ai-panel-header { display: flex; align-items: baseline; justify-content: space-between; }
.ai-panel-header h3 { margin: 0; font-size: 13px; }
.ai-output {
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  max-height: 260px;
  overflow-y: auto;
  min-height: 40px;
}
.ai-actions, .summarize-row { display: flex; gap: 8px; align-items: center; }
.summarize-row select { flex: 1; }

.ai-info-card {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 14px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 12%, var(--panel-bg)), color-mix(in srgb, var(--accent-2) 8%, var(--panel-bg)));
  border: 1px solid color-mix(in srgb, var(--accent) 25%, var(--border));
}
.ai-info-icon { font-size: 22px; line-height: 1.3; }
.ai-info-card strong { font-size: 13.5px; }
.ai-info-card p { margin: 4px 0 0; }

fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
legend {
  padding: 0 7px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--accent);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12.5px;
  color: var(--muted);
}
.field input, .field select { color: var(--text); }
.field.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

/* ---------- Toast ---------- */

#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 100;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

/* ---------- Export modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 15, 35, 0.55);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal {
  width: min(92%, 480px);
  max-height: 88vh;
  overflow-y: auto;
  background: var(--panel-bg);
  border-radius: var(--radius);
  padding: 22px 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.modal h2 { margin: 0 0 6px; font-size: 17px; }

#export-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 14px 0;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 10px;
}

#export-live, #export-result {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}
#export-canvas, #export-preview-video {
  width: 100%;
  max-height: 360px;
  object-fit: contain;
  background: #000;
  border-radius: 8px;
}

@media (max-width: 860px) {
  #side-panel { position: fixed; right: 0; top: 49px; bottom: 0; width: 100%; max-width: 380px; box-shadow: -6px 0 20px rgba(0,0,0,0.2); }
  .feature-grid { grid-template-columns: repeat(2, 1fr); }
}

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