/* Contrast (WCAG 2.x AA), checked against every background they sit on:
   text >= 4.5:1 (--muted is 4.8 even on the selected row, --danger 5.0,
   white on --primary 4.8); --control-line (borders of inputs, selects,
   buttons) >= 3:1 against the panels and the input fill. --line is for
   decorative panel/card edges only, which don't need 3:1. */
:root {
  --bg: #0c0e12; --panel: #161a22; --panel-2: #1d2230; --line: #2a3142; --control-line: #69748e;
  --text: #e6e8ee; --muted: #9fa7ba; --accent: #d4a85a; --primary: #2d6be6;
  --danger: #ef645c; --ok: #2bb673;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: #8ab4ff; }
button, input, select { font: inherit; color: inherit; }
button {
  background: var(--panel-2); border: 1px solid var(--control-line); border-radius: 6px; padding: 6px 10px; cursor: pointer;
}
/* Keyboard focus: always visible (gold ring, 7:1 on the panels). */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::placeholder { color: var(--muted); opacity: 1; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.primary, a.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
a.btn {
  display: inline-block; background: var(--panel-2); border: 1px solid var(--control-line); border-radius: 6px;
  padding: 6px 10px; color: var(--text); text-decoration: none; white-space: nowrap;
}
a.btn:hover { border-color: var(--accent); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
button.link { background: none; border: 0; padding: 0; color: #8ab4ff; }
button.danger, .danger { color: var(--danger); }
input, select { background: var(--bg); border: 1px solid var(--control-line); border-radius: 6px; padding: 6px 8px; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.warn { color: #f0b429; }
form.inline { display: inline; }

.topbar {
  display: flex; align-items: center; gap: 14px; padding: 8px 16px; height: 50px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--accent); text-decoration: none; white-space: nowrap; }
.topbar-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-nav { display: flex; align-items: center; gap: 10px; }
.who { color: var(--muted); }
.lang-picker { padding: 3px 6px; font-size: 13px; }
.badge { padding: 3px 8px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); }

.messages { padding: 8px 16px 0; }
.msg { padding: 8px 12px; border-radius: 6px; margin-bottom: 6px; background: var(--panel-2); }
.msg-error { border-left: 3px solid var(--danger); }
.msg-success { border-left: 3px solid var(--ok); }

.page { max-width: 1000px; margin: 20px auto; padding: 0 16px; }
.page.narrow { max-width: 380px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; margin-bottom: 16px; }
.card h2 { margin-top: 0; font-size: 16px; }
.card label { display: block; margin-bottom: 10px; }
.card label input, .card label select { display: block; width: 100%; margin-top: 4px; }
.row-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.row-form label { flex: 1 1 200px; margin: 0; }
.row-form label.check { flex: 0 0 auto; display: flex; align-items: center; gap: 6px; }
.row-form label.check input { width: auto; display: inline; margin: 0; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.table th { color: var(--muted); font-weight: 500; }

/* editor */
body.editor { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }
.nav-toggle, .sheet-handle { display: none; }
body.editor .messages { position: absolute; top: 56px; right: 16px; z-index: 1000; }
.editor-layout { flex: 1; display: flex; min-height: 0; position: relative; }
.sidebar {
  width: 320px; flex: 0 0 320px; display: flex; flex-direction: column; gap: 8px; padding: 10px;
  background: var(--panel); border-right: 1px solid var(--line);
  /* Scroll as a whole when the panels don't fit (e.g. a selection panel
     opened) instead of pushing the bottom ones off screen. */
  overflow-y: auto; overscroll-behavior: contain;
}
.panel { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; flex-shrink: 0; }
/* The member list takes the spare height but never shrinks below ~220px;
   it scrolls on its own. */
.panel.grow { flex: 1 0 220px; min-height: 220px; display: flex; flex-direction: column; overflow: hidden; }
details.panel > summary {
  cursor: pointer; list-style: none; margin: 0; font-size: 13px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted); font-weight: 700; display: flex; justify-content: space-between;
}
details.panel > summary::-webkit-details-marker { display: none; }
details.panel > summary::after { content: "▾"; }
details.panel:not([open]) > summary::after { content: "▸"; }
details.panel[open] > summary { margin-bottom: 8px; }
.panel h3 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.panel h3 .rk { color: var(--accent); text-transform: none; }
#search { width: 100%; }
.toggles { display: flex; flex-direction: column; gap: 2px; margin-top: 8px; color: var(--muted); font-size: 13px; }
.stats { margin-top: 8px; font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
.tool-row { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.tool { display: flex; align-items: center; justify-content: center; padding: 4px 0; }
.tool img { width: 30px; height: 30px; object-fit: contain; }
.tool.active { border-color: var(--accent); background: #2a2415; }
#tool-hint { color: var(--accent); margin-top: 6px; }
#tool-hint[hidden] { display: none; }

/* Go to coordinates (map control under the zoom buttons) + pointer readout. */
.goto-ctl {
  display: flex; gap: 4px; padding: 4px; background: #11151d; border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 4px 14px rgba(0, 0, 0, .4);
}
.goto-ctl input { width: 64px; padding: 4px 6px; -moz-appearance: textfield; appearance: textfield; }
.goto-ctl input::-webkit-inner-spin-button, .goto-ctl input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.goto-ctl button { padding: 4px 10px; }
/* Beside the zoom buttons, not under them (Leaflet stacks a corner's
   controls with clear: both). */
.leaflet-left .goto-ctl { clear: none; }
.coord-readout {
  background: rgba(17, 21, 29, .85); color: var(--text); border: 1px solid var(--line); border-radius: 6px;
  padding: 2px 8px; font: 12px/1.5 ui-monospace, Consolas, monospace;
}
@media (hover: none) { .coord-readout { display: none; } }
.goto-mark { animation: goto-pulse .8s ease-in-out infinite alternate; }
@keyframes goto-pulse { from { stroke-opacity: 1; } to { stroke-opacity: .2; } }

/* Fertile Land name on the map (white on dark: 15:1). */
.fertile-label span {
  display: inline-block; transform: translate(-50%, -50%); white-space: nowrap;
  padding: 2px 8px; border: 1px solid var(--accent); border-radius: 999px;
  background: rgba(12, 14, 18, .85); color: var(--text); font-size: 12px; font-weight: 600;
}

/* Map layers: a button on the map (top right) with a small panel. */
.layers {
  display: flex; flex-direction: column; align-items: flex-end;
  background: #11151d; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 4px 14px rgba(0, 0, 0, .4);
}
.layers-btn { width: 40px; height: 40px; padding: 0; border: 0; background: none; color: var(--text); display: flex; align-items: center; justify-content: center; }
.layers.open .layers-btn { color: var(--accent); }
.layers-panel { align-self: stretch; min-width: 230px; padding: 0 12px 10px; }
.layers-panel[hidden] { display: none; }
.layers-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 4px; }
.layers .toggles { margin-top: 0; color: var(--text); }
.alliance-key { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 13px; }
.alliance-key[hidden] { display: none; }
.alliance-key > div:not(.layers-title) { display: flex; align-items: center; gap: 4px; padding: 2px 0; }
.alliance-key .swatch, .leaflet-tooltip .swatch, .kv .swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; }
.mode-banner {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 1000;
  background: #2a2415; border: 1px solid var(--accent); border-radius: 8px; padding: 6px 8px 6px 12px;
  display: flex; align-items: center; gap: 10px; box-shadow: 0 6px 20px rgba(0, 0, 0, .4);
}
.mode-banner[hidden] { display: none; }
.kv { font-size: 13px; color: var(--muted); }
.kv b { color: var(--text); }
.btn-row { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.gaps-setting { margin-top: 8px; padding: 6px 8px 8px; border: 1px solid var(--line); border-radius: 6px; }
.panel label.check { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.panel label.check input { width: auto; margin: 0; }
.field { display: block; font-size: 13px; color: var(--muted); margin-top: 8px; }
.field input, .field select { width: 100%; margin-top: 4px; }
#bld-alliance-new { width: 100%; margin-top: 6px; }
#bld-alliance-new[hidden] { display: none; }

.member-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.member-list li.member {
  display: grid; grid-template-columns: 20px 1fr auto; grid-template-rows: auto auto; column-gap: 6px;
  padding: 5px 6px; border-radius: 6px; cursor: pointer;
}
.member-list li.member:hover { background: #262c3b; }
.member-list li.member.selected { background: #2f3850; outline: 1px solid var(--accent); }
.member-list .st { grid-row: 1 / 3; align-self: center; }
.member-list .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-list .rk { color: var(--accent); font-size: 12px; }
.member-list .wh { grid-column: 2 / 4; color: var(--muted); font-size: 12px; }
.list-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.hide-planned { font-size: 12px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.removed { margin-top: 8px; padding-top: 6px; border-top: 1px solid var(--line); font-size: 13px; }
.removed[hidden] { display: none; }
.removed summary { cursor: pointer; color: var(--muted); padding: 4px 2px; }
.removed ul { list-style: none; margin: 4px 0 0; padding: 0; }
.removed li { display: flex; align-items: center; gap: 8px; padding: 3px 2px; }
.removed .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.removed button { font-size: 12px; padding: 2px 8px; }
.warnings { list-style: none; margin: 0; padding: 0; max-height: 140px; overflow-y: auto; font-size: 13px; }
.warnings li { padding: 3px 0; cursor: pointer; }

/* position is set here, in both layouts, on purpose: with none, Leaflet
   writes an inline position:relative onto the map when it starts -- which
   then beats the phone layout's position:absolute (the map vanished after
   shrinking the window, and covered the sidebar after widening it again). */
#map { position: relative; flex: 1; background: #07080b url("../images/background.8ee0e7130543.png") center / cover; }
#map.placing { cursor: crosshair; }
.leaflet-container { background: transparent; }
.hive-tip { background: #11151d; color: var(--text); border: 1px solid var(--line); box-shadow: none; }
.hive-tip::before { display: none; }
.leaflet-interactive.origin-ghost { cursor: inherit; }
.mine-tip { display: flex; gap: 8px; align-items: center; }
.mine-tip img { width: 34px; height: 34px; object-fit: contain; }

.ctx-menu {
  position: absolute; z-index: 1000; min-width: 170px; padding: 4px;
  background: #11151d; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, .45);
  display: flex; flex-direction: column; gap: 2px;
}
.ctx-menu[hidden] { display: none; }
.ctx-menu .ctx-title { padding: 4px 8px 6px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); margin-bottom: 2px; }
.ctx-menu button { background: none; border: 0; border-radius: 5px; text-align: left; padding: 6px 8px; }
.ctx-menu button:hover:not(:disabled) { background: #262c3b; }

.confirm-pop {
  position: fixed; z-index: 2000; width: 300px; padding: 12px;
  background: #11151d; border: 1px solid var(--accent); border-radius: 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
}
.confirm-pop .confirm-text { font-size: 13px; line-height: 1.5; }
.confirm-pop .btn-row { justify-content: flex-end; margin-top: 10px; }
.confirm-pop .tab-list { list-style: none; margin: 8px 0; padding: 0; font-size: 13px; }
.confirm-pop .tab-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 3px 0; }
.tab-filter { font-size: 12px; max-width: 45%; }
.member-sort { font-size: 12px; }
.member-list .rk .tg { color: var(--muted); }
.member-list .rk .tc { color: var(--text); white-space: nowrap; }

.toast {
  position: absolute; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 1000; max-width: 90%;
  background: #11151d; border: 1px solid var(--line); border-left: 3px solid var(--ok); border-radius: 8px;
  padding: 10px 14px; box-shadow: 0 6px 20px rgba(0, 0, 0, .4);
}
.toast.error { border-left-color: var(--danger); }
.toast-action { margin-left: 12px; padding: 3px 10px; border-color: var(--accent); color: var(--accent); background: none; }

/* ── phones ─────────────────────────────────────────────────────────────── */
@media (max-width: 800px) {
  /* 16px stops iOS zooming in when a field is focused. */
  input, select { font-size: 16px; }

  /* Top bar: one row; the actions open from ☰ as a full-width list. */
  .topbar { position: relative; gap: 8px; padding: 6px 10px; }
  .brand .brand-name { display: none; }
  .nav-toggle { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 40px; font-size: 20px; padding: 0; }
  .topbar-nav { display: none; }
  .topbar.nav-open .topbar-nav {
    display: flex; flex-direction: column; align-items: stretch; gap: 8px;
    position: absolute; top: 100%; left: 0; right: 0; z-index: 2000; padding: 12px;
    background: var(--panel); border-bottom: 1px solid var(--line); box-shadow: 0 12px 28px rgba(0, 0, 0, .55);
  }
  .topbar-nav > button, .topbar-nav > a.btn, .topbar-nav > select { width: 100%; min-height: 44px; text-align: center; }
  .topbar-nav > a:not(.btn) { padding: 8px 0; text-align: center; }
  .topbar-nav form.inline { display: block; }
  .topbar-nav button.link { width: 100%; min-height: 44px; }
  .topbar-nav .who, .topbar-nav .badge { text-align: center; }

  /* Editor: full-screen map with the sidebar as a bottom sheet. */
  .editor-layout { display: block; }
  #map { position: absolute; inset: 0; }
  .sidebar {
    position: absolute; left: 0; right: 0; bottom: 0; z-index: 900;
    width: auto; height: 58px; padding: 0 10px 12px; gap: 8px;
    border-right: 0; border-top: 1px solid var(--line); border-radius: 14px 14px 0 0;
    box-shadow: 0 -10px 28px rgba(0, 0, 0, .5); overflow: hidden; transition: height .2s ease;
  }
  body.sheet-open .sidebar { height: 62dvh; overflow-y: auto; }
  /* Tapped something on the map: just its card, sized to fit, so most of
     the map stays free to pan and zoom. The handle opens everything. */
  body.sheet-details .sidebar { height: auto; max-height: 45dvh; }
  body.sheet-details .sidebar > .panel:not(#selection-panel) { display: none; }
  .sheet-handle {
    display: flex; flex-direction: column; align-items: center; gap: 6px; order: -2;
    position: sticky; top: 0; z-index: 1; flex-shrink: 0; width: calc(100% + 20px); margin: 0 -10px;
    padding: 8px 12px 10px; min-height: 58px; background: var(--panel); border: 0; border-radius: 14px 14px 0 0;
  }
  .sheet-handle .grab { width: 44px; height: 5px; border-radius: 3px; background: #3a4258; }
  .sheet-summary { font-size: 13px; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #selection-panel { order: -1; }          /* what you tapped comes first */
  #stats { display: none; }                /* it's on the handle */
  .panel.grow { flex: none; min-height: 0; overflow: visible; }
  .member-list { overflow: visible; }      /* the sheet scrolls, not the list */
  .warnings { max-height: none; }

  /* Touch-sized targets. */
  .sidebar button, .ctx-menu button { min-height: 40px; }
  .tool { min-height: 44px; }
  .toggles { font-size: 14px; gap: 0; }
  .toggles label { padding: 7px 0; }
  .member-list li.member { padding: 9px 6px; }
  .warnings li { padding: 8px 0; }

  /* Things that float over the map. */
  .mode-banner { max-width: calc(100% - 20px); font-size: 13px; }
  .toast { top: 62px; bottom: auto; }     /* above the sheet, below the banner */
  .confirm-pop { left: 8px !important; right: 8px; top: 60px !important; width: auto; }
  body.editor .messages { top: 56px; left: 8px; right: 8px; }
}

/* inactive member: the city isn't on the map any more */
.art-inactive { filter: grayscale(1) brightness(.75); opacity: .55; }

/* Update from MightPulse: progress panel and confirm list */
.sync-panel {
  position: fixed; right: 12px; bottom: 12px; z-index: 1500; width: min(420px, calc(100vw - 24px));
  background: #11151d; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
}
.sync-panel.running { border-color: var(--accent); }
.sync-panel[hidden] { display: none; }
.sync-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.sync-log { margin: 0; padding: 8px 10px; max-height: 32vh; overflow: auto; font-size: 12px; white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.sync-log .sync-group { padding-left: 14px; display: flex; gap: 6px; align-items: baseline; }
.sync-log .swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; flex: 0 0 auto; position: relative; top: 1px; }
.sync-targets { margin: 6px 0; padding-left: 18px; }
.confirm-pop label.check { display: flex; gap: 6px; align-items: center; margin: 6px 0; font-size: 13px; }

/* Team page */
.copyable { user-select: all; }
.team-form { max-width: 560px; }
.card label.check { display: flex; align-items: center; gap: 6px; }
.card label.check input { width: auto; display: inline; margin: 0; }
.howto { margin: 4px 0 14px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.howto summary { cursor: pointer; font-weight: 600; }
.howto ol { margin: 10px 0 6px; padding-left: 22px; line-height: 1.6; }

/* Team page: the way back to the map */
.back-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.topbar-nav a.current { font-weight: 600; text-decoration: underline; }
