/* ─── Reset & Variables ─── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #ffffff;
  --surface: #fafafa;
  --surface-2: #f4f4f5;
  --border: #e4e4e7;
  --border-hover: #a1a1aa;
  --text: #09090b;
  --text-muted: #71717a;
  --text-placeholder: #a1a1aa;
  --primary: #18181b;
  --primary-fg: #fafafa;
  --accent: #f4f4f5;
  --accent-hover: #e4e4e7;
  --ring: rgba(24,24,27,0.18);

  --blue: #3b82f6;
  --blue-light: #eff6ff;
  --green: #22c55e;
  --green-light: #f0fdf4;
  --yellow: #eab308;
  --yellow-light: #fefce8;
  --red: #ef4444;
  --red-light: #fef2f2;

  --line-h: 28px;
  --linenums-w: 48px;
  --controls-w: 232px;
  --max-w: 1440px;
  --header-h: 52px;
  --controls-header-h: 28px;

  --radius: 6px;
  --radius-sm: 4px;
  --radius-lg: 8px;

  --font-mono: 'JetBrains Mono', 'Fira Code', ui-monospace, monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -2px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -4px rgba(0,0,0,0.1);
}

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ─── Page Wrapper (max-width + center) ─── */
.page-wrap {
  max-width: var(--max-w);
  margin: 0 auto;
  height: 100vh;
  display: flex;
  flex-direction: column;
  /* side borders so the centered content has definition on wide screens */
  box-shadow: 0 0 0 1px var(--border);
}

/* ─── Header ─── */
.header {
  flex-shrink: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-icon {
  width: 18px;
  height: 18px;
  color: var(--text);
  flex-shrink: 0;
}

.app-title {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.app-version {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
  margin-left: 4px;
  letter-spacing: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 500;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s, color 0.1s;
  white-space: nowrap;
  line-height: 1;
}

.btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.btn-outline {
  background: var(--bg);
  border-color: var(--border);
  color: var(--text);
}
.btn-outline:hover { background: var(--accent); border-color: var(--border-hover); }

.btn-ghost {
  background: transparent;
  color: var(--text);
}
.btn-ghost:hover { background: var(--accent); }

.btn-primary {
  background: var(--primary);
  color: var(--primary-fg);
  border-color: var(--primary);
}
.btn-primary:hover { background: #3f3f46; }

.icon-btn {
  padding: 6px;
  width: 32px;
  height: 32px;
  justify-content: center;
}
.icon-btn svg { width: 16px; height: 16px; }

.chevron {
  width: 12px !important;
  height: 12px !important;
  transition: transform 0.15s;
}
.dropdown-wrapper.open .chevron { transform: rotate(180deg); }

/* ─── Dropdown ─── */
.dropdown-wrapper { position: relative; }

.dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 200px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  z-index: 200;
}
.dropdown-wrapper.open .dropdown-menu { display: block; }

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 14px;
  background: none;
  border: none;
  font-size: 13px;
  font-family: var(--font-sans);
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: background 0.1s;
}
.dropdown-item:hover { background: var(--accent); }
.dropdown-item svg { width: 14px; height: 14px; color: var(--text-muted); flex-shrink: 0; }

/* ─── Editor Wrapper ─── */
.editor-wrap {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

/* ─── Line Numbers Column ─── */
.line-numbers-col {
  flex-shrink: 0;
  width: var(--linenums-w);
  overflow: hidden;           /* scroll is driven by textarea scroll event */
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding-top: var(--controls-header-h); /* align with controls header */
}

.line-num-item {
  height: var(--line-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  user-select: none;
}

/* ─── Main Textarea ─── */
#mainTextarea {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  overflow: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: var(--line-h);
  padding: calc(var(--controls-header-h) + 0px) 12px 0 12px; /* top matches controls header */
  white-space: pre;
  /* Line highlight via CSS gradient — updated by JS via --hl-top / --hl-bottom */
  background-color: var(--bg);
  background-image: linear-gradient(
    transparent var(--hl-top, 9999px),
    var(--surface-2) var(--hl-top, 9999px),
    var(--surface-2) var(--hl-bottom, 9999px),
    transparent var(--hl-bottom, 9999px)
  );
  color: var(--text);
  caret-color: var(--primary);
}

#mainTextarea::placeholder {
  color: var(--text-placeholder);
  font-style: italic;
}

/* ─── Controls Column ─── */
.controls-col {
  flex-shrink: 0;
  width: var(--controls-w);
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.controls-col-header {
  flex-shrink: 0;
  height: var(--controls-header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 0 6px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}

.col-header-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.col-header-label.slide-col {
  font-size: 11px;
}

.controls-rows {
  flex: 1;
  overflow: hidden; /* scroll driven by textarea scroll event */
}

/* ─── Control Row (per line) ─── */
.control-row {
  height: var(--line-h);
  display: flex;
  align-items: center;
  gap: 1px;
  padding: 0 6px;
  transition: background 0.08s;
}

.control-row:hover {
  background: var(--accent);
}

.control-row.is-playing {
  background: var(--blue-light);
}

.control-row.is-loading {
  background: var(--yellow-light);
}

.control-row.is-error {
  background: var(--red-light);
}

.control-row.is-cursor:not(.is-playing):not(.is-loading):not(.is-error) {
  background: var(--surface-2);
}

.line-num-item.is-cursor {
  color: var(--text);
  font-weight: 600;
}

/* ─── Control Buttons ─── */
.ctrl-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.1s, color 0.1s;
}

.ctrl-btn svg {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
  pointer-events: none;
}

.ctrl-btn:hover { background: var(--accent-hover); color: var(--text); }
.ctrl-btn:disabled { opacity: 0.3; cursor: not-allowed; }

.ctrl-btn.btn-play { color: var(--blue); }
.ctrl-btn.btn-play:hover { background: var(--blue-light); color: var(--blue); }

.ctrl-btn.btn-regen:hover { background: var(--yellow-light); color: var(--yellow); }
.ctrl-btn.btn-dl:hover { background: var(--green-light); color: var(--green); }

.ctrl-btn.is-spinning svg { animation: spin 0.7s linear infinite; }

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* ─── Row line number (inside control row) ─── */
.row-line-num {
  font-size: 11px;
  font-family: var(--font-mono);
  color: var(--text-muted);
  min-width: 20px;
  text-align: right;
  flex-shrink: 0;
  padding-right: 2px;
  user-select: none;
}

/* ─── Slide ID Input ─── */
.slide-id-input {
  flex-shrink: 0;
  width: 46px;
  height: 22px;
  padding: 0 6px;
  margin-left: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-family: var(--font-mono);
  color: var(--text);
  background: var(--bg);
  text-align: center;
  outline: none;
  transition: border-color 0.1s, box-shadow 0.1s;
  -moz-appearance: textfield;
}

.slide-id-input::-webkit-outer-spin-button,
.slide-id-input::-webkit-inner-spin-button { -webkit-appearance: none; }

.slide-id-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--ring);
}

