/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --bg: #edf0f4;
  --surface: #ffffff;
  --surface-2: #f5f7fa;
  --ink: #1b2130;
  --muted: #586173;
  --line: #d5dbe4;
  --rule: #dbe5f4;
  --accent: #c8372d;
  --accent-hover: #ae2c23;
  --accent-ink: #ffffff;
  --accent-soft: #f9e4e2;
  --focus: #2f6fdb;
  --ok: #1f7a4d;
  --warn-bg: #fff4dc;
  --warn-ink: #7a4d00;
  --ok-bg: #e1f2e8;
  --stage: #e3e8ef;
  --bezel: #0e1116;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 6px 18px rgba(20, 30, 50, .07);

  --f-display: "Archivo Narrow", "Arial Narrow", "Roboto Condensed", system-ui, sans-serif;
  --f-body: "IBM Plex Sans", "Segoe UI", system-ui, -apple-system, sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;

  --radius: 4px;
  --ruled: 28px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #12151b;
    --surface: #1b2029;
    --surface-2: #222833;
    --ink: #e7eaf0;
    --muted: #9ba4b4;
    --line: #2f3643;
    --rule: #233147;
    --accent: #ee6a5f;
    --accent-hover: #f5857b;
    --accent-ink: #1a0e0c;
    --accent-soft: #3a2220;
    --focus: #7aa7ff;
    --ok: #5cc08b;
    --warn-bg: #3a2e12;
    --warn-ink: #f0cf85;
    --ok-bg: #173325;
    --stage: #0d1015;
    --bezel: #3a414e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .25);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #12151b;
  --surface: #1b2029;
  --surface-2: #222833;
  --ink: #e7eaf0;
  --muted: #9ba4b4;
  --line: #2f3643;
  --rule: #233147;
  --accent: #ee6a5f;
  --accent-hover: #f5857b;
  --accent-ink: #1a0e0c;
  --accent-soft: #3a2220;
  --focus: #7aa7ff;
  --ok: #5cc08b;
  --warn-bg: #3a2e12;
  --warn-ink: #f0cf85;
  --ok-bg: #173325;
  --stage: #0d1015;
  --bezel: #3a414e;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .25);
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.55 var(--f-body);
  min-height: 100vh;
}
img { max-width: 100%; }
a { color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 2px; }
h1, h2, h3 { font-family: var(--f-display); font-weight: 700; line-height: 1.1; margin: 0; text-wrap: balance; letter-spacing: .005em; }
h1 { font-size: clamp(30px, 4.5vw, 44px); }
h2 { font-size: 26px; }
h3 { font-size: 19px; }
p { margin: 0; }

.wrap { width: 100%; max-width: 1180px; margin-inline: auto; padding-inline: 20px; }
@media (max-width: 480px) { .wrap { padding-inline: 16px; } }

