/* Self-hosted (not linked from Google's CDN) to keep the app free of
   external runtime dependencies, matching the rest of this cluster. Latin
   subset only -- app is English-only. */
@font-face {
  font-family: "Uncial Antiqua";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("/fonts/uncial-antiqua.woff2") format("woff2");
}

@font-face {
  font-family: "Castoro Titling";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("/fonts/castoro-titling.woff2") format("woff2");
}

:root {
  --bg: #efe6d3;
  --bg-raised: #f7f0e2;
  --ink: #2b2013;
  --ink-soft: #5a4c38;
  --border: #c9b78f;
  --accent: #7a2323;
  --accent-ink: #fdf6e6;
  --danger: #7a2323;
  --danger-ink: #fdf6e6;
  --hp-good: #3d6b35;
  --hp-mid: #a3791f;
  --hp-low: #7a2323;
  --shadow: rgba(43, 32, 19, 0.18);
  --font-display: "Uncial Antiqua", cursive;
  --font-heading: "Castoro Titling", Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* Dark palette applies automatically by system preference, or explicitly
   when the header toggle sets data-theme -- see initThemeToggle() in
   app.js. The two blocks below intentionally duplicate the same values:
   one for "no explicit choice, but the system prefers dark", one for
   "explicit dark" that overrides a light system preference. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1712;
    --bg-raised: #262019;
    --ink: #ecddb8;
    --ink-soft: #b8a582;
    --border: #4a3f2e;
    --accent: #c0522f;
    --accent-ink: #1c1712;
    --danger: #c0522f;
    --danger-ink: #1c1712;
    --hp-good: #6fae5f;
    --hp-mid: #d1a53a;
    --hp-low: #d1613a;
    --shadow: rgba(0, 0, 0, 0.45);
  }
}

:root[data-theme="dark"] {
  --bg: #1c1712;
  --bg-raised: #262019;
  --ink: #ecddb8;
  --ink-soft: #b8a582;
  --border: #4a3f2e;
  --accent: #c0522f;
  --accent-ink: #1c1712;
  --danger: #c0522f;
  --danger-ink: #1c1712;
  --hp-good: #6fae5f;
  --hp-mid: #d1a53a;
  --hp-low: #d1613a;
  --shadow: rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  min-height: 100vh;
}

a { color: var(--accent); }

.site-header {
  padding: 12px 20px 0;
  background: var(--bg-raised);
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 400;
  text-decoration: none;
  color: var(--ink);
  letter-spacing: 0.03em;
}

.user-badge {
  font-size: 0.8rem;
  color: var(--ink-soft);
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.logout-form { display: contents; }

.logout-btn {
  font-size: 0.85rem;
  padding: 6px 12px;
}

.rules-trigger {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  padding: 6px 12px;
}

.rules-trigger.icon-only {
  padding: 7px;
}

.rules-trigger.icon-only .icon {
  width: 1.3em;
  height: 1.3em;
}

.theme-toggle {
  transition: transform 0.2s ease;
}

.theme-toggle:active {
  transform: rotate(15deg);
}

/* ---------- Combat rules popover ---------- */

.rules-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.rules-overlay.open {
  display: flex;
}

.rules-panel {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  max-width: 560px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 18px 22px 22px;
  box-shadow: 0 10px 40px var(--shadow);
}

.rules-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px;
  margin-bottom: 4px;
}

.rules-panel-header h2 {
  display: flex;
  align-items: center;
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 1.15rem;
  margin: 0;
}

.rules-panel-header h2 .icon {
  color: var(--accent);
  width: 1.3em;
  height: 1.3em;
  margin-right: 8px;
}

.icon-btn {
  border: none;
  background: transparent;
  color: var(--ink-soft);
  font-size: 1.4rem;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 6px;
}

.icon-btn:hover {
  color: var(--danger);
  background: var(--bg);
}

.rules-disclaimer {
  font-size: 0.82rem;
  font-style: italic;
  color: var(--ink-soft);
  margin: 10px 0 14px;
}

.rules-body h3 {
  display: flex;
  align-items: center;
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 0.95rem;
  margin: 16px 0 6px;
}

.rules-body h3:first-child {
  margin-top: 0;
}

.rules-body h3 .icon {
  color: var(--accent);
  width: 1.1em;
  height: 1.1em;
  margin-right: 6px;
}

.rules-body ul {
  margin: 0;
  padding-left: 20px;
}

.rules-body li {
  margin-bottom: 5px;
  font-size: 0.92rem;
  line-height: 1.4;
}

/* ---------- Equipment picker ---------- */

.equipment-picker-trigger {
  margin-top: 8px;
}

.equipment-search {
  width: 100%;
  font-family: var(--font-body);
  font-size: 1rem;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--ink);
  margin-top: 10px;
}

.equipment-search:focus {
  outline: none;
  border-color: var(--accent);
}

