/* ── Tracely shell — tokens + chrome. Tab modules style their own DOM under
     a namespaced root class and reuse these primitives.
     Identity: paper-and-flame. Warm paper surfaces, one confident orange,
     serif for document text, sans for chrome. 8px spacing grid.
     Type scale: 12.5 / 13.5 / 15 / 18 / 24 / 32. ─────────────────────────── */
:root {
  --bg: #faf8f5;
  --bg-raised: #ffffff;
  --bg-panel: #fdfbf8;
  --line: #eae5dd;
  --line-strong: #ddd6ca;
  --ink: #23201a;
  --ink-dim: #6f685c;
  --ink-faint: #a39a8a;
  --accent: #f97316;
  --accent-deep: #ea580c;
  --accent-soft: #fff1e6;

  /* The mark system's vocabulary — three colours plus pending. Source of truth
     for the *decision* is /shared/marks.js; these tokens only paint it. */
  --mark-red: #d93636;
  --mark-amber: #ffb800;
  --mark-orange: #ff5900;
  --mark-grey: #9a9ba1;

  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;

  /* type scale */
  --fs-xs: 12.5px;
  --fs-sm: 13.5px;
  --fs-md: 15px;
  --fs-lg: 18px;
  --fs-xl: 24px;
  --fs-2xl: 32px;

  /* 8px spacing grid */
  --s-1: 8px;
  --s-2: 16px;
  --s-3: 24px;
  --s-4: 32px;
  --s-5: 40px;
  --s-6: 48px;

  /* layered shadows: tight crisp + wide soft */
  --shadow: 0 1px 2px #1c150d14, 0 6px 24px #1c150d0f;
  --shadow-lift: 0 2px 4px #1c150d1a, 0 10px 32px #1c150d14;
  --shadow-pop: 0 4px 12px #1c150d24, 0 24px 70px #1c150d2e;

  --radius: 13px;
  --radius-sm: 8px;

  /* motion */
  --t-fast: 150ms;
  --t-med: 200ms;
  --ease: cubic-bezier(.3, .7, .4, 1);
}

/* Dark palette — decided here, once. Everything paints through var(--…). */
html[data-theme="dark"] {
  --bg: #161310;
  --bg-raised: #1e1a15;
  --bg-panel: #1a1712;
  --line: #2c2721;
  --line-strong: #3a342b;
  --ink: #ece7dc;
  --ink-dim: #a89f8f;
  --ink-faint: #6e675c;
  --accent: #fb8c3c;
  --accent-deep: #f97316;
  --accent-soft: #2a2018;
  --mark-red: #ff5f52;
  --mark-amber: #ffc233;
  --mark-orange: #ff7433;
  --mark-grey: #8a8b90;
  --shadow: 0 1px 2px #00000040, 0 6px 24px #00000050;
  --shadow-lift: 0 2px 4px #00000052, 0 10px 32px #00000060;
  --shadow-pop: 0 4px 12px #00000066, 0 24px 70px #00000073;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  color-scheme: light;
  font: var(--fs-sm)/1.55 var(--sans);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
html[data-theme="dark"] body { color-scheme: dark; }
.hidden { display: none !important; }

h1, h2, h3 { line-height: 1.15; }

/* ── selection + thin scrollbars ── */
::selection {
  background: color-mix(in srgb, var(--accent) 26%, transparent);
  color: var(--ink);
}
* { scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--line-strong);
  border-radius: 8px;
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--ink-faint); }
::-webkit-scrollbar-corner { background: transparent; }

/* ── focus rings in the accent ── */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}
.btn:focus-visible, .input:focus-visible, .tabs button:focus-visible { outline-offset: 1px; }

