/* ---- Tokens ---------------------------------------------------------- */
:root {
  --paper: oklch(97.6% 0.008 85);
  --paper-2: oklch(95.2% 0.011 85);
  --paper-3: oklch(91.5% 0.014 85);
  --paper-4: oklch(87% 0.016 85);
  --ink: oklch(23% 0.018 60);
  --ink-2: oklch(44% 0.018 60);
  --ink-3: oklch(60% 0.014 60);
  --line: oklch(87.5% 0.013 80);
  --line-strong: oklch(78% 0.016 80);
  --accent: oklch(49% 0.105 205);
  --accent-hover: oklch(43% 0.11 205);
  --accent-soft: oklch(93% 0.035 205);
  --accent-ink: oklch(99% 0 0);
  --danger: oklch(54% 0.17 28);
  --danger-soft: oklch(94.5% 0.04 28);
  --ok: oklch(56% 0.13 150);
  --ok-soft: oklch(94% 0.05 150);
  --warn: oklch(72% 0.14 75);
  --warn-soft: oklch(95% 0.06 85);

  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-body: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, system-ui, sans-serif;
  --font-mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;

  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --text-lg: 1.125rem;
  --text-xl: clamp(1.5rem, 1.2rem + 1vw, 1.9rem);
  --text-display: clamp(1.9rem, 1.4rem + 1.8vw, 2.6rem);

  --r-xs: 6px;
  --r-sm: 9px;
  --r-md: 13px;
  --r-lg: 20px;
  --r-pill: 999px;

  --shadow-1: 0 1px 2px oklch(20% 0.02 60 / 0.06), 0 0 0 1px oklch(20% 0.02 60 / 0.04);
  --shadow-2: 0 10px 30px -12px oklch(20% 0.02 60 / 0.28), 0 2px 6px oklch(20% 0.02 60 / 0.06);
  --shadow-3: 0 30px 70px -20px oklch(20% 0.02 60 / 0.4), 0 6px 18px oklch(20% 0.02 60 / 0.08);

  --dur-fast: 120ms;
  --dur: 200ms;
  --dur-slow: 360ms;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --ease-spring: cubic-bezier(0.34, 1.4, 0.64, 1);

  --ok-ink: oklch(38% 0.1 150);
  --warn-ink: oklch(45% 0.1 70);
  --code-bg: oklch(100% 0 0 / 0.6);
  --glow-1: oklch(95% 0.03 205 / 0.55);
  --glow-2: oklch(95% 0.03 60 / 0.5);
  --folder-bg: linear-gradient(160deg, oklch(97% 0.03 80), oklch(93% 0.05 75));
  --btn-highlight: oklch(100% 0 0 / 0.6);
  --select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 4.5 6 8l3.5-3.5' fill='none' stroke='%23555' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --grain-opacity: 0.35;

  --topbar-h: 60px;
  --gutter: clamp(16px, 2.5vw, 36px);
  color-scheme: light;
}