.equipment-list {
  max-height: 50vh;
  overflow-y: auto;
  margin-top: 6px;
}

.equipment-list h3 {
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--ink-soft);
  margin: 14px 0 6px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 4px;
}

.equipment-list h3:first-child {
  margin-top: 0;
}

.equipment-item {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 6px;
  padding: 8px 8px;
  font-family: var(--font-body);
  color: var(--ink);
  cursor: pointer;
}

.equipment-item:hover {
  background: var(--bg);
}

.equipment-item-name {
  font-weight: 600;
  flex-shrink: 0;
}

.equipment-item-stat {
  font-size: 0.82rem;
  color: var(--ink-soft);
  text-align: right;
}

.equipment-empty {
  color: var(--ink-soft);
  font-size: 0.9rem;
  padding: 12px 0;
  text-align: center;
}

/* ---------- Login page ---------- */

.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  gap: 20px;
}

.login-card {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 40px var(--shadow);
  padding: 32px 28px;
  max-width: 360px;
  width: 100%;
  text-align: center;
}

.login-card .crest {
  width: 64px;
  height: 64px;
  color: var(--accent);
  margin-bottom: 8px;
}

.login-card h1 {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 400;
  letter-spacing: 0.03em;
  margin: 0 0 6px;
}

.login-sub {
  color: var(--ink-soft);
  font-size: 0.88rem;
  margin: 0 0 18px;
}

.login-error {
  background: var(--danger);
  color: var(--danger-ink);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 0.85rem;
  margin-bottom: 14px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: left;
}

.login-submit {
  width: 100%;
  margin-top: 4px;
}

.login-page .ivy-divider {
  max-width: 360px;
  height: 20px;
}

.crest {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  color: var(--accent);
}

.ivy-divider {
  display: block;
  width: 100%;
  height: 22px;
  margin-top: 10px;
  color: var(--border);
}

.ivy-divider.footer-flourish {
  margin: 24px 0 0;
}

/* ---------- Icons ---------- */

.icon {
  width: 1.15em;
  height: 1.15em;
  flex-shrink: 0;
  vertical-align: -0.2em;
  color: inherit;
}

h2 .icon {
  color: var(--accent);
  width: 1.3em;
  height: 1.3em;
  margin-right: 6px;
}

.heading-icon {
  display: inline-flex;
  align-items: center;
}

.skull-badge {
  width: 16px;
  height: 16px;
  color: var(--hp-low);
  vertical-align: -3px;
  margin-left: 6px;
}

#app {
  max-width: 960px;
  margin: 0 auto;
  padding: 20px 16px 80px;
}

button {
  font-family: var(--font-body);
  cursor: pointer;
}

/* ---------- List view ---------- */

.list-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  gap: 12px;
  flex-wrap: wrap;
}

.list-toolbar h1 {
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 1.3rem;
  margin: 0;
}

.btn {
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--ink);
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 0.95rem;
}

.btn:hover { border-color: var(--accent); }

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  font-weight: 600;
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}

.btn:disabled { opacity: 0.5; cursor: default; }

.char-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.char-card {
  display: block;
  text-decoration: none;
  color: var(--ink);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  box-shadow: 0 1px 3px var(--shadow);
}

.char-card:hover { border-color: var(--accent); }

.char-card .name {
  font-family: var(--font-heading);
  font-size: 1.15rem;
  font-weight: 400;
  margin-bottom: 2px;
}

.char-card .sub {
  color: var(--ink-soft);
  font-size: 0.9rem;
  margin-bottom: 10px;
}

.hp-bar-track {
  height: 8px;
  border-radius: 4px;
  background: var(--border);
  overflow: hidden;
  margin-bottom: 4px;
}

.hp-bar-fill { height: 100%; }

.char-card .hp-label {
  font-size: 0.8rem;
  color: var(--ink-soft);
}

.empty-state {
  text-align: center;
  color: var(--ink-soft);
  padding: 40px 0;
}

.empty-state .illustration {
  width: 96px;
  height: 96px;
  color: var(--border);
  margin-bottom: 12px;
}

.char-card .name {
  display: flex;
  align-items: center;
}

#delete-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* ---------- Sheet view ---------- */

.sheet-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.sheet-topbar .left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.sheet-topbar h1 {
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 1.3rem;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dirty-flag {
  font-size: 0.8rem;
  color: var(--danger);
  visibility: hidden;
}

.dirty-flag.visible { visibility: visible; }

.dirty-flag.autosave-mode { color: var(--ink-soft); font-style: italic; }

.autosave-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 0.82rem;
  color: var(--ink-soft);
  cursor: pointer;
  user-select: none;
}

.autosave-toggle .switch {
  position: relative;
  display: inline-block;
  width: 34px;
  height: 19px;
  flex-shrink: 0;
}

.autosave-toggle .switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.autosave-toggle .switch-track {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  transition: background 0.15s ease;
}

