/* Flint's Empire Config - tokens first, dark by default, light on request. */
:root {
  --bg: #15120e;
  --panel: #211c16;
  --panel-2: #2b241c;
  --ink: #f1e9dc;
  --muted: #b3a690;
  --gold: #e3b04b;
  --gold-ink: #1c1406;
  --line: #3d3328;
  --discord: #5865f2;
  --radius: 14px;
  --shadow: 0 10px 30px rgb(0 0 0 / 0.35);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f6f0e4; --panel: #fffaf1; --panel-2: #f1e7d6; --ink: #241c12;
    --muted: #6d5f4a; --line: #e2d4bd; --shadow: 0 10px 30px rgb(60 40 10 / 0.12);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; min-height: 100vh; background: var(--bg); color: var(--ink);
  font: 16px/1.5 "Heebo", system-ui, sans-serif;
}
a { color: inherit; }
.muted { color: var(--muted); font-size: 0.85rem; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 800; }
.crest { color: var(--gold); font-size: 1.4rem; }

.btn {
  display: inline-flex; align-items: center; gap: 8px; border: 0; cursor: pointer;
  padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 600; text-decoration: none;
  background: var(--gold); color: var(--gold-ink);
}
.btn-discord { background: var(--discord); color: #fff; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.btn:focus-visible, .portal:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

.who { display: flex; align-items: center; gap: 10px; }
.who-text { display: grid; line-height: 1.2; }
.avatar { border-radius: 50%; }

.home { max-width: 1100px; margin: 0 auto; padding: 32px 16px 64px; }
.hero { text-align: center; margin: 24px 0 40px; }
.hero h1 { font-size: clamp(2rem, 6vw, 3.4rem); margin: 0 0 8px; letter-spacing: -0.5px; }
.hero p { color: var(--muted); margin: 0; }

.portals { display: grid; gap: 18px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.portal {
  display: block; padding: 28px 24px; border-radius: var(--radius); text-decoration: none;
  background: linear-gradient(160deg, var(--panel-2), var(--panel)); border: 1px solid var(--line);
  box-shadow: var(--shadow); transition: transform .18s ease, border-color .18s ease;
}
.portal:hover { transform: translateY(-4px); border-color: var(--gold); }
.portal-icon { font-size: 2.2rem; }
.portal h2 { margin: 12px 0 4px; }
.portal p { margin: 0; color: var(--muted); }

.center { display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px; max-width: 440px; box-shadow: var(--shadow); }
@media (prefers-reduced-motion: reduce) { .portal, .node { transition: none; } }

/* ---- inner pages ---- */
.page { max-width: 1200px; margin: 0 auto; padding: 24px 16px 64px; }
.page-head h1 { margin: 4px 0; font-size: clamp(1.6rem, 4vw, 2.3rem); }
.back { color: var(--muted); text-decoration: none; font-size: .9rem; }
.section-title { font-size: 1rem; color: var(--muted); font-weight: 600; margin: 24px 0 12px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.error, .bad { color: #ff8a7a; }
.ok { color: #8fd694; }

/* ---- roles: org chart ---- */
.node-ruler { --tier: #e3b04b; } .node-nobility { --tier: #b58cf0; } .node-officials { --tier: #6cb2f0; }
.node-knights { --tier: #9aa7b4; } .node-citizens { --tier: #7fc48a; }

.chart-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page-wide { max-width: none; }
.chart-area {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 28px 16px 24px; overflow: hidden;
  background: radial-gradient(ellipse at top, var(--panel-2), var(--bg) 70%);
}
.chart { display: flex; justify-content: center; }
.chart .tree { transform-origin: top center; flex: none; }   /* keep its natural width; fitChart scales it */

/* Classic connector tree. Laid out LTR on purpose (the ::before/::after halves
   assume the first child is leftmost); roles.js reverses the children so the
   chart still reads right-to-left. */
.tree, .tree ul { direction: ltr; display: flex; justify-content: center; margin: 0; padding: 22px 0 0; position: relative; }
.tree { padding-top: 0; width: max-content; min-width: 100%; }
.tree li { list-style: none; position: relative; padding: 22px 6px 0; display: flex; flex-direction: column; align-items: center; }
.tree > li { padding-top: 0; }
.tree li::before, .tree li::after {
  content: ""; position: absolute; top: 0; right: 50%; width: 50%; height: 22px; border-top: 2px solid var(--line);
}
.tree li::after { right: auto; left: 50%; border-left: 2px solid var(--line); }
.tree li:only-child::before, .tree li:only-child::after { display: none; }
.tree li:only-child { padding-top: 0; }
.tree li:first-child::before, .tree li:last-child::after { border: 0 none; }
.tree li:last-child::before { border-right: 2px solid var(--line); border-radius: 0 8px 0 0; }
.tree li:first-child::after { border-radius: 8px 0 0 0; }
.tree ul::before { content: ""; position: absolute; top: 0; left: 50%; height: 22px; border-left: 2px solid var(--line); }
.tree > li > .node, .tree li > ul::before { z-index: 1; }

.node {
  direction: rtl; display: flex; flex-direction: column; align-items: center; gap: 3px;
  min-width: 128px; max-width: 168px; padding: 10px 10px 9px; border-radius: 12px; font: inherit; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line); border-top: 3px solid var(--tier, var(--gold));
  box-shadow: 0 6px 16px rgb(0 0 0 / .25); text-align: center;
  transition: transform .15s ease, box-shadow .15s ease;
}
button.node { cursor: pointer; }
button.node:hover, button.node:focus-visible { transform: translateY(-3px); box-shadow: 0 0 0 2px var(--tier), 0 10px 22px rgb(0 0 0 / .35); outline: none; }
.node-root { --tier: var(--gold); flex-direction: row; gap: 8px; padding: 10px 18px; max-width: none; font-size: 1.05rem;
  background: linear-gradient(160deg, #3a2c12, var(--panel)); }
.root-crest { color: var(--gold); font-size: 1.5rem; }
.node-group { min-width: 0; max-width: none; white-space: nowrap; padding: 6px 14px; font-weight: 700; color: var(--muted); background: var(--panel-2); border-style: dashed; border-top-style: solid; }
.faces { position: relative; width: 56px; height: 56px; display: grid; place-items: center; }
.head3d { image-rendering: pixelated; filter: drop-shadow(0 4px 6px rgb(0 0 0 / .45)); }
.disc-face { border-radius: 50%; }
.disc-badge { position: absolute; bottom: -2px; inset-inline-start: -4px; border-radius: 50%; border: 2px solid var(--panel); }
.node-name { font-size: .95rem; line-height: 1.2; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-disc { font-size: .75rem; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-disc.no-mc { font-style: italic; opacity: .8; }
.chip { margin-top: 3px; font-size: .72rem; padding: 1px 8px; border-radius: 999px; background: var(--panel-2); color: var(--tier); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head { image-rendering: pixelated; border-radius: 6px; }

/* A flat team folds into a grid: one line down from the manager, then wrap. */
.tree .team-grid {
  direction: rtl; display: grid; grid-template-columns: repeat(var(--cols), auto); gap: 10px;
  padding: 14px; margin-top: 22px; position: relative; justify-content: center;
  border: 1px dashed var(--line); border-radius: 14px; background: rgb(0 0 0 / .12);
}
.tree .team-grid::before { content: ""; position: absolute; top: -24px; left: 50%; height: 22px; border-left: 2px solid var(--line); }
.tree .team-grid li { padding: 0; }
.tree .team-grid li::before, .tree .team-grid li::after { display: none; }

.unattached { display: grid; gap: 14px; margin-top: 28px; padding-top: 22px; border-top: 1px solid var(--line); }
.band { border-inline-start: 4px solid var(--tier); padding-inline-start: 12px; }
.band-head { display: flex; align-items: center; gap: 8px; margin: 0 0 10px; font-size: 1rem; }
.count { background: var(--panel-2); border-radius: 999px; padding: 0 10px; font-size: .8rem; font-weight: 700; }
.band-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 10px; }
.band-list .node { min-width: 120px; }

.side { margin-top: 28px; max-width: 520px; }

.dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--ink); padding: 20px; width: min(420px, 92vw); }
.dialog::backdrop { background: rgb(0 0 0 / .55); }
.dialog h3 { margin: 0 0 12px; }
.dialog label { display: block; font-weight: 600; margin-bottom: 6px; }
.dialog select { width: 100%; padding: 9px; border-radius: 9px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font: inherit; }
.dialog-actions { display: flex; gap: 8px; margin-top: 14px; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; background: var(--panel-2); font-weight: 700; margin: 0 0 8px; }
.badge.ok { background: #1f3a24; color: #8fd694; }

/* ---- application form ---- */
.apply { display: grid; gap: 8px; }
.apply label { font-weight: 600; font-size: .9rem; margin-top: 6px; }
.apply input:not([type="radio"]), .apply textarea {
  width: 100%; padding: 9px 11px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font: inherit;
}
.apply input:focus, .apply textarea:focus { outline: 2px solid var(--gold); outline-offset: 1px; }
.role-pick { display: grid; gap: 6px; grid-template-columns: 1fr 1fr; }
.role-pick input { position: absolute; opacity: 0; pointer-events: none; }
.role-card {
  display: grid; gap: 2px; padding: 8px 10px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel-2); margin: 0 !important;
}
.role-pick input:checked + .role-card { border-color: var(--gold); box-shadow: inset 0 0 0 1px var(--gold); }
.role-pick input:focus-visible + .role-card { outline: 2px solid var(--gold); }
.mc-row { display: flex; gap: 10px; align-items: center; }
.head-preview { image-rendering: pixelated; border-radius: 8px; background: var(--panel-2); flex: none; }
.mc-status { font-size: .85rem; min-height: 1.2em; }
.form-msg { min-height: 1.2em; margin: 4px 0 0; }

/* ---- tasks ---- */
.tasks-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.tasks-tools { display: flex; gap: 10px; align-items: center; }
.filter { display: flex; gap: 4px; }
.filter .btn.on { border-color: var(--gold); color: var(--ink); }
.board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; margin-top: 20px; align-items: start; }
@media (max-width: 860px) { .board { grid-template-columns: 1fr; } }
.col { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; min-height: 160px; transition: border-color .15s; }
.col.over { border-color: var(--gold); }
.col h2 { font-size: 1rem; margin: 2px 4px 12px; display: flex; align-items: center; gap: 8px; }
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.cards .empty { padding: 18px 6px; text-align: center; font-size: .9rem; }
.task {
  width: 100%; text-align: start; display: grid; gap: 8px; padding: 12px; border-radius: 11px; cursor: pointer;
  background: var(--panel-2); border: 1px solid var(--line); border-inline-start: 4px solid var(--prio); color: var(--ink); font: inherit;
  transition: transform .12s ease, box-shadow .12s ease;
}
.task:hover, .task:focus-visible { transform: translateY(-2px); box-shadow: 0 6px 16px rgb(0 0 0 / .3); outline: none; }
.task.dragging { opacity: .5; }
.prio-high { --prio: #ff8a6a; } .prio-normal { --prio: var(--gold); } .prio-low { --prio: #7f8a96; }
.task-top { display: flex; align-items: center; gap: 8px; }
.prio-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--prio); flex: none; }
.task-title { line-height: 1.3; }
.task-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.faces-row { display: flex; align-items: center; }
.faces-row .face { margin-inline-start: -6px; border: 2px solid var(--panel-2); }
.faces-row .face:first-child { margin-inline-start: 0; }
.face { border-radius: 6px; image-rendering: pixelated; background: var(--panel); }
.face.disc { border-radius: 50%; image-rendering: auto; }
.more { font-size: .75rem; color: var(--muted); margin-inline-start: 4px; }
.due { font-size: .78rem; color: var(--muted); }
.due.late { color: #ff8a7a; font-weight: 700; }
.aud { font-size: .75rem; }
.badge.warn { background: #3e3214; color: var(--gold); justify-self: start; font-size: .75rem; margin: 0; }
.danger { color: #ff8a7a !important; }

.dialog-wide { width: min(560px, 94vw); }
.dialog .apply label { margin-top: 8px; }
.dialog select, .dialog input[type="date"], .dialog input[type="search"] {
  width: 100%; padding: 9px; border-radius: 9px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font: inherit;
}
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.audience { border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px 12px; margin: 10px 0 0; }
.audience legend { font-weight: 600; padding: 0 6px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 400 !important; margin: 4px 0 !important; }
.check-row { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 4px 0; }
.picker { display: grid; gap: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-pick { display: inline-flex; align-items: center; gap: 6px; padding: 3px 8px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel-2); color: var(--ink); font: inherit; font-size: .85rem; cursor: pointer; }
.pick-list { list-style: none; margin: 0; padding: 0; max-height: 170px; overflow: auto; }
.pick-list button { width: 100%; display: flex; align-items: center; gap: 8px; padding: 5px 6px; border: 0; background: none; color: var(--ink); font: inherit; cursor: pointer; border-radius: 8px; text-align: start; }
.pick-list button:hover, .pick-list button:focus-visible { background: var(--panel-2); outline: none; }

.prio-badge { background: var(--panel-2); color: var(--prio); font-size: .8rem; }
.task-body { white-space: pre-wrap; line-height: 1.6; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 12px 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.people { display: flex; flex-wrap: wrap; gap: 8px; }
.person { display: inline-flex; align-items: center; gap: 6px; }
.dialog-actions { flex-wrap: wrap; }

/* ---- map ---- */
.map-page { display: flex; flex-direction: column; height: 100vh; }
.map-layout { flex: 1; display: grid; grid-template-columns: 320px 1fr; min-height: 0; }
@media (max-width: 760px) { .map-layout { grid-template-columns: 1fr; grid-template-rows: 1fr auto; } .map-side { order: 2; max-height: 40vh; } }
.map-side { background: var(--panel); border-inline-end: 1px solid var(--line); padding: 16px; overflow: auto; display: grid; align-content: start; gap: 12px; }
.map-side h1 { margin: 2px 0 4px; font-size: 1.4rem; }
.map-wrap { position: relative; min-height: 300px; }
#map { position: absolute; inset: 0; background: #0f1a24; }
.coords { position: absolute; top: 10px; inset-inline-start: 10px; z-index: 500; background: rgb(0 0 0 / .6); color: #fff;
  padding: 3px 10px; border-radius: 8px; font: 600 .85rem/1.4 ui-monospace, Consolas, monospace; pointer-events: none; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.region-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.region-item { width: 100%; display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--ink); font: inherit; cursor: pointer; text-align: start; }
.region-item.on { border-color: var(--gold); }
.region-item strong { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.swatch { width: 14px; height: 14px; border-radius: 4px; background: var(--c); flex: none; }
.status { font-size: .75rem; padding: 0 8px; border-radius: 999px; background: var(--bg); }
.st-planned { color: var(--muted); } .st-building { color: var(--gold); } .st-done { color: #8fd694; }
.region-detail { padding: 8px 4px 4px; }
.region-detail .notes { white-space: pre-wrap; margin: 0 0 6px; }
/* Leaflet owns .region-label's transform (that is how it positions markers),
   so the centring lives on the inner span. */
.region-label { width: 0; height: 0; pointer-events: none; }
.region-label span { position: absolute; transform: translate(-50%, -50%); color: #fff; font: 800 19px/1.15 Heebo, system-ui, sans-serif;
  white-space: nowrap; text-shadow: 0 0 4px #000, 0 0 4px #000, 0 2px 3px #000; border-bottom: 3px solid var(--c); padding-bottom: 2px; }
.region-label.hidden { display: none; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.sw { width: 30px; height: 30px; border-radius: 8px; background: var(--c); cursor: pointer; position: relative; margin: 0 !important; }
.sw input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.sw:has(input:checked) { outline: 3px solid var(--ink); outline-offset: 2px; }
.sw:has(input:focus-visible) { outline: 3px solid var(--gold); }
.base-image { image-rendering: pixelated; }
.leaflet-container { font-family: Heebo, system-ui, sans-serif; }

.sw-custom { background: conic-gradient(from 0deg, #ff5a5a, #ffd25a, #5aff8c, #5ac8ff, #a05aff, #ff5ac8, #ff5a5a); display: grid; place-items: center; }
.sw-custom::after { content: ""; position: absolute; inset: 6px; border-radius: 5px; background: var(--c); }
.sw-plus { position: relative; z-index: 1; font-weight: 800; color: #fff; text-shadow: 0 0 3px #000; pointer-events: none; }
.reset-view { font-size: 18px; line-height: 30px !important; font-weight: 700; text-decoration: none; }

/* in-page colour picker */
.cp-panel { margin-top: 4px; }
.cp { display: grid; gap: 10px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); }
.cp-sv { position: relative; height: 150px; border-radius: 8px; cursor: crosshair; touch-action: none;
  background: linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, var(--hue)); direction: ltr; }
.cp-hue { position: relative; height: 16px; border-radius: 999px; cursor: pointer; touch-action: none; direction: ltr;
  background: linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00); }
.cp-dot { position: absolute; width: 16px; height: 16px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px #000, 0 2px 4px rgb(0 0 0 / .5);
  transform: translate(-50%, -50%); pointer-events: none; }
.cp-hue .cp-dot { top: 50%; }
.cp-sv:focus-visible, .cp-hue:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.cp-row { display: flex; gap: 10px; align-items: center; }
.cp-preview { width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line); flex: none; }
.dialog .cp .cp-hex { width: 120px; padding: 7px 9px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); font: 600 .95rem ui-monospace, Consolas, monospace; }