/* ---- Dark theme ------------------------------------------------------ */
:root[data-theme="dark"] { color-scheme: dark; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { color-scheme: dark; }
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: oklch(19% 0.01 60);
    --paper-2: oklch(23% 0.012 60);
    --paper-3: oklch(28% 0.014 60);
    --paper-4: oklch(34% 0.016 60);
    --ink: oklch(93% 0.008 80);
    --ink-2: oklch(74% 0.01 80);
    --ink-3: oklch(58% 0.012 80);
    --line: oklch(30% 0.012 60);
    --line-strong: oklch(38% 0.014 60);
    --accent: oklch(72% 0.1 205);
    --accent-hover: oklch(78% 0.1 205);
    --accent-soft: oklch(30% 0.04 205);
    --accent-ink: oklch(16% 0.02 205);
    --danger: oklch(70% 0.16 28);
    --danger-soft: oklch(28% 0.06 28);
    --ok: oklch(72% 0.13 150);
    --ok-soft: oklch(27% 0.05 150);
    --ok-ink: oklch(82% 0.1 150);
    --warn-soft: oklch(30% 0.06 80);
    --warn-ink: oklch(84% 0.1 80);
    --code-bg: oklch(0% 0 0 / 0.3);
    --glow-1: oklch(35% 0.05 205 / 0.35);
    --glow-2: oklch(32% 0.04 60 / 0.35);
    --folder-bg: linear-gradient(160deg, oklch(30% 0.04 80), oklch(26% 0.03 75));
    --btn-highlight: oklch(100% 0 0 / 0.06);
    --select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 4.5 6 8l3.5-3.5' fill='none' stroke='%23bbb' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    --grain-opacity: 0.18;
    --shadow-1: 0 1px 2px oklch(0% 0 0 / 0.4), 0 0 0 1px oklch(100% 0 0 / 0.04);
    --shadow-2: 0 10px 30px -12px oklch(0% 0 0 / 0.7), 0 2px 6px oklch(0% 0 0 / 0.3);
    --shadow-3: 0 30px 70px -20px oklch(0% 0 0 / 0.8), 0 6px 18px oklch(0% 0 0 / 0.4);
  }
}
:root[data-theme="dark"] {
  --paper: oklch(19% 0.01 60);
  --paper-2: oklch(23% 0.012 60);
  --paper-3: oklch(28% 0.014 60);
  --paper-4: oklch(34% 0.016 60);
  --ink: oklch(93% 0.008 80);
  --ink-2: oklch(74% 0.01 80);
  --ink-3: oklch(58% 0.012 80);
  --line: oklch(30% 0.012 60);
  --line-strong: oklch(38% 0.014 60);
  --accent: oklch(72% 0.1 205);
  --accent-hover: oklch(78% 0.1 205);
  --accent-soft: oklch(30% 0.04 205);
  --accent-ink: oklch(16% 0.02 205);
  --danger: oklch(70% 0.16 28);
  --danger-soft: oklch(28% 0.06 28);
  --ok: oklch(72% 0.13 150);
  --ok-soft: oklch(27% 0.05 150);
  --ok-ink: oklch(82% 0.1 150);
  --warn-soft: oklch(30% 0.06 80);
  --warn-ink: oklch(84% 0.1 80);
  --code-bg: oklch(0% 0 0 / 0.3);
  --glow-1: oklch(35% 0.05 205 / 0.35);
  --glow-2: oklch(32% 0.04 60 / 0.35);
  --folder-bg: linear-gradient(160deg, oklch(30% 0.04 80), oklch(26% 0.03 75));
  --btn-highlight: oklch(100% 0 0 / 0.06);
  --select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 4.5 6 8l3.5-3.5' fill='none' stroke='%23bbb' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --grain-opacity: 0.18;
  --shadow-1: 0 1px 2px oklch(0% 0 0 / 0.4), 0 0 0 1px oklch(100% 0 0 / 0.04);
  --shadow-2: 0 10px 30px -12px oklch(0% 0 0 / 0.7), 0 2px 6px oklch(0% 0 0 / 0.3);
  --shadow-3: 0 30px 70px -20px oklch(0% 0 0 / 0.8), 0 6px 18px oklch(0% 0 0 / 0.4);
}
:root[data-theme="light"] { color-scheme: light; }

/* ---- Base ------------------------------------------------------------ */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: 1.5;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 100% -10%, var(--glow-1), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, var(--glow-2), transparent 60%),
    var(--paper);
  -webkit-font-smoothing: antialiased;
  transition: background var(--dur), color var(--dur);
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: -1;
  opacity: var(--grain-opacity);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
p { margin: 0; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}
.muted { color: var(--ink-3); }
.mono { font-family: var(--font-mono); font-size: 0.86em; }
.display { font-family: var(--font-display); font-weight: 500; letter-spacing: -0.015em; font-size: var(--text-display); line-height: 1.12; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: var(--r-xs); }