.autosave-toggle .switch-track::before {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  left: 3px;
  top: 2.5px;
  background: var(--bg-raised);
  border-radius: 50%;
  transition: transform 0.15s ease;
}

.autosave-toggle .switch input:checked + .switch-track {
  background: var(--accent);
}

.autosave-toggle .switch input:checked + .switch-track::before {
  transform: translateX(15px);
}

.actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }

.section {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}

.section h2 {
  display: flex;
  align-items: center;
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 1.05rem;
  margin: 0 0 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field label {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--ink-soft);
}

.field input,
.field select,
.field textarea {
  font-family: var(--font-body);
  font-size: 1rem;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--ink);
  width: 100%;
}

.field textarea { resize: vertical; }

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.abilities-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  gap: 12px;
}

.ability-box {
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  background: var(--bg);
}

.ability-box label {
  display: block;
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin-bottom: 4px;
}

.ability-box input {
  width: 100%;
  text-align: center;
  font-size: 1.2rem;
  border: none;
  background: transparent;
  color: var(--ink);
}

.ability-box .mod {
  font-size: 0.85rem;
  color: var(--accent);
  font-weight: 700;
}

.ability-score-value {
  font-size: 1.2rem;
}

.ability-dice {
  font-size: 0.7rem;
  color: var(--ink-soft);
  margin-top: 2px;
}

.ability-box.rolling {
  border-color: var(--accent);
}

.ability-box.rolling .ability-score-value {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.wizard-tradeoffs {
  margin-top: 16px;
}

.wizard-tradeoffs h3 {
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 0.95rem;
  margin: 0 0 8px;
}

.wizard-tradeoff-row {
  margin-bottom: 6px;
}

.wizard-tradeoff-row .btn {
  width: 100%;
  text-align: left;
}

.hp-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.hp-row input { width: 70px; text-align: center; }

.stepper {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  border-radius: 6px;
  width: 30px;
  height: 30px;
  font-size: 1rem;
  line-height: 1;
}

.repeatable-header,
.repeatable-row {
  display: grid;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
}

.repeatable-header {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.repeatable-row input,
.repeatable-row textarea {
  font-family: var(--font-body);
  font-size: 0.95rem;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--ink);
  width: 100%;
}

.repeatable-row input[type="checkbox"] {
  width: auto;
  justify-self: center;
}

.remove-row {
  border: none;
  background: transparent;
  color: var(--danger);
  font-size: 1.1rem;
  padding: 4px;
  line-height: 1;
}

.add-row {
  margin-top: 4px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--ink-soft);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.85rem;
}

.add-row:hover { border-color: var(--accent); color: var(--accent); }

.total-line {
  margin-top: 8px;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.notes textarea {
  min-height: 120px;
}

@media (max-width: 480px) {
  .sheet-topbar h1 { max-width: 100%; }

  /* The HP control (two steppers + two number inputs + a slash) needs
     ~240px of fixed-width content -- wider than the ~150px a narrow phone
     gives a .field-grid column, which made it overflow its box. Giving it
     the full row sidesteps that without shrinking the tap targets. */
  .hp-field { grid-column: 1 / -1; }
}

/* ---------- Character creation wizard ---------- */

.list-toolbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

#wizard-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.wizard-step p.rules-disclaimer {
  margin-top: 0;
}

.wizard-nav {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.wizard-choice-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.wizard-class-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  margin-bottom: 8px;
}

.wizard-class-card {
  text-align: left;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  cursor: pointer;
  color: var(--ink);
}

.wizard-class-card:hover:not(:disabled) {
  border-color: var(--accent);
}

.wizard-class-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.wizard-class-card.ineligible {
  opacity: 0.5;
  cursor: not-allowed;
}

.wizard-class-name {
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 1.05rem;
}

.wizard-class-meta {
  font-size: 0.8rem;
  color: var(--ink-soft);
  margin-top: 2px;
}

.wizard-class-blocked {
  font-size: 0.78rem;
  color: var(--danger);
  margin-top: 6px;
}

.wizard-equipment-list {
  max-height: 40vh;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 10px;
}

.wizard-cart {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.wizard-cart-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.92rem;
}

.wizard-gold-controls {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.wizard-gold-controls .field {
  flex: 1;
  min-width: 140px;
}

.wizard-in-debt {
  color: var(--danger);
  font-weight: 600;
}

/* ---------- Attack (hit/miss) calculator ---------- */

.attack-calc-result:not(:empty) {
  margin-top: 16px;
}

.attack-calc-verdict {
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 2rem;
  text-align: center;
  padding: 10px;
  border-radius: 8px;
  border: 2px solid currentColor;
}

.attack-calc-result.hit .attack-calc-verdict {
  color: var(--hp-good);
}

.attack-calc-result.miss .attack-calc-verdict {
  color: var(--danger);
}

.attack-calc-math {
  text-align: center;
  font-size: 0.85rem;
  color: var(--ink-soft);
  margin-top: 8px;
}
