/* Brand: #e2000f accent, white surfaces, #303030 for all dark tones. */
:root {
  --brand: #e2000f;
  --brand-ink: #b30009;
  --ink: #303030;
  --ink-2: #5c5c5c;
  --muted: #8a8a8a;
  --surface: #ffffff;
  --surface-2: #f5f5f5;
  --hairline: #e0e0e0;
  --focus: rgba(226, 0, 15, 0.28);
  --sidebar: 400px;
}

* { box-sizing: border-box; }
/* Author `display` rules below outrank the UA stylesheet's [hidden] rule, so
   panels toggled via the hidden attribute need this to actually disappear. */
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 13px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--surface-2);
}
button, input, select { font: inherit; color: inherit; }

.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: -0.01em; font-size: 15px; }
.brand .mark { height: 22px; width: auto; display: block; flex: 0 0 auto; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

/* ---------------------------------------------------------------- login -- */
#login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--surface-2); z-index: 50; }
#login form {
  width: 320px; padding: 26px; background: var(--surface);
  border: 1px solid var(--hairline); border-radius: 12px;
  box-shadow: 0 1px 2px rgba(48,48,48,0.06), 0 10px 32px rgba(48,48,48,0.1);
  display: flex; flex-direction: column; gap: 14px;
}
#login label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--ink-2); }
#login input {
  padding: 9px 11px; border: 1px solid var(--hairline); border-radius: 8px; background: var(--surface);
}
#login input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--focus); }
#login button { margin-top: 4px; }
.err { color: var(--brand); margin: 0; font-size: 12px; }

/* --------------------------------------------------------------- shell --- */
#app { height: 100%; display: flex; flex-direction: column; }
header {
  height: 50px; flex: 0 0 auto; display: flex; align-items: center; gap: 12px;
  padding: 0 14px; background: var(--surface); border-bottom: 1px solid var(--hairline);
}
main { flex: 1; display: flex; min-height: 0; }
#map { flex: 1; min-width: 0; position: relative; }
aside {
  width: var(--sidebar); flex: 0 0 var(--sidebar); display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--hairline); min-height: 0;
}

/* --------------------------------------------------------------- tabs ---- */
.tabs { display: flex; border-bottom: 1px solid var(--hairline); flex: 0 0 auto; }
.tabs button {
  flex: 1; padding: 11px 8px; background: none; border: 0; cursor: pointer;
  color: var(--ink-2); font-weight: 500; border-bottom: 2px solid transparent;
}
.tabs button:hover { background: var(--surface-2); }
.tabs button.active { color: var(--ink); border-bottom-color: var(--brand); }

aside section { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* ------------------------------------------------------------- buttons --- */
button.primary {
  background: var(--brand); color: #fff; border: 1px solid var(--brand);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 600;
}
button.primary:hover { background: var(--brand-ink); border-color: var(--brand-ink); }
button.primary:disabled { background: var(--muted); border-color: var(--muted); cursor: not-allowed; }
#login button { background: var(--brand); color: #fff; border: 0; padding: 10px; border-radius: 8px; cursor: pointer; font-weight: 600; }
button.ghost {
  background: var(--surface); border: 1px solid var(--hairline); border-radius: 8px;
  padding: 7px 11px; cursor: pointer; color: var(--ink-2);
}
button.ghost:hover { border-color: var(--ink-2); color: var(--ink); }
button.small { padding: 4px 8px; font-size: 12px; }
button.wide { width: calc(100% - 24px); margin: 0 12px 10px; }

/* ------------------------------------------------------------- filters --- */
.filters { padding: 12px; display: flex; flex-direction: column; gap: 8px; flex: 0 0 auto; }
.filters .row { display: flex; gap: 8px; }
.filters input, .filters select, .apply select {
  padding: 8px 10px; border: 1px solid var(--hairline); border-radius: 8px; background: var(--surface); width: 100%;
}
.filters input:focus, .filters select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--focus); }

.counts {
  display: flex; align-items: center; gap: 10px; padding: 0 12px 8px; flex: 0 0 auto;
}
.pick { display: flex; align-items: center; gap: 7px; cursor: pointer; color: var(--ink-2); }
#hitcount { font-variant-numeric: tabular-nums; }

