/* ── Color tokens ─────────────────────────────────────────── */
:root {
  --bg:           #f0f4f8;
  --bg-raised:    #ffffff;
  --bg-subtle:    #f1f5f9;
  --bg-hover:     #e2e8f0;
  --bg-hover2:    #cbd5e1;
  --bg-chosen:    #eff6ff;
  --bg-active:    #dbeafe;
  --border:       #e2e8f0;
  --border-input: #cbd5e1;
  --border-soft:  #e2e8f0;
  --text:         #0f172a;
  --text-muted:   #64748b;
  --text-faint:   #94a3b8;
  --text-dim:     #94a3b8;
  --text-dimmer:  #cbd5e1;
  --text-icon:    #64748b;
  --accent:       #2563eb;
  --danger:       #dc2626;
  color-scheme: light;
}

/* Auto dark (respects system preference unless overridden) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:           #0f172a;
    --bg-raised:    #1e293b;
    --bg-subtle:    #1e293b;
    --bg-hover:     #334155;
    --bg-hover2:    #475569;
    --bg-chosen:    #1e3a5f;
    --bg-active:    #1e3a5f;
    --border:       #334155;
    --border-input: #475569;
    --border-soft:  #334155;
    --text:         #f1f5f9;
    --text-muted:   #94a3b8;
    --text-faint:   #64748b;
    --text-dim:     #64748b;
    --text-dimmer:  #475569;
    --text-icon:    #94a3b8;
    --accent:       #60a5fa;
    --danger:       #f87171;
    color-scheme: dark;
  }
}

/* Manual dark override */
:root[data-theme="dark"] {
  --bg:           #0f172a;
  --bg-raised:    #1e293b;
  --bg-subtle:    #1e293b;
  --bg-hover:     #334155;
  --bg-hover2:    #475569;
  --bg-chosen:    #1e3a5f;
  --bg-active:    #1e3a5f;
  --border:       #334155;
  --border-input: #475569;
  --border-soft:  #334155;
  --text:         #f1f5f9;
  --text-muted:   #94a3b8;
  --text-faint:   #64748b;
  --text-dim:     #64748b;
  --text-dimmer:  #475569;
  --text-icon:    #94a3b8;
  --accent:       #60a5fa;
  --danger:       #f87171;
  color-scheme: dark;
}

/* Manual light override */
:root[data-theme="light"] {
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  font-family: system-ui, Arial;
  margin: 0;
  height: 100vh;
  display: flex;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

/* ── Sidebar ──────────────────────────────────────────────── */
#sidebar {
  width: 280px;
  min-width: 280px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 1rem;
  transition: transform .25s ease;
  z-index: 100;
  background: var(--bg);
}

/* On wide screens: sidebar slides in/out by shifting left */
#sidebar.hidden { transform: translateX(-100%); }

/* On narrow screens: sidebar becomes a floating overlay */
@media (max-width: 640px) {
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    box-shadow: 4px 0 16px rgba(0,0,0,.18);
  }
  #sidebar.hidden { transform: translateX(-110%); }
  body.sidebar-open::after {
    content: '';
    position: fixed; inset: 0;
    background: rgba(0,0,0,.35);
    z-index: 99;
  }
}

#sidebar h2 { margin: 0 0 .75rem; font-size: 1rem; }

.save-row { display: flex; gap: .5rem; align-items: center; }
.save-row input { flex: 1; min-width: 0; }
.save-row button { width: auto; white-space: nowrap; flex-shrink: 0; }

.list-item { border: 1px solid var(--border); border-radius: 6px; margin: .4rem 0; overflow: hidden; }
.list-header {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .4rem .6rem;
  cursor: pointer;
  background: var(--bg-raised);
  user-select: none;
}
.list-header:hover { background: var(--bg-hover); }
.list-header .list-name { flex: 1; font-weight: 600; font-size: .88rem; }
.list-header .list-count { font-size: .75rem; color: var(--text-dim); }
.list-header .del-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-dimmer);
  font-size: .85rem;
  padding: 0 .2rem;
  line-height: 1;
  width: auto;
}
.list-header .del-btn:hover { color: var(--danger); }

.list-tasks { padding: .25rem .6rem .5rem; display: none; }
.list-tasks.open { display: block; }

label.task-check:has(> input:checked) { border: solid 2px var(--accent); color: var(--accent); }
label.task-check:has(> input:checked)::before { content: " ✓ "; }
.task-check { align-items: flex-start; gap: .4rem; margin: .2rem 0; font-size: .83rem; cursor: pointer; border-radius: 4px; padding: .15rem .3rem; }
.task-check input[type=checkbox] { display: none; }

.load-btn { width: 100%; margin-top: .75rem; padding: .45rem; font-size: .85rem; cursor: pointer; }

.select-all-btn {
  background: none;
  border: none;
  font-size: .75rem;
  color: var(--text-dim);
  cursor: pointer;
  padding: 0 .1rem;
  width: auto;
  flex-shrink: 0;
}
.select-all-btn:hover { color: var(--accent); background: none; }