/* ---- Topbar ---------------------------------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  height: var(--topbar-h);
  padding: 0 var(--gutter);
  background: oklch(from var(--paper) l c h / 0.82);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--ink);
  text-decoration: none;
  font-family: var(--font-display);
  font-size: var(--text-lg);
}
.brand-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 34vw; }
.brand-mark {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 7px 7px 7px 3px;
  background: linear-gradient(145deg, var(--accent), oklch(60% 0.11 220));
  box-shadow: inset 0 0 0 1px oklch(100% 0 0 / 0.25), 0 2px 6px oklch(49% 0.105 205 / 0.35);
  position: relative;
}
.brand-mark::after {
  content: "";
  position: absolute;
  left: 5px; right: 5px; top: 9px;
  height: 2px;
  border-radius: 2px;
  background: oklch(100% 0 0 / 0.85);
  box-shadow: 0 4px 0 oklch(100% 0 0 / 0.55);
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }

/* ---- Buttons --------------------------------------------------------- */
.btn {
  --btn-bg: var(--paper-2);
  --btn-fg: var(--ink);
  --btn-border: var(--line-strong);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px;
  border-radius: var(--r-sm);
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-size: var(--text-sm);
  font-weight: 500;
  white-space: nowrap;
  text-decoration: none;
  box-shadow: 0 1px 0 var(--btn-highlight) inset, 0 1px 2px oklch(20% 0.02 60 / 0.05);
  transition: transform var(--dur-fast) var(--ease), background var(--dur-fast), border-color var(--dur-fast), box-shadow var(--dur-fast);
}
.theme-toggle { width: 36px; padding: 0; }
.theme-toggle svg { width: 16px; height: 16px; }
.btn:hover { background: var(--paper-3); border-color: var(--ink-3); }
.btn:active { transform: translateY(1px); box-shadow: none; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn.primary { --btn-bg: var(--accent); --btn-fg: var(--accent-ink); --btn-border: transparent; box-shadow: 0 1px 0 oklch(100% 0 0 / 0.2) inset, 0 6px 14px -6px oklch(49% 0.105 205 / 0.6); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { --btn-fg: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn.danger.solid { --btn-bg: var(--danger); --btn-fg: white; --btn-border: transparent; }
.btn.ghost { --btn-bg: transparent; --btn-border: transparent; box-shadow: none; }
.btn.ghost:hover { background: var(--paper-3); }
.btn.small { height: 30px; padding: 0 10px; font-size: var(--text-xs); border-radius: var(--r-xs); }
.btn.block { width: 100%; height: 42px; font-size: var(--text-base); }
.btn.icon { width: 36px; padding: 0; }
.btn.icon.small { width: 30px; }
.btn svg { width: 16px; height: 16px; flex: none; }

.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border-radius: var(--r-sm);
  background: var(--paper-3);
  border: 1px solid var(--line);
}
.segmented button {
  width: 32px; height: 28px;
  border: 0; border-radius: 6px;
  background: transparent; color: var(--ink-2);
  display: inline-grid; place-items: center;
  transition: background var(--dur-fast), color var(--dur-fast), box-shadow var(--dur-fast);
}
.segmented button svg { width: 16px; height: 16px; }
.segmented button:hover { color: var(--ink); }
.segmented button[aria-pressed="true"] { background: var(--paper); color: var(--ink); box-shadow: var(--shadow-1); }

/* ---- Inputs ---------------------------------------------------------- */
.field { display: grid; gap: 6px; font-size: var(--text-sm); color: var(--ink-2); }
.field > span { font-weight: 500; }
.field input, .field select, .field textarea, .input {
  height: 40px;
  padding: 0 12px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line-strong);
  background: var(--paper);
  color: var(--ink);
  font-size: var(--text-base);
  transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
}
.field input:focus, .input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field .hint { font-size: var(--text-xs); color: var(--ink-3); }
.form-error { min-height: 1.2em; color: var(--danger); font-size: var(--text-sm); }
.form-row { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }

select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 30px !important;
  background-image: var(--select-arrow);
  background-repeat: no-repeat;
  background-position: right 10px center;
}

.switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--paper);
  cursor: pointer;
  transition: border-color var(--dur-fast), background var(--dur-fast);
}
.switch:hover { border-color: var(--line-strong); }
.switch:has(input:checked) { background: var(--accent-soft); border-color: oklch(from var(--accent) l c h / 0.4); }
.switch-text { display: grid; gap: 2px; }
.switch-text strong { font-weight: 600; font-size: var(--text-sm); }
.switch-text span { font-size: var(--text-xs); color: var(--ink-2); }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  flex: none;
  width: 38px; height: 22px;
  border-radius: var(--r-pill);
  background: var(--paper-4);
  position: relative;
  transition: background var(--dur) var(--ease);
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 3px oklch(0% 0 0 / 0.25);
  transition: transform var(--dur) var(--ease-spring);
}
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- Cards & layout -------------------------------------------------- */
.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-2);
}
.centered-page { display: grid; place-items: center; padding: var(--gutter); }
.message-card { max-width: 440px; padding: 36px; display: grid; gap: 12px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  height: 24px; padding: 0 9px;
  border-radius: var(--r-pill);
  font-size: var(--text-xs); font-weight: 500;
  background: var(--paper-3); color: var(--ink-2);
}
.chip.on { background: var(--ok-soft); color: var(--ok-ink); }
.chip.off { background: var(--paper-2); color: var(--ink-3); text-decoration: line-through; text-decoration-color: var(--ink-3); }
.chip.warn { background: var(--warn-soft); color: var(--warn-ink); }
.chip svg { width: 12px; height: 12px; }
.chip.info { background: var(--accent-soft); color: var(--accent-hover); }

/* ---- Modal ----------------------------------------------------------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 60;
  display: grid; place-items: center;
  padding: 16px;
  background: oklch(20% 0.02 60 / 0.35);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: fade-in var(--dur) var(--ease);
}
.modal {
  width: min(100%, var(--modal-w, 480px));
  max-height: calc(100dvh - 32px);
  display: flex; flex-direction: column;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-3);
  animation: pop-in var(--dur-slow) var(--ease-spring);
  overflow: hidden;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 20px 24px 0; }
.modal-head h2 { font-family: var(--font-display); font-weight: 500; font-size: var(--text-xl); }
.modal-body { padding: 16px 24px; display: grid; gap: 14px; overflow: auto; min-height: 0; }
.modal-head, .modal-foot { flex: none; }
.modal-body p { color: var(--ink-2); }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 24px 20px; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }

/* ---- Toasts ---------------------------------------------------------- */
.toasts {
  position: fixed; left: 50%; bottom: 22px; z-index: 80;
  display: grid; gap: 8px; justify-items: center;
  transform: translateX(-50%);
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: inline-flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  border-radius: var(--r-md);
  background: var(--ink);
  color: var(--paper);
  font-size: var(--text-sm);
  box-shadow: var(--shadow-2);
  animation: toast-in var(--dur-slow) var(--ease-spring);
}
.toast.error { background: var(--danger); color: white; }
.toast.success::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: oklch(75% 0.15 150); }
.toast.leaving { animation: toast-out var(--dur) var(--ease) forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px) scale(0.96); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

/* ---- Context menu ---------------------------------------------------- */
.menu {
  position: fixed; z-index: 70;
  min-width: 200px;
  padding: 6px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-3);
  animation: pop-in var(--dur) var(--ease-spring);
}
.menu button {
  display: flex; align-items: center; gap: 10px;
  width: 100%; height: 34px; padding: 0 10px;
  border: 0; border-radius: var(--r-xs);
  background: transparent; text-align: left;
  font-size: var(--text-sm);
}
.menu button svg { width: 16px; height: 16px; color: var(--ink-2); }
.menu button:hover, .menu button:focus-visible { background: var(--paper-3); outline: none; }
.menu button.danger { color: var(--danger); }
.menu button.danger svg { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; }

/* ---- Empty state ----------------------------------------------------- */
.empty { display: grid; justify-items: center; gap: 8px; padding: 56px 20px; text-align: center; color: var(--ink-2); }
.empty h2 { font-family: var(--font-display); font-weight: 500; font-size: var(--text-xl); color: var(--ink); }
.empty-art {
  width: 84px; height: 64px; margin-bottom: 10px;
  border-radius: 10px 14px 14px 14px;
  background: linear-gradient(160deg, var(--paper-2), var(--paper-4));
  border: 1px solid var(--line-strong);
  position: relative;
}
.empty-art::before { content: ""; position: absolute; left: -1px; top: -9px; width: 38px; height: 12px; border-radius: 6px 8px 0 0; background: var(--paper-4); border: 1px solid var(--line-strong); border-bottom: 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