.eyebrow, .meta {
  font-family: var(--f-mono);
  font-size: 12px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
}
.muted { color: var(--muted); }
.loading { padding-block: 60px; color: var(--muted); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 60px; }
.brand { display: inline-flex; align-items: center; gap: 10px; text-decoration: none; min-width: 0; }
.brand-mark { width: 30px; height: 22px; flex: none; }
.bm-card { fill: var(--surface); stroke: var(--ink); stroke-width: 1.5; }
.bm-red { stroke: var(--accent); stroke-width: 1.5; }
.bm-blue { stroke: #8fb0de; stroke-width: 1.2; }
.brand-name { font-family: var(--f-display); font-weight: 700; font-size: 21px; letter-spacing: .01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav { display: flex; align-items: center; justify-content: flex-end; }
.nav-links { display: flex; align-items: center; gap: 4px; }
.nav a, .nav button { font-size: 14px; }
.nav .nav-toggle { display: none; }
.badge {
  display: inline-grid; place-items: center;
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px;
  background: var(--accent); color: var(--accent-ink);
  font: 600 11px/1 var(--f-body);
  font-variant-numeric: tabular-nums;
}

/* ---------- buttons & fields ---------- */
.btn {
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 38px;
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: 500 14px/1.2 var(--f-body);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); border-color: transparent; }
.btn-danger { color: var(--accent); }
.btn-danger-solid { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-sm { min-height: 32px; padding: 5px 10px; font-size: 13px; }
.btn[disabled] { opacity: .55; cursor: default; }

.field { display: grid; gap: 6px; align-content: start; }
.field > span { font-weight: 500; font-size: 14px; }
.field small { color: var(--muted); font-size: 13px; }
input[type="text"], input[type="url"], input[type="password"], input[type="search"], textarea, select {
  width: 100%;
  font: 400 15px/1.4 var(--f-body);
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 11px;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: transparent; }
textarea.code {
  font: 400 13px/1.5 var(--f-mono);
  min-height: 160px;
  resize: vertical;
  tab-size: 2;
  white-space: pre;
  overflow-wrap: normal;
  overflow-x: auto;
}
select { width: auto; padding-right: 30px; }

.error { color: var(--accent); font-weight: 500; }
.notice { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 12px; border-radius: var(--radius); font-size: 13px; }

/* ---------- directory ---------- */
.intro { display: grid; gap: 14px; padding-block: 40px 28px; max-width: 720px; }
.intro p { color: var(--muted); font-size: 16px; max-width: 62ch; }
.intro-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 4px; }

.filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px;
  padding-block: 14px;
  border-top: 1px solid var(--line);
}
.search { position: relative; flex: 1 1 280px; max-width: 460px; }
.search input { padding-left: 36px; }
.search svg { position: absolute; left: 11px; top: 50%; translate: 0 -50%; width: 16px; height: 16px; color: var(--muted); pointer-events: none; }
.count { margin-left: auto; font-variant-numeric: tabular-nums; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 18px;
  padding-block: 8px 60px;
}

/* index card */
.icard {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto 1fr auto;
  column-gap: 14px;
  background-color: var(--surface);
  border-radius: 3px;
  box-shadow: var(--shadow);
  padding: 0 16px 14px;
  min-height: 176px;
  transition: translate .15s ease, box-shadow .15s ease;
}
.icard:hover { translate: 0 -2px; }
.icard-head {
  grid-column: 1 / -1;
  padding-block: 14px 8px;
  border-bottom: 2px solid var(--accent);
}
.icard-name { font-family: var(--f-display); font-weight: 700; font-size: 22px; line-height: 1.15; text-decoration: none; overflow-wrap: anywhere; }
.icard-name::after { content: ""; position: absolute; inset: 0; border-radius: inherit; }
.icard-name:focus-visible { outline: none; }
.icard:has(.icard-name:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.icard-fics {
  list-style: none; margin: 0; padding: 0;
  line-height: var(--ruled);
  background-image: repeating-linear-gradient(to bottom, transparent 0 calc(var(--ruled) - 1px), var(--rule) calc(var(--ruled) - 1px) var(--ruled));
  min-height: calc(var(--ruled) * 3);
  padding-top: 2px;
}
.icard-fics li { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.icard-thumb {
  grid-column: 2; grid-row: 2 / 4;
  width: 64px; height: 92px;
  margin-top: 12px;
  object-fit: cover;
  border-radius: 2px;
  background: var(--surface-2);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .18);
  rotate: 2deg;
}
.icard-meta { grid-column: 1; padding-top: 10px; }

.empty {
  display: grid; gap: 12px; justify-items: start;
  padding: 36px 24px;
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  margin-block: 8px 60px;
}

/* ---------- profile ---------- */
.back { display: inline-flex; margin-top: 22px; text-decoration: none; color: var(--muted); font-size: 14px; }
.back:hover { color: var(--ink); }
.phead { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 16px; padding-block: 12px 22px; }
.phead-text { display: grid; gap: 8px; min-width: 0; }
.phead h1 { overflow-wrap: anywhere; }
.phead-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.viewbar {
  position: sticky;
  top: calc(env(safe-area-inset-top, 0px) + 61px);
  z-index: 10;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 10px 0;
  background: var(--bg);
  border-block: 1px solid var(--line);
}
.viewbar .meta { margin-right: 2px; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 2px; }
.seg button {
  appearance: none; border: 0; background: transparent; color: var(--muted);
  font: 500 13px/1 var(--f-body);
  padding: 7px 11px; border-radius: 3px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--bg); }