/* ── app frame ── */
.app { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.view { flex: 1; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }

/* subtle crossfade as a tab's content mounts */
@keyframes view-enter {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}
.view > * { animation: view-enter var(--t-med) var(--ease) both; }

/* fade-slide for list items / marks — modules get the stagger for free on cards */
@keyframes fade-slide {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
.view .card { animation: fade-slide var(--t-med) var(--ease) backwards; }
.view .card:nth-child(2) { animation-delay: 80ms; }
.view .card:nth-child(3) { animation-delay: 160ms; }
.view .card:nth-child(4) { animation-delay: 240ms; }
.view .card:nth-child(5) { animation-delay: 320ms; }
.view .card:nth-child(n+6) { animation-delay: 400ms; }

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

/* ── topbar + tabs ── */
.topbar {
  display: flex; align-items: center; gap: var(--s-3);
  padding: 12px var(--s-3);
  border-bottom: 1px solid var(--line);
  background: var(--bg-raised);
  box-shadow: var(--shadow);
  z-index: 20;
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.brand-mark {
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-deep));
  width: 32px; height: 32px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px;
  box-shadow: 0 1px 2px color-mix(in srgb, var(--accent-deep) 35%, transparent),
              0 4px 12px color-mix(in srgb, var(--accent) 30%, transparent);
  transition: transform var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.brand:hover .brand-mark { transform: rotate(-6deg) scale(1.04); }
.brand-mark svg { width: 16px; height: 16px; }
.brand-name { font-family: var(--serif); font-size: 21px; font-weight: 700; letter-spacing: -.2px; }

.tabs { display: flex; gap: 2px; }
.tabs button {
  background: none; border: none; cursor: pointer;
  font: var(--fs-sm) var(--sans); color: var(--ink-dim);
  padding: 8px 16px; border-radius: var(--radius-sm);
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease),
              transform var(--t-fast) var(--ease);
}
.tabs button:hover { background: var(--bg); color: var(--ink); }
.tabs button:active { transform: translateY(1px); }
.tabs button.active { background: var(--accent-soft); color: var(--accent-deep); font-weight: 600; }
.top-status {
  margin-left: auto; font-size: 12px; color: var(--ink-faint);
  display: flex; align-items: center; gap: 12px; min-width: 0; white-space: nowrap;
}
.usage-meter {
  font-family: var(--mono); font-size: 11.5px; letter-spacing: .1px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-faint);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  cursor: default;
  transition: color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
  animation: fade-slide var(--t-med) var(--ease);
}
.usage-meter:hover { color: var(--ink-dim); border-color: var(--line-strong); }
.usage-meter b { font-weight: 600; color: var(--ink-dim); }

/* ── shared primitives for tab modules ── */
.btn {
  border: 1px solid var(--line-strong); background: var(--bg-raised); color: var(--ink);
  border-radius: var(--radius-sm); padding: 7px 14px; font-size: var(--fs-sm); cursor: pointer;
  transition: border-color var(--t-fast) var(--ease), background var(--t-fast) var(--ease),
              color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease),
              transform var(--t-fast) var(--ease);
}
.btn:hover {
  border-color: var(--accent); color: var(--accent-deep);
  box-shadow: 0 1px 2px color-mix(in srgb, var(--accent) 14%, transparent);
}
.btn:active { transform: translateY(1px); box-shadow: none; }
.btn-primary {
  background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600;
  box-shadow: 0 1px 2px color-mix(in srgb, var(--accent-deep) 30%, transparent),
              0 4px 14px color-mix(in srgb, var(--accent) 25%, transparent);
}
.btn-primary:hover {
  background: var(--accent-deep); border-color: var(--accent-deep); color: #fff;
  box-shadow: 0 1px 2px color-mix(in srgb, var(--accent-deep) 38%, transparent),
              0 6px 18px color-mix(in srgb, var(--accent) 32%, transparent);
}
.btn-primary:active { box-shadow: 0 1px 2px color-mix(in srgb, var(--accent-deep) 30%, transparent); }
.btn-ghost { background: transparent; }
.btn[disabled] { opacity: .45; cursor: default; transform: none; box-shadow: none; }
.btn[disabled]:hover { border-color: var(--line-strong); color: var(--ink); }