/* --------------------------------------------------------------- table --- */
.tablewrap { flex: 1; overflow-y: auto; min-height: 0; border-top: 1px solid var(--hairline); }
#areas { width: 100%; border-collapse: collapse; }
#areas thead th {
  position: sticky; top: 0; background: var(--surface); z-index: 1;
  text-align: left; font-weight: 500; color: var(--muted); font-size: 11px;
  padding: 7px 8px; border-bottom: 1px solid var(--hairline); cursor: pointer; white-space: nowrap;
}
#areas thead th.cb { cursor: default; width: 28px; }
#areas thead th.sorted { color: var(--ink); }
#areas thead th.sorted::after { content: " ↑"; }
#areas thead th.sorted.desc::after { content: " ↓"; }
#areas td { padding: 5px 8px; border-bottom: 1px solid var(--surface-2); }
#areas tr:hover td { background: var(--surface-2); }
#areas tr.sel td { background: #fdeaec; }
#areas td.plz { font-variant-numeric: tabular-nums; font-weight: 600; }
#areas td.ort, #areas td.reg { color: var(--ink-2); max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#areas td.land { color: var(--muted); font-size: 11px; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; box-shadow: inset 0 0 0 1px rgba(48,48,48,0.2); }
.more { padding: 8px 12px; margin: 0; }

.apply {
  flex: 0 0 auto; display: flex; gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--hairline); background: var(--surface);
}
.apply select { flex: 1; }

/* ------------------------------------------------------------- regions --- */
#regionlist { flex: 1; overflow-y: auto; padding: 8px 12px; min-height: 0; }
.region {
  display: grid; grid-template-columns: 26px 1fr auto; gap: 8px; align-items: center;
  padding: 9px 0; border-bottom: 1px solid var(--surface-2);
}
.region input[type="color"] {
  width: 26px; height: 26px; padding: 0; border: 1px solid var(--hairline);
  border-radius: 6px; background: none; cursor: pointer;
}
.region .fields { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.region input[type="text"] {
  border: 1px solid transparent; border-radius: 6px; padding: 4px 6px; width: 100%; background: none;
}
.region input[type="text"]:hover { border-color: var(--hairline); }
.region input[type="text"]:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--focus); background: var(--surface); }
.region .name { font-weight: 600; }
.region .who { font-size: 12px; color: var(--ink-2); }
.region .meta { display: flex; align-items: center; gap: 8px; }
.region .n { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
.region .del {
  border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 15px; line-height: 1;
  padding: 2px 4px; border-radius: 5px;
}
.region .del:hover { color: var(--brand); background: var(--surface-2); }
.hint { padding: 0 12px 12px; margin: 0; font-size: 12px; }

/* -------------------------------------------------------------- tooltip -- */
#tip {
  position: fixed; pointer-events: none; z-index: 30; display: none;
  background: var(--surface); border: 1px solid var(--hairline); border-radius: 8px;
  box-shadow: 0 1px 2px rgba(48,48,48,0.08), 0 6px 18px rgba(48,48,48,0.12);
  padding: 7px 10px; max-width: 230px;
}
#tip .p { font-weight: 700; font-variant-numeric: tabular-nums; }
#tip .o { color: var(--ink-2); }
#tip .r { display: flex; align-items: center; gap: 6px; margin-top: 4px; padding-top: 4px; border-top: 1px solid var(--hairline); }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 40;
  background: var(--ink); color: #fff; padding: 9px 15px; border-radius: 8px;
  box-shadow: 0 6px 20px rgba(48,48,48,0.28);
}
.maplibregl-ctrl-attrib { font-size: 10px; }

/* ======================================================= reader view ==== */

/* Scoped to the reader shell: the admin shell lays #map out as a flex child,
   and an unscoped `#app #map` rule here would cover its whole UI. */
.reader #app { display: block; }
.reader #map { position: absolute; inset: 0; }

#panel {
  position: absolute; top: 14px; left: 14px; z-index: 5;
  width: 320px; max-height: calc(100% - 28px);
  display: flex; flex-direction: column;
  border-radius: 12px; overflow: hidden;
  box-shadow: 0 1px 2px rgba(48,48,48,.08), 0 10px 34px rgba(48,48,48,.16);
}
.phead {
  display: block; height: auto; gap: 0;
  padding: 14px 16px 10px; border-bottom: 1px solid var(--hairline);
  background: none;
}
.phead .brand { font-size: 14px; }
.phead #sub { margin: 4px 0 0; font-size: 12px; }

