:root {
  --bg: #f4f1ec;
  --card: #fffdf9;
  --ink: #241f1b;
  --muted: #7a7168;
  --line: #e2dacd;
  --accent: #7b1e1e;
  --accent-2: #a8432a;
  --class-hl: #fbf3e4;
  --good: #2e6d4f;
  --warn: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141109; --card: #2d2822; --ink: #efe8dd; --muted: #b0a595;
    --line: #40372c; --accent: #d98a3a; --accent-2: #e0a35c; --class-hl: #37301f;
    --shadow: 0 1px 3px rgba(0,0,0,.5);
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 20px; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 26px; }
.topbar h1 { font-size: 18px; margin: 0; }
.tagline { margin: 0; font-size: 12px; color: var(--muted); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar select, .toolbar button {
  font: inherit; padding: 7px 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); cursor: pointer;
}
.toolbar button:hover { border-color: var(--accent); }
.toolbar button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.toolbar select { min-width: 190px; }

/* layout */
.layout {
  display: grid; grid-template-columns: minmax(340px, 1fr) minmax(360px, 1.15fr);
  gap: 18px; padding: 18px; max-width: 1240px; margin: 0 auto; align-items: start;
}
@media (max-width: 860px) { .layout { grid-template-columns: 1fr; } }
.col { display: flex; flex-direction: column; gap: 18px; }