.card {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  transition: box-shadow var(--t-med) var(--ease), transform var(--t-med) var(--ease),
              border-color var(--t-med) var(--ease);
}
.card:hover { box-shadow: var(--shadow-lift); transform: translateY(-1px); }

.input, textarea.input, select.input {
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  padding: 7px 12px; font-size: var(--fs-sm); background: var(--bg-raised); color: var(--ink);
  outline: none;
  transition: border-color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.input::placeholder { color: var(--ink-faint); }
.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
.eyebrow { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 1.2px; color: var(--ink-faint); }

.grade-chip {
  font-family: var(--mono); font-weight: 700;
  border-radius: var(--radius-sm); padding: 2px 9px; font-size: var(--fs-sm);
  background: var(--accent-soft); color: var(--accent-deep);
  transition: background var(--t-fast) var(--ease);
}

/* ── key banner ── */
.key-banner {
  background: var(--accent-soft);
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 28%, var(--line));
  padding: 12px var(--s-3);
  animation: fade-slide var(--t-med) var(--ease);
}
.key-banner-inner { display: flex; gap: 12px; align-items: center; max-width: 900px; }
.key-icon { font-size: 22px; }
.key-detail { color: var(--ink-dim); margin-top: 2px; font-size: var(--fs-sm); }
.key-detail code {
  font-family: var(--mono); color: var(--accent-deep);
  background: color-mix(in srgb, var(--bg-raised) 70%, transparent);
  padding: 1px 5px; border-radius: 4px;
  border: 1px solid color-mix(in srgb, var(--accent) 28%, var(--line));
}

/* ── tracer dock (bottom-left tutor panel) ── */
.tracer-dock { position: fixed; left: var(--s-2) ; bottom: var(--s-2); z-index: 60; }

/* ── modal root ── */
#modalRoot .modal-backdrop {
  position: fixed; inset: 0;
  background: color-mix(in srgb, var(--ink) 24%, transparent);
  backdrop-filter: blur(2px);
  z-index: 80;
  display: flex; align-items: center; justify-content: center;
  animation: backdrop-in var(--t-med) var(--ease);
}
@keyframes backdrop-in { from { opacity: 0; } to { opacity: 1; } }
#modalRoot .modal {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: var(--shadow-pop);
  max-width: 720px; width: calc(100vw - 60px); max-height: 84vh; overflow-y: auto;
  animation: modal-in var(--t-med) var(--ease);
}
@keyframes modal-in {
  from { opacity: 0; transform: translateY(10px) scale(.985); }
  to { opacity: 1; transform: none; }
}

/* ── toasts ── */
.toasts { position: fixed; bottom: var(--s-3); left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: var(--s-1); z-index: 100; }
.toast {
  background: var(--bg-raised); border: 1px solid var(--line-strong); border-radius: 11px;
  padding: 10px 16px; font-size: var(--fs-sm); color: var(--ink);
  box-shadow: var(--shadow-lift);
  animation: rise .25s var(--ease);
}
.toast.err {
  border-color: color-mix(in srgb, var(--mark-red) 40%, var(--line-strong));
  background: color-mix(in srgb, var(--mark-red) 7%, var(--bg-raised));
}
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ── polished empty states — the paper-plane motif ── */
.empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: var(--s-1); padding: var(--s-6) var(--s-3); text-align: center;
  color: var(--ink-dim); max-width: 420px; margin: 0 auto;
}
.empty-state .empty-plane {
  width: 44px; height: 44px; color: var(--ink-faint); opacity: .8;
  margin-bottom: var(--s-1);
}
.empty-state h3 { font-family: var(--serif); font-size: var(--fs-lg); color: var(--ink); font-weight: 600; }
.empty-state p { font-size: var(--fs-sm); color: var(--ink-dim); line-height: 1.55; }