.search-wrap { padding: 10px 12px; border-bottom: 1px solid var(--hairline); }
.search-wrap input {
  width: 100%; padding: 8px 10px; border: 1px solid var(--hairline);
  border-radius: 8px; background: var(--surface-2);
}
.search-wrap input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--focus); background: var(--surface); }
#hits { list-style: none; margin: 6px 0 0; padding: 0; max-height: 190px; overflow-y: auto; }
#hits li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
#hits li:hover { background: var(--surface-2); }
#hits .plz { font-variant-numeric: tabular-nums; font-weight: 600; }
#hits .meta { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.readlist { overflow-y: auto; padding: 6px; flex: 1; min-height: 0; }
.readrow {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 10px; border: 0; border-radius: 8px; background: none;
  color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.readrow:hover { background: var(--surface-2); }
.readrow[aria-pressed="true"] { background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--brand); }
.readrow .sw { width: 14px; height: 14px; border-radius: 4px; flex: 0 0 auto; box-shadow: inset 0 0 0 1px rgba(48,48,48,.22); }
.rtext { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.rname { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rwho { font-size: 12px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rwho em { color: var(--muted); font-style: normal; }
.rcount { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }

.pfoot {
  display: flex; align-items: center; gap: 10px; padding: 9px 14px;
  border-top: 1px solid var(--hairline); font-size: 12px;
}
.pfoot .spacer { flex: 1; }
.pfoot a, .linkish {
  color: var(--brand); background: none; border: 0; padding: 0;
  font: inherit; cursor: pointer; text-decoration: none;
}
.pfoot a:hover, .linkish:hover { text-decoration: underline; }

#tip .who { color: var(--ink-2); font-size: 12px; margin-top: 2px; }

@media (max-width: 640px) {
  #panel { width: auto; left: 10px; right: 10px; max-height: 58%; }
}

#denied {
  max-width: 34rem; margin: 18vh auto; padding: 26px 28px;
  background: var(--surface); border: 1px solid var(--hairline);
  border-left: 3px solid var(--brand); border-radius: 10px;
}
#denied h1 { margin: 0 0 10px; font-size: 19px; }
#denied p { margin: 0 0 8px; color: var(--ink-2); }
#denied a { color: var(--brand); }

.selbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px 6px; font-size: 12px;
  border-bottom: 1px solid var(--hairline);
}
.selbar #selhint { flex: 1; }

/* ============================================== headquarters flag pin ==== */

.hq-pin { position: relative; width: 62px; height: 76px; cursor: pointer; }

.hq-pole {
  position: absolute; left: 0; bottom: 0; width: 2px; height: 76px;
  background: linear-gradient(to right, #5a5a5a, #303030);
  border-radius: 1px;
}
.hq-finial {
  position: absolute; left: -2px; top: -3px; width: 6px; height: 6px;
  border-radius: 50%; background: var(--ink);
}
/* The point that actually sits on the coordinate. */
.hq-foot {
  position: absolute; left: -3px; bottom: -3px; width: 8px; height: 8px;
  border-radius: 50%; background: var(--ink);
  box-shadow: 0 0 0 2px #fff, 0 1px 3px rgba(48, 48, 48, .45);
}

.hq-flag {
  position: absolute; left: 2px; top: 3px; width: 56px;
  transform-origin: left center;
  filter: url(#hq-flag-wave) drop-shadow(0 1px 2px rgba(48, 48, 48, .35));
  animation: hq-sway 5.5s ease-in-out infinite;
}
.hq-flag img { display: block; width: 100%; height: auto; border-radius: 2px; }

/* Swings from the pole; the SVG filter supplies the ripple across the cloth. */
@keyframes hq-sway {
  0%, 100% { transform: skewY(0deg)    scaleX(1);    }
  25%      { transform: skewY(-1.4deg) scaleX(.985); }
  50%      { transform: skewY(.7deg)   scaleX(1);    }
  75%      { transform: skewY(1.4deg)  scaleX(.985); }
}

@media (prefers-reduced-motion: reduce) {
  .hq-flag { animation: none; filter: drop-shadow(0 1px 2px rgba(48, 48, 48, .35)); }
}

.hq-pop { display: flex; flex-direction: column; gap: 2px; padding: 2px 4px; }
.hq-pop strong { font-size: 13px; color: var(--ink); }
.hq-pop span { font-size: 11.5px; color: var(--ink-2); }
.maplibregl-popup-content { border-radius: 8px; box-shadow: 0 2px 10px rgba(48,48,48,.22); }