/* ─── Status dot next to line number ─── */
.line-num-item {
  position: relative;
  gap: 4px;
}

.status-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  flex-shrink: 0;
  visibility: hidden;
}
.status-dot.cached  { background: var(--green);  visibility: visible; }
.status-dot.playing { background: var(--blue);   visibility: visible; animation: pulse 1s ease-in-out infinite; }
.status-dot.loading { background: var(--yellow); visibility: visible; animation: pulse 0.5s ease-in-out infinite; }
.status-dot.error   { background: var(--red);    visibility: visible; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

/* ─── Modal ─── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 300;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: min(480px, 100%);
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

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

.modal-notice {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.6;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
}

/* ─── Form ─── */
.form-group { display: flex; flex-direction: column; gap: 6px; }

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.form-label { font-size: 13px; font-weight: 500; }
.form-hint-inline { font-weight: 400; color: var(--text-muted); }

.form-label-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.form-range {
  width: 100%;
  height: 6px;
  cursor: pointer;
  accent-color: var(--primary);
  margin-top: 2px;
}

.form-input {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--font-sans);
  color: var(--text);
  background: var(--bg);
  outline: none;
  width: 100%;
  transition: border-color 0.1s, box-shadow 0.1s;
}
.form-input:focus { border-color: var(--primary); box-shadow: 0 0 0 2px var(--ring); }
select.form-input { cursor: pointer; }
.form-hint { font-size: 12px; color: var(--text-muted); }

/* ─── Status Bar ─── */
.status-bar {
  flex-shrink: 0;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  font-size: 11px;
  font-family: var(--font-mono);
  color: var(--text-muted);
  user-select: none;
}

.status-bar.over-limit { color: var(--red); }

/* ─── Per-line char count column ─── */
.char-count-outer {
  flex-shrink: 0;
  width: 36px;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.char-count-header {
  flex-shrink: 0;
  height: var(--controls-header-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 6px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  user-select: none;
}

.char-count-col {
  flex: 1;
  overflow: hidden;
  background: var(--bg);
}

.char-count-item {
  height: var(--line-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 6px;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
  opacity: 0.6;
  user-select: none;
}

.char-count-item.over-limit {
  color: var(--red);
  opacity: 1;
  font-weight: 600;
}

/* ─── Toast ─── */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 400;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  background: var(--primary);
  color: var(--primary-fg);
  box-shadow: var(--shadow);
  pointer-events: auto;
  max-width: 320px;
  animation: toast-in 0.2s ease;
}
.toast.error   { background: var(--red); }
.toast.success { background: #16a34a; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ─── Scrollbar ─── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-hover); }

/* ─── Responsive ─── */
@media (max-width: 900px) {
  :root {
    --controls-w: 200px;
  }

  .ctrl-btn {
    width: 24px;
    height: 24px;
  }

  .ctrl-btn svg {
    width: 12px;
    height: 12px;
  }
}

@media (max-width: 600px) {
  :root {
    --linenums-w: 36px;
    --controls-w: 172px;
  }

  .app-title { display: none; }

  .line-num-item {
    font-size: 10px;
    padding-right: 6px;
  }

  .btn span, .btn-text { display: none; }

  /* Hide download label text, keep icon */
  #downloadAllBtn .btn-text { display: none; }

  .slide-id-input { width: 38px; }
}

@media (max-width: 400px) {
  :root { --controls-w: 130px; }

  .row-line-num { display: none; }

  /* hide regen + download buttons to save space */
  .ctrl-btn.btn-regen,
  .ctrl-btn.btn-dl { display: none; }
}