.io-row { display: flex; gap: .4rem; margin-top: .75rem; }
.io-row button { flex: 1; font-size: .8rem; padding: .35rem .4rem; cursor: pointer; width: auto; }

.empty-msg { font-size: .83rem; color: var(--text-dimmer); margin: .5rem 0; }

/* ── Task rows ────────────────────────────────────────────── */
.task-row {
  display: flex;
  align-items: center;
  gap: .4rem;
  margin: .2rem 0;
}
.drag-handle {
  cursor: grab;
  color: var(--border-input);
  font-size: 1.1rem;
  flex-shrink: 0;
  padding: 0 .1rem;
  user-select: none;
  touch-action: none;
  line-height: 1;
}
.drag-handle:active { cursor: grabbing; }
.task-row input { flex: 1; margin: 0; }
.del-task-btn {
  width: auto;
  flex-shrink: 0;
  padding: .3rem .5rem;
  background: none;
  border: 1px solid var(--border-soft);
  color: var(--border-input);
  cursor: pointer;
  border-radius: 5px;
  line-height: 1;
}
.del-task-btn:hover { color: var(--danger); border-color: var(--danger); background: none; }
.task-row.sortable-ghost { opacity: .3; }
.task-row.sortable-chosen { background: var(--bg-chosen); border-radius: 5px; }
#addTaskBtn {
  margin-top: .4rem;
  background: none;
  border: 1px dashed var(--border-input);
  color: var(--text-faint);
  font-size: .85rem;
  padding: .35rem;
}
#addTaskBtn:hover { border-color: var(--text-muted); color: var(--text); background: none; }

/* Links rendered inside the study task display */
#studyTask a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
}

/* ── Main ─────────────────────────────────────────────────── */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* ── Top bar ──────────────────────────────────────────────── */
#topbar {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .5rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

/* Shared style for icon buttons in the topbar */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  flex-shrink: 0;
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1.15rem;
  line-height: 1;
  color: var(--text-icon);
  transition: background .15s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.active { background: var(--bg-active); color: var(--text); }

/* Push theme toggle to the right */
#themeToggle { margin-left: auto; font-size: 1rem; }

#topbar h1 { margin: 0; font-size: 1rem; font-weight: 600; }

/* ── Global form elements ─────────────────────────────────── */
label { font-size: .82rem; color: var(--text-muted); display: block; margin-top: .6rem; }
input, button, textarea, select {
  font: inherit;
  margin: .2rem 0;
  padding: .45rem .5rem;
  width: 100%;
  border: 1px solid var(--border-input);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
}
button { cursor: pointer; background: var(--bg-subtle); }
button:hover { background: var(--bg-hover2); }
button:disabled { opacity: .45; cursor: default; }

/* ── Setup view ───────────────────────────────────────────── */
#setupView {
  padding: 1rem 1.5rem;
  overflow-y: auto;
  max-width: 580px;
  width: 100%;
}
#setupView.hidden { display: none; }

.row { display: flex; gap: .5rem; }
.row > * { flex: 1; min-width: 0; }

@media (max-width: 420px) {
  .row { flex-direction: column; gap: 0; }
}

.setup-actions { display: flex; gap: .5rem; margin-top: 1rem; }
.setup-actions button { flex: 1; }

/* ── Study view ───────────────────────────────────────────── */
#studyView {
  display: none;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem 1.5rem;
  text-align: center;
}
#studyView.active { display: flex; }

.study-phase {
  font-size: .85rem;
  color: var(--text-faint);
  letter-spacing: .1em;
  text-transform: uppercase;
  margin-bottom: .15rem;
}
.big-timer {
  font-size: clamp(4.5rem, 14vw, 7.5rem);
  font-weight: 200;
  letter-spacing: -.03em;
  line-height: 1;
  margin: .15rem 0 .6rem;
}
.study-task {
  font-size: clamp(1.1rem, 5vw, 1.4rem);
  font-weight: 600;
  max-width: 520px;
  margin: .2rem 0;
}
.study-cycle {
  font-size: .82rem;
  color: var(--text-dim);
  margin-bottom: 1.5rem;
}
.study-controls { display: flex; gap: .6rem; flex-wrap: wrap; justify-content: center; }
.study-controls button {
  width: auto;
  min-width: 90px;
  padding: .55rem 1.25rem;
}

#studyMusicBox { margin-top: 1.75rem; width: min(220px, 70vw); }
#studyMusicBox label { text-align: center; }
#studyMusicVol { width: 100%; }

/* ── Mobile tweaks ────────────────────────────────────────── */
@media (max-width: 640px) {
  input, button, textarea, select {
    padding: .65rem .6rem;
    font-size: 1rem;
  }
  label { font-size: .9rem; }
  #setupView { padding: .75rem 1rem; }
  .study-controls button {
    min-width: 80px;
    padding: .75rem 1.1rem;
    font-size: 1rem;
  }
  .big-timer { font-size: clamp(4rem, 20vw, 7rem); }
  .study-task { font-size: clamp(1.1rem, 5.5vw, 1.4rem); }
}