.seg svg { width: 14px; height: 14px; }

.fic { padding-block: 30px 10px; display: grid; gap: 18px; }
.fic + .fic { border-top: 1px solid var(--line); }
.fic-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px; }
.fic-head h2 { overflow-wrap: anywhere; }
.fic-link { font-size: 14px; color: var(--accent); text-decoration: none; font-weight: 500; }
.fic-link:hover { text-decoration: underline; }

.code { display: grid; gap: 10px; }
.code-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.code-label { font-family: var(--f-mono); font-size: 13px; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.code-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.code-src {
  margin: 0;
  max-height: 320px;
  overflow: auto;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: 400 12.5px/1.5 var(--f-mono);
  white-space: pre;
}

/* ---------- preview stage ---------- */
.pv-stage {
  background: var(--stage);
  border-radius: var(--radius);
  padding: 18px;
  display: grid;
  justify-items: center;
  gap: 8px;
  overflow: hidden;
}
.pv-cap { font-family: var(--f-mono); font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.pv-holder { position: relative; overflow: hidden; border-radius: 3px; box-shadow: 0 2px 10px rgba(0, 0, 0, .18); max-width: 100%; }
.pv-holder.phone { border-radius: 22px; border: 8px solid var(--bezel); box-shadow: 0 4px 18px rgba(0, 0, 0, .28); }
.pv-holder iframe { display: block; border: 0; transform-origin: top left; background: #fff; }

/* ---------- forms / editor ---------- */
.form-page { display: grid; gap: 28px; padding-block: 34px 120px; max-width: 900px; }
.form-page.narrow { max-width: 440px; }
.panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  display: grid;
  gap: 16px;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; border-bottom: 2px solid var(--accent); padding-bottom: 10px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 640px) { .row2 { grid-template-columns: 1fr; } .panel { padding: 16px; } }

.stack { display: grid; gap: 22px; }
.stack-sm { gap: 12px; }
.form-page > .back { margin-top: 0; }

.code-edit { display: grid; gap: 10px; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-2); }
.code-edit-head { display: flex; gap: 10px; align-items: end; }
.code-edit-head .field { flex: 1; }

.savebar {
  position: sticky;
  bottom: 0;
  z-index: 15;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 14px 0 calc(14px + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.savebar .error { flex: 1 1 240px; }

.danger { display: grid; gap: 10px; justify-items: start; padding: 18px; border: 1px solid var(--accent-soft); border-radius: var(--radius); }
.confirm-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.help { font-size: 14px; color: var(--muted); }
.help code { font-family: var(--f-mono); font-size: 12.5px; }

/* ---------- calendar ---------- */
.intro-tight { padding-block: 34px 18px; }
.cal-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px;
  padding-block: 12px;
  border-top: 1px solid var(--line);
}
.cal-title { font-size: 24px; min-width: 9.5ch; text-align: center; }
.cal-legend { margin-left: auto; text-transform: none; letter-spacing: .02em; }
.cal-body { display: grid; gap: 16px; padding-block: 6px 70px; }
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.cal-head {
  background: var(--surface-2);
  padding: 8px 10px;
  font: 500 11px/1 var(--f-mono); letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
}
.cal-cell {
  background: var(--surface);
  min-height: 104px;
  padding: 6px 8px 8px;
  display: flex; flex-direction: column; gap: 4px;
  min-width: 0;
}
.cal-cell.is-out { background: var(--surface-2); }
.cal-cell.is-past { background: color-mix(in srgb, var(--surface) 70%, var(--surface-2)); }
.cal-cell.is-past .cal-num { color: var(--muted); }
.cal-cell.is-today { box-shadow: inset 0 0 0 2px var(--accent); }
.cal-num { font: 600 13px/1.2 var(--f-mono); font-variant-numeric: tabular-nums; }
.cal-cell.is-today .cal-num { color: var(--accent); }
.cal-ev {
  font-size: 12.5px; line-height: 1.35;
  padding: 3px 6px;
  border-radius: 3px;
  background: var(--accent-soft);
  overflow-wrap: anywhere;
}
.cal-ev a { text-decoration: none; font-weight: 500; }
.cal-ev a:hover { text-decoration: underline; }
.cal-arrow { color: var(--accent); }
.agenda { display: none; }
.agenda-day { display: grid; gap: 4px; padding: 12px 14px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.agenda-day.is-past { opacity: .7; }
.agenda-day.is-today { box-shadow: inset 0 0 0 2px var(--accent); }
.agenda-day h3 { display: flex; align-items: center; gap: 8px; font-size: 17px; }
.agenda-day p { font-size: 14.5px; }
.cal-empty { padding-top: 4px; }
@media (max-width: 720px) {
  .cal-scroll { display: none; }
  .agenda { display: grid; gap: 10px; }
  .cal-legend { display: none; }
  .cal-title { font-size: 21px; }
}

/* ---------- swaps ---------- */
.pill {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  font: 500 11px/1.2 var(--f-mono); letter-spacing: .06em; text-transform: uppercase;
  background: var(--surface-2); color: var(--muted);
}
.pill-pending { background: var(--warn-bg); color: var(--warn-ink); }
.pill-accepted { background: var(--ok-bg); color: var(--ok); }
.swap-section { display: grid; gap: 12px; padding-block: 22px 6px; border-top: 1px solid var(--line); }
.swap-section:last-child { padding-bottom: 70px; }
.swap-section h2 { display: flex; align-items: center; gap: 10px; font-size: 22px; }
.count-chip { font: 600 12px/1 var(--f-mono); padding: 4px 8px; border-radius: 999px; background: var(--ink); color: var(--bg); }
.swap-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
@media (max-width: 400px) { .swap-list { grid-template-columns: 1fr; } }
.swap {
  display: grid; gap: 10px; align-content: start;
  padding: 16px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.swap.is-muted { box-shadow: none; border: 1px solid var(--line); background: transparent; }
.swap-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.swap-who { font-weight: 600; }
.swap-who a { color: inherit; }
.swap-lines { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.swap-lines li { display: grid; grid-template-columns: 96px 1fr; gap: 10px; align-items: baseline; font-size: 14.5px; }
.swap-date { font: 500 12px/1.3 var(--f-mono); color: var(--muted); font-variant-numeric: tabular-nums; }
.swap-note { display: grid; gap: 2px; font-size: 14px; padding: 8px 10px; background: var(--surface-2); border-radius: var(--radius); }
.swap-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.swap-side { border: 0; padding: 0; margin: 0; display: grid; gap: 10px; min-width: 0; }
.swap-side legend { padding: 0; margin-bottom: 8px; font: 700 18px/1.2 var(--f-display); }
input[type="date"] {
  width: 100%;
  font: 400 15px/1.4 var(--f-body);
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 11px;
}
select { max-width: 100%; }
.panel select { width: 100%; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  translate: -50% 0;
  z-index: 50;
  background: var(--ink);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
  max-width: calc(100vw - 32px);
}

@media (max-width: 720px) {
  .nav .nav-toggle { display: inline-flex; border-color: var(--line); }
  .nav-links {
    display: none;
    position: absolute; top: 100%; left: 16px; right: 16px;
    flex-direction: column; align-items: stretch; gap: 2px;
    margin-top: 6px; padding: 8px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
  }
  .nav.open .nav-links { display: flex; }
  .nav-links .btn { justify-content: flex-start; }
}

@media (max-width: 560px) {
  .brand { gap: 8px; }
  .brand-mark { width: 26px; height: 19px; }
  .brand-name { font-size: 18px; }
  .count { margin-left: 0; }
  .viewbar { top: calc(env(safe-area-inset-top, 0px) + 61px); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .icard:hover { translate: none; }
}
