:root {
  --bg: #1d1b16;
  --panel: #26231c;
  --panel-2: #322e25;
  --line: #4a4436;
  --text: #f2ecdc;
  --muted: #b4ab96;
  --accent: #7fb04f;
  --accent-ink: #15200c;
  --danger: #e2705f;
  --shadow: 0 6px 20px rgb(0 0 0 / 45%);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

#map {
  position: fixed;
  inset: 0;
  background: #14202e;
}

/* Keep each map pixel crisp when zoomed in past the biome resolution. */
.leaflet-tile-pane img { image-rendering: pixelated; }
.leaflet-container { font: inherit; }
.adding .leaflet-container, .adding #map { cursor: crosshair !important; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
button.danger { color: var(--danger); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

input, select, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
}
input[type="color"] { padding: 2px; height: 32px; }

/* Holds the toolbar and the mode bar, so the bar always sits below however the toolbar wraps. */
.top {
  position: fixed;
  top: 10px;
  left: 56px;
  right: 346px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  z-index: 1000;
  pointer-events: none;
}
.top > * { pointer-events: auto; }

.toolbar {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
  box-shadow: var(--shadow);
}
.toolbar h1 { font-size: 15px; margin: 0; white-space: nowrap; }
.goto { display: flex; gap: 6px; align-items: center; }
.goto label { display: flex; gap: 4px; align-items: center; color: var(--muted); }
.goto input { width: 84px; padding: 4px 6px; }
.goto button { padding: 4px 10px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-left: auto; }

.mode-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 6px 12px;
  z-index: 1000;
  box-shadow: var(--shadow);
}
.mode-bar[hidden] { display: none; }

.readout {
  position: fixed;
  left: 10px;
  bottom: 10px;
  background: rgb(29 27 22 / 92%);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
  z-index: 1000;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  min-width: 170px;
}
.readout b { color: var(--muted); font-weight: 600; }
.readout .sub { color: var(--muted); font-size: 12px; }