/* cards */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  border-top: 3px solid var(--card-accent, var(--accent));
  padding: 16px; box-shadow: var(--shadow);
}
.card h2 {
  font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--card-accent, var(--accent));
  margin: 0 0 12px; font-weight: 700;
}
/* Distinct accent per card so they're easy to tell apart */
.inputs .card:nth-of-type(1) { --card-accent: #4f6bd6; } /* Identity */
.inputs .card:nth-of-type(2) { --card-accent: #2e9e7a; } /* Ability Scores */
.inputs .card:nth-of-type(3) { --card-accent: #6b7686; } /* Armor & Shield */
.inputs .card:nth-of-type(4) { --card-accent: #a8432a; } /* Weapons */
.inputs .card:nth-of-type(5) { --card-accent: #7a4fbf; } /* Feats */
.inputs .card:nth-of-type(6) { --card-accent: #2b8a8a; } /* Traits */
.sheet .card:nth-of-type(1)  { --card-accent: #7b1e1e; } /* name header */
.sheet .card:nth-of-type(2)  { --card-accent: #2f6fb0; } /* Defense */
.sheet .card:nth-of-type(3)  { --card-accent: #b5471f; } /* Offense */
.sheet .card:nth-of-type(4)  { --card-accent: #b8860b; } /* Skills */

/* Identity portrait + re-roll */
.identity-top { display: flex; gap: 14px; align-items: stretch; margin-bottom: 10px; }
.identity-fields { flex: 1; grid-template-columns: 1fr; align-content: start; }
.portrait {
  position: relative; width: 96px; min-height: 96px; flex: none;
  border: 1px dashed var(--line); border-radius: 10px; background: var(--bg);
  display: flex; align-items: center; justify-content: center; cursor: pointer; overflow: hidden;
}
.portrait:hover { border-color: var(--card-accent, var(--accent)); }
.portrait img { width: 100%; height: 100%; object-fit: cover; }
.portrait-placeholder { font-size: 11px; color: var(--muted); text-align: center; padding: 4px; }
.portrait-clear {
  position: absolute; top: 4px; right: 4px; width: 18px; height: 18px; border: none;
  border-radius: 50%; background: rgba(0,0,0,.55); color: #fff; font-size: 11px; line-height: 1; cursor: pointer;
}
.btn-reroll {
  font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink); cursor: pointer;
}
.btn-reroll:hover { border-color: var(--card-accent, var(--accent)); }
.card h2 small { text-transform: none; letter-spacing: 0; color: var(--muted); font-weight: 400; font-size: 11px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

/* fields */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
@media (max-width: 480px) { .grid2 { grid-template-columns: 1fr; } }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.field input, .field select {
  font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink); width: 100%;
}
.note { font-size: 12px; color: var(--muted); margin: 8px 0 0; }

/* ability scores */
.abil-method { font: inherit; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.ability-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 12px; }
.ability-controls:empty { display: none; }
.pb-budget { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.pb-counter { font-size: 13px; }
.pb-counter.over { color: var(--warn); font-weight: 700; }
.pb-counter.exact { color: var(--good); font-weight: 700; }
.roll-pool { font-size: 13px; color: var(--muted); }

.ability-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.ability-row { display: grid; grid-template-columns: 88px 96px 1fr; align-items: center; gap: 10px; }
.ability-row > label { font-size: 13px; font-weight: 600; }
.ab-input { width: 100%; text-align: center; font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.ab-step-wrap { display: flex; align-items: center; gap: 4px; }
.ab-step { width: 26px; height: 28px; flex: none; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink); cursor: pointer; font-size: 15px; line-height: 1; }
.ab-step:hover { border-color: var(--card-accent, var(--accent)); }
.ab-val { flex: 1; text-align: center; font-weight: 600; }
.ab-assign { width: 100%; font: inherit; padding: 6px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }

.ability-out { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.ab-total { font-size: 15px; font-weight: 600; white-space: nowrap; }
.ab-total b { color: var(--card-accent, var(--accent)); }
.ab-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { font-size: 10px; padding: 1px 6px; border-radius: 999px; border: 1px solid var(--line); white-space: nowrap; color: var(--muted); }
.chip.pos { color: var(--good); border-color: color-mix(in srgb, var(--good) 45%, transparent); }
.chip.neg { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); }
.chip.suppressed { color: var(--muted); text-decoration: line-through; opacity: .6; }

.ability-mods-head { display: flex; align-items: center; justify-content: space-between; margin: 14px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.btn-add-mod { font: inherit; font-size: 12px; padding: 4px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink); cursor: pointer; }
.btn-add-mod:hover { border-color: var(--card-accent, var(--accent)); }
.ability-mods { display: flex; flex-direction: column; gap: 6px; }
.mod-row { display: grid; grid-template-columns: 58px 56px 108px 1fr 26px; gap: 6px; align-items: center; }
.mod-row select, .mod-row input { font: inherit; font-size: 12px; padding: 5px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink); width: 100%; min-width: 0; }
.mod-del { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 13px; }
.mod-del:hover { color: var(--warn); }

/* sheet head */
.sheet-head { border-left: 4px solid var(--accent); }
.sheet-head h2 { color: var(--ink); font-size: 20px; text-transform: none; letter-spacing: 0; margin-bottom: 2px; }
.sheet-sub { margin: 0; color: var(--muted); font-size: 13px; }

/* stat blocks */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(78px, 1fr)); gap: 10px; }
.stat-grid.saves { margin-top: 12px; }
.stat {
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 8px; text-align: center; display: flex; flex-direction: column; gap: 2px;
}
.stat.big { background: linear-gradient(180deg, var(--class-hl), var(--bg)); }
.stat-val { font-size: 22px; font-weight: 800; letter-spacing: -.02em; }
.stat.big .stat-val { font-size: 28px; color: var(--accent); }
.stat-label { font-size: 10px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }

/* skills */
.sk-meta { font-size: 12px; display: flex; gap: 12px; align-items: baseline; }
.sk-meta b { font-size: 14px; }
#sk-points.over { color: var(--warn); }
#sk-points.exact { color: var(--good); }
.dim { color: var(--muted); }
.skill-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.skill-table th {
  text-align: left; font-size: 10px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); padding: 4px 6px; border-bottom: 1px solid var(--line);
}
.skill-table th.num, .skill-table td.num { text-align: right; }
.skill-table td { padding: 3px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.skill-table tr.is-class .sk-name { font-weight: 700; }
.skill-table tr.is-class { background: var(--class-hl); }
.sk-total { font-weight: 700; width: 46px; }
.sk-rank { width: 54px; text-align: center; padding: 4px; font: inherit; border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--ink); }
.sk-rank.warn { border-color: var(--warn); color: var(--warn); }
.sk-breakdown { font-size: 11px; color: var(--muted); white-space: nowrap; }
.tag { font-size: 9px; text-transform: uppercase; letter-spacing: .04em; background: var(--line); color: var(--muted); padding: 1px 5px; border-radius: 4px; vertical-align: middle; }

/* weapons */
.add-weapon { font: inherit; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.weapon-list { display: flex; flex-direction: column; gap: 6px; }
.gear-row {
  display: grid; grid-template-columns: 26px 1fr auto 22px; align-items: center; gap: 8px;
  padding: 6px 8px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; font-size: 13px;
}
.gear-row.equipped { border-color: var(--card-accent, var(--accent)); background: color-mix(in srgb, var(--card-accent, var(--accent)) 9%, var(--bg)); }
.equip-toggle {
  width: 22px; height: 22px; flex: none; padding: 0; border: 1px solid var(--line); border-radius: 6px;
  background: var(--card); color: var(--good); cursor: pointer; font-size: 13px; line-height: 1;
}
.equip-toggle:hover { border-color: var(--good); }
.gear-row.equipped .equip-toggle { border-color: var(--good); background: color-mix(in srgb, var(--good) 22%, transparent); }
.w-name { font-weight: 600; }
.w-detail { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 11.5px; white-space: nowrap; }
.w-del { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 14px; }
.w-del:hover { color: var(--warn); }
.gear-subhead { margin-top: 14px; }

/* feats */
.feat-counter { font-size: 12px; font-weight: 400; color: var(--muted); }
.feat-counter.over { color: var(--warn); font-weight: 700; }
.feat-counter.exact { color: var(--good); font-weight: 700; }
.feat-list { display: flex; flex-direction: column; gap: 5px; }
.feat-row { display: grid; grid-template-columns: 1fr auto 22px; align-items: center; gap: 8px; padding: 6px 8px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; font-size: 13px; }
.feat-name { font-weight: 600; }
.feat-type { font-size: 10px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.feat-warn { color: var(--warn); }
.feat-row.unmet { border-color: color-mix(in srgb, var(--warn) 40%, var(--line)); }
.req-ok { color: var(--good); }
.req-bad { color: var(--warn); }
.req-unk { color: var(--muted); }

/* flash toast */
.flash {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  font-size: 13px; opacity: 0; pointer-events: none; transition: .25s; z-index: 50;
}
.flash.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* auth overlay */
.auth-overlay {
  position: fixed; inset: 0; z-index: 100; display: none;
  align-items: center; justify-content: center; padding: 20px;
  background: var(--bg);
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px 26px;
  display: flex; flex-direction: column; gap: 12px;
}
.auth-brand { font-size: 18px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.auth-sub { margin: 0 0 6px; color: var(--muted); font-size: 14px; }
.auth-card .field span { margin-bottom: 2px; }
.auth-card .primary {
  margin-top: 6px; padding: 10px; border: none; border-radius: 8px;
  background: var(--accent); color: #fff; font: inherit; font-weight: 600; cursor: pointer;
}
.auth-card .primary:disabled { opacity: .6; cursor: default; }
.auth-error { color: var(--warn); font-size: 13px; margin: 0; min-height: 1em; }
.auth-toggle { font-size: 13px; color: var(--muted); margin: 4px 0 0; }
.auth-toggle a { color: var(--accent); }
.auth-forgot { font-size: 12px; margin: 2px 0 0; }
.auth-forgot a { color: var(--muted); }
.auth-forgot a:hover { color: var(--accent); }
.auth-fineprint { font-size: 11px; color: var(--muted); margin: 8px 0 0; text-align: center; }

/* modal */
.modal { position: fixed; inset: 0; z-index: 90; display: flex; align-items: center; justify-content: center; padding: 20px; background: rgba(0,0,0,.5); }
.modal[hidden] { display: none; }
.modal-card { width: 100%; max-width: 560px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; max-height: 85vh; overflow: auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h2 { margin: 0; font-size: 16px; color: var(--ink); }
.icon-btn { border: none; background: none; font-size: 18px; color: var(--muted); cursor: pointer; }
.admin-add { display: flex; gap: 8px; margin-bottom: 8px; }
.admin-add input { flex: 1; font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.admin-add .primary { border: none; border-radius: 8px; background: var(--accent); color: #fff; padding: 0 14px; cursor: pointer; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 8px; }
.admin-table th { text-align: left; font-size: 10px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 4px 6px; border-bottom: 1px solid var(--line); }
.admin-table td { padding: 6px; border-bottom: 1px solid var(--line); }
.admin-actions { display: flex; gap: 6px; justify-content: flex-end; }
.admin-actions button { font: inherit; font-size: 12px; padding: 4px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink); cursor: pointer; }
.admin-actions button.danger:hover { border-color: var(--warn); color: var(--warn); }
.banned { color: var(--warn); font-weight: 600; }

/* custom dropdown (race/class/alignment/deity) with hoverable options */
.cs { position: relative; }
.cs-btn {
  width: 100%; text-align: left; font: inherit; padding: 8px 26px 8px 10px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); cursor: pointer;
}
.cs-btn::after { content: '▾'; position: absolute; right: 9px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 11px; pointer-events: none; }
.cs.open .cs-btn { border-color: var(--card-accent, var(--accent)); }
.cs-panel {
  position: absolute; z-index: 60; top: calc(100% + 4px); left: 0; right: auto; min-width: 100%;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.22); padding: 4px;
}
.cs-panel[hidden] { display: none; }
.cs-search {
  width: 100%; box-sizing: border-box; font: inherit; padding: 7px 9px; margin-bottom: 4px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink);
}
.cs-list { max-height: 220px; overflow-y: auto; }
.cs-add { min-width: 150px; }
.cs-add > .cs-btn { font-size: 12px; padding: 6px 22px 6px 9px; color: var(--muted); }
.cs-add .cs-panel { min-width: 230px; right: 0; left: auto; }
.cs-option { padding: 7px 9px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.cs-option:hover { background: color-mix(in srgb, var(--card-accent, var(--accent)) 16%, transparent); }
.cs-option.selected { font-weight: 600; color: var(--card-accent, var(--accent)); }

/* info popover */
.info-pop {
  position: absolute; z-index: 200; max-width: 300px;
  background: var(--card); color: var(--ink); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18); padding: 11px 13px; font-size: 13px; line-height: 1.5;
}
.info-pop[hidden] { display: none; }
.info-title { font-weight: 700; margin-bottom: 3px; color: var(--accent); }
.info-body p { margin: 6px 0 0; }
.info-meta { display: block; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.info-calc { margin-top: 8px; padding-top: 6px; border-top: 1px dashed var(--line); font-size: 12px; color: var(--ink); font-variant-numeric: tabular-nums; }
[data-info] { cursor: help; }
button[data-info] { cursor: pointer; }

@media print {
  .topbar, .inputs, .toolbar, .flash { display: none !important; }
  .layout { grid-template-columns: 1fr; }
  body { background: #fff; }
  .card { box-shadow: none; border-color: #ccc; break-inside: avoid; }
}