.panel {
  position: fixed;
  top: 10px;
  right: 10px;
  bottom: 10px;
  width: 326px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 1000;
  overflow: hidden;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1;
  background: none;
  border: 0;
  border-radius: 0;
  padding: 9px 4px;
  color: var(--muted);
}
.tabs button.active { color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.tab {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
}
.tab[hidden] { display: none; }

.list { list-style: none; margin: 8px 0 0; padding: 0; }
.list li { border-bottom: 1px solid var(--line); }
.list .row {
  display: block;
  width: 100%;
  background: none;
  border: 0;
  border-radius: 0;
  padding: 7px 4px;
  text-align: left;
}
.list .row:hover { background: var(--panel-2); }
.row-title { display: flex; align-items: center; gap: 6px; }
.row-title .name { overflow-wrap: anywhere; }
.row-sub { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.swatch {
  flex: none;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  border: 1px solid rgb(255 255 255 / 40%);
}
.group-title {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  margin: 12px 0 0;
}
.empty { color: var(--muted); padding: 10px 4px; }

.history-row { padding: 7px 4px; }
.history-row button { padding: 2px 8px; font-size: 12px; margin-top: 4px; }

.toggles label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  cursor: pointer;
}
.toggles input { width: auto; accent-color: var(--accent); }
.fine { color: var(--muted); font-size: 12px; margin: 12px 0; }
code { overflow-wrap: anywhere; }

.detail {
  position: fixed;
  left: 10px;
  bottom: 74px;
  width: 310px;
  max-height: calc(100vh - 150px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  z-index: 1000;
  box-shadow: var(--shadow);
}
.detail[hidden] { display: none; }
.detail h2 { margin: 0 24px 2px 0; font-size: 16px; overflow-wrap: anywhere; }
.detail .kind { color: var(--muted); font-size: 12px; }
.detail .coords-line {
  font-variant-numeric: tabular-nums;
  margin: 8px 0;
  font-weight: 600;
}
.detail .notes { white-space: pre-wrap; overflow-wrap: anywhere; margin: 8px 0; }
.detail .meta { color: var(--muted); font-size: 12px; margin: 8px 0; }
.detail .buttons { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.detail .close {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 0 7px;
  background: none;
  border: 0;
  font-size: 18px;
  color: var(--muted);
}

dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgb(0 0 0 / 55%); }
dialog h2 { margin: 0 0 10px; font-size: 17px; }
dialog p { margin: 0 0 10px; color: var(--muted); }
dialog label { display: block; margin-bottom: 10px; color: var(--muted); font-size: 13px; }
dialog label input, dialog label select, dialog label textarea { margin-top: 3px; color: var(--text); }
.coords { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.opt { font-size: 11px; opacity: 0.8; }
.error { color: var(--danger) !important; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 14px;
  z-index: 2000;
  box-shadow: var(--shadow);
}
.toast[hidden] { display: none; }

.pin { filter: drop-shadow(0 1px 2px rgb(0 0 0 / 70%)); }
.place-label.leaflet-tooltip {
  background: rgb(20 18 14 / 82%);
  color: #fff;
  border: 0;
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 12px;
  box-shadow: none;
}
.place-label.leaflet-tooltip::before { display: none; }
.territory-label.leaflet-tooltip {
  background: transparent;
  border: 0;
  box-shadow: none;
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  text-shadow: 0 0 3px #000, 0 0 6px #000;
}
.hide-labels .place-label { display: none; }

.me-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: var(--panel-2);
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 8px 12px;
  text-align: left;
}
.me-chip .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-hint { margin-left: auto; color: var(--muted); font-size: 12px; white-space: nowrap; }
.badge {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 4px;
  padding: 0 6px;
  font-size: 11px;
  font-weight: 700;
}
.swatch.round { border-radius: 50%; background: #fff; border-color: #14120e; }
.swatch.mixed { background: linear-gradient(135deg, #c01111 0 33%, #6e1895 33% 66%, #16819c 66%); }
.key-row { display: flex; align-items: center; gap: 8px; padding: 3px 4px; }
.owner-line { display: flex; align-items: center; gap: 6px; color: var(--text) !important; margin: 4px 0 10px; }
.plain-row { display: flex !important; align-items: center; gap: 8px; padding: 3px 0; margin: 0 !important; color: var(--text) !important; }
.plain-row input { width: auto; margin: 0 !important; accent-color: var(--accent); }
.inline-check { display: flex !important; align-items: center; gap: 8px; margin: 8px 0 !important; color: var(--text) !important; cursor: pointer; }
.inline-check input { width: auto; margin: 0 !important; accent-color: var(--accent); }
.check-list { max-height: 150px; overflow-y: auto; padding: 4px 0; }
details { margin: 6px 0; }
summary { cursor: pointer; padding: 3px 0; }
.stack { display: flex; flex-direction: column; gap: 6px; padding: 6px 0; }
.player-controls {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
}
.swatches { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 0 10px; }
.pick {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 6px;
  border: 2px solid rgb(255 255 255 / 25%);
}
.pick.chosen { outline: 3px solid var(--text); outline-offset: 1px; }
.pick:disabled { opacity: 0.18; cursor: not-allowed; }
.field-label { color: var(--muted); font-size: 13px; }
.segmented { display: flex; margin-bottom: 12px; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.segmented button { flex: 1; border: 0; border-radius: 0; background: none; color: var(--muted); }
.segmented button.active { background: var(--panel-2); color: var(--text); font-weight: 600; }
.warn { color: #f0c36a !important; margin: 4px 0 10px; }
.corner-handle { background: #fff; border: 2px solid #14120e; border-radius: 3px; cursor: grab; }

.world-history {
  position: fixed;
  inset: 0;
  z-index: 1500;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.wh-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.wh-header h2 { margin: 0; font-size: 17px; }
.wh-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-left: auto; }
.wh-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 270px 1fr; }
.wh-list { overflow-y: auto; padding: 8px 12px; border-right: 1px solid var(--line); background: var(--panel); }
.wh-list .row.selected { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--accent); }
.wh-main { overflow-y: auto; padding: 18px 24px 32px; }
.wh-main > * { max-width: 1000px; margin-inline: auto; }
.wh-empty { color: var(--muted); padding-top: 40px; }
.wh-empty h3 { color: var(--text); }
.doc-title { font-size: 24px; margin: 0 0 4px; overflow-wrap: anywhere; }
.doc-body { font-size: 15px; line-height: 1.6; overflow-wrap: anywhere; }
.doc-body h3 { font-size: 21px; margin: 22px 0 6px; }
.doc-body h4 { font-size: 17px; margin: 18px 0 4px; }
.doc-body h5 { font-size: 15px; margin: 14px 0 4px; color: var(--muted); }
.doc-body p { margin: 8px 0; }
.table-wrap { overflow-x: auto; margin: 10px 0; }
.doc-body table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
.doc-body th, .doc-body td { border: 1px solid var(--line); padding: 5px 8px; text-align: left; vertical-align: top; }
.doc-body th { background: var(--panel-2); }
.coord-link {
  display: inline;
  padding: 0;
  background: none;
  border: 0;
  border-radius: 0;
  color: #9ecf6c;
  text-decoration: underline;
  font-variant-numeric: tabular-nums;
}
.doc-editor h3, .generator h3 { margin-top: 0; }
.editor-row { display: grid; grid-template-columns: 1fr 170px; gap: 8px; }
.editor-split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 6px; }
.editor-split textarea { min-height: 380px; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.doc-preview {
  height: 380px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 2px 12px;
  background: var(--panel);
}
.generator { max-width: 640px !important; }
.generator progress { width: 100%; accent-color: var(--accent); }

.tabs .help-button { flex: none; width: 30px; padding: 0; color: var(--muted); }
.wide { width: 100%; }
.small { padding: 2px 8px; font-size: 12px; }
.tight { margin: 2px 0 !important; }
.mark { flex: none; display: block; object-fit: contain; background: rgb(20 18 14 / 70%); border: 2px solid; border-radius: 6px; }
.mark.shield {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 0;
  color: #fff;
  font-weight: 800;
  text-shadow: 0 1px 2px #000;
  clip-path: polygon(0 0, 100% 0, 100% 62%, 50% 100%, 0 62%);
}
.faction-label { display: inline-flex; flex-direction: column; align-items: center; gap: 2px; }
.hide-labels .faction-label-text { display: none; }
.faction-head { display: flex; gap: 10px; align-items: center; margin: 8px 0; }
.faction-head .owner-line { margin: 4px 0 0; }
.motto { font-style: italic; overflow-wrap: anywhere; }
.link-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0;
  background: none;
  border: 0;
  border-radius: 0;
  color: #9ecf6c;
  text-align: left;
  overflow-wrap: anywhere;
}
.member-row { display: flex; align-items: center; gap: 8px; padding: 5px 0; }
.member-row > :first-child { flex: 1; min-width: 0; }
.role-select { width: auto; padding: 2px 4px; font-size: 12px; }
.add-member { display: flex; gap: 6px; margin-top: 8px; }
.add-member select { flex: 1; min-width: 0; }
.add-member button { flex: none; }
.logo-row { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 10px; }
.logo-controls { flex: 1; min-width: 0; }
.logo-controls label { margin-bottom: 4px; }
.help-list { margin: 0 0 12px; padding-left: 18px; }
.help-list li { margin-bottom: 8px; }

@media (max-width: 800px) {
  .top { left: 8px; right: 8px; top: 8px; }
  .toolbar { padding: 6px 8px; }
  .toolbar h1 { display: none; }
  .goto input { width: 66px; }
  .actions { margin-left: 0; }
  .actions button { padding: 5px 8px; }
  .leaflet-top.leaflet-left { top: 136px; }
  .mode-bar { width: 100%; justify-content: center; }
  .panel {
    top: auto;
    left: 8px;
    right: 8px;
    bottom: 8px;
    width: auto;
    height: 32vh;
  }
  .readout { bottom: calc(32vh + 16px); left: 8px; }
  .detail {
    left: 8px;
    right: 8px;
    width: auto;
    bottom: 8px;
    max-height: 60vh;
    z-index: 1001;
  }
  .toast { bottom: calc(32vh + 70px); width: max-content; max-width: calc(100vw - 32px); }
  .wh-layout { grid-template-columns: 1fr; grid-template-rows: 30vh 1fr; }
  .wh-list { border-right: 0; border-bottom: 1px solid var(--line); }
  .wh-main { padding: 12px 16px 24px; }
  .editor-split, .editor-row { grid-template-columns: 1fr; }
}
