:root {
  --ink: #33251a;
  --sepia: #6d5235;
  --paper: #f3e7cb;
  --paper-2: #e9d9b4;
  --paper-3: #fbf4e2;
  --edge: #b89a6a;
  --seal: #8e2a1f;
  --table: #1f1813;
  --sea: #8fb3b9;
  --ok: #4f6b3a;
  --gap: max(14px, env(safe-area-inset-top));
  --sb: max(16px, env(safe-area-inset-bottom));
  /* Desktop: where the toolbar and the tool options start (below the cartouche, whose height
     app.js keeps in --cart-h), and the bottom-left strip they must keep clear of the scale and
     zoom cards. */
  --tools-top: calc(var(--gap) + var(--cart-h, 70px) + 28px);
  --bl-h: calc(var(--sb) + 232px);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  background: var(--table);
  color: var(--ink);
  font: 16px/1.4 "EB Garamond", Georgia, serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--seal); outline-offset: 2px; }

#map {
  position: fixed; inset: 0; width: 100%; height: 100%;
  touch-action: none; cursor: grab; display: block;
}
#map.painting { cursor: crosshair; }
#map.pointing { cursor: pointer; }
#map.dragging { cursor: grabbing; }

.card {
  background: var(--paper);
  border: 1px solid var(--edge);
  border-radius: 3px;
  box-shadow: 0 8px 28px rgba(0,0,0,.45), inset 0 0 0 3px var(--paper), inset 0 0 0 4px rgba(109,82,53,.32);
}

/* Role / mode visibility */
body[data-mode="spieler"] .sl-only { display: none !important; }
body[data-role="spieler"] .sl-role { display: none !important; }
body[data-state="laden"] .tools, body[data-state="laden"] .zoom, body[data-state="laden"] .scale,
body[data-state="laden"] .compass, body[data-state="laden"] .hint, body[data-state="laden"] .cartouche,
body[data-state="leer"] .tools, body[data-state="leer"] .zoom, body[data-state="leer"] .scale,
body[data-state="leer"] .compass, body[data-state="leer"] .hint, body[data-state="leer"] .cartouche,
body[data-state="fehler"] .tools, body[data-state="fehler"] .zoom, body[data-state="fehler"] .scale,
body[data-state="fehler"] .compass, body[data-state="fehler"] .hint, body[data-state="fehler"] .cartouche,
body[data-state="fehler"] .modebox { display: none; }

/* Title cartouche */
.cartouche {
  position: fixed; top: var(--gap); left: 16px;
  padding: 10px 18px 9px; text-align: center; z-index: 7; max-width: calc(100vw - 340px);
}
.cartouche h1 {
  margin: 0; font: 400 25px/1.1 "IM Fell English SC", Georgia, serif; letter-spacing: .04em;
  overflow-wrap: anywhere;
}
.cartouche p { margin: 2px 0 0; font-style: italic; font-size: 14px; color: var(--sepia); }
.link {
  background: none; border: 0; padding: 0; margin-left: 8px; cursor: pointer;
  font-style: italic; font-size: 14px; color: var(--seal); text-decoration: underline; text-underline-offset: 2px;
}
.link:hover { color: #6a1d14; }

/* Karte menu */
.menu {
  position: fixed; left: 16px; top: calc(var(--gap) + var(--cart-h, 70px) + 4px); z-index: 20;
  display: flex; flex-direction: column; padding: 6px; min-width: 240px;
}
.menu button {
  text-align: left; border: 0; background: transparent; padding: 7px 12px; cursor: pointer; border-radius: 2px; font-size: 16px;
}
.menu button:hover, .menu button:focus-visible { background: var(--paper-2); }
.menu hr { border: 0; border-top: 1px solid rgba(109,82,53,.3); margin: 4px 8px; width: auto; }

/* Feld suchen */
.finder {
  position: fixed; left: 16px; top: calc(var(--gap) + var(--cart-h, 70px) + 4px); z-index: 20;
  padding: 10px 12px 12px; width: 250px; display: flex; flex-direction: column; gap: 4px;
}
.findrow { display: flex; gap: 6px; }
.findrow input {
  flex: 1; min-width: 0; font-size: 18px; letter-spacing: .08em; color: var(--ink); background: var(--paper-3);
  border: 1px solid var(--edge); border-radius: 2px; padding: 3px 8px;
}
.finder .err { font-size: 14px; line-height: 1.3; }

/* Mode switch, save status */
.modebox {
  position: fixed; top: var(--gap); right: 16px; z-index: 7; text-align: right;
  display: flex; flex-direction: column; align-items: flex-end; gap: 8px; pointer-events: none;
}
.modebox > * { pointer-events: auto; }
/* The row itself lets clicks through (on phones its box reaches over the cartouche). */
.modebox > .toprow { pointer-events: none; }
.toprow > * { pointer-events: auto; }
.toprow { display: flex; align-items: center; gap: 10px; }
.seg { display: inline-flex; padding: 4px; gap: 2px; }
.seg button {
  border: 0; background: transparent; padding: 6px 14px; cursor: pointer; border-radius: 2px;
  font: 400 16px "IM Fell English SC", Georgia, serif; letter-spacing: .03em; color: var(--sepia);
}
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.ribbon {
  display: none; padding: 4px 12px; font-style: italic; font-size: 14px;
  background: var(--seal); color: #f6e6cf; border-radius: 2px; box-shadow: 0 4px 14px rgba(0,0,0,.4);
}
body[data-mode="spieler"][data-role="sl"] .ribbon { display: inline-block; }

.status {
  display: inline-flex; align-items: center; gap: 7px; flex-wrap: wrap; justify-content: flex-end;
  padding: 5px 12px; border-radius: 16px; font-style: italic; font-size: 14px;
  background: rgba(31,24,19,.82); color: #eadcbc; max-width: 520px;
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: #9fbf7a; flex: none; }
.status[data-state="saving"] .dot, .status[data-state="dirty"] .dot { background: #e2c26b; animation: pulse 1.1s ease-in-out infinite; }
.status[data-state="retry"] .dot, .status[data-state="error"] .dot, .status[data-state="conflict"] .dot { background: #e0745f; }
.status[data-state="lokal"] .dot { background: #c9b48a; }
.status[data-state="conflict"], .status[data-state="error"] { background: rgba(110,26,18,.92); color: #f6e6cf; }
.statusbtns { display: inline-flex; gap: 6px; }
.statusbtns button {
  border: 1px solid rgba(246,230,207,.6); background: rgba(246,230,207,.12); color: #f6e6cf; border-radius: 12px;
  padding: 1px 10px; font-size: 14px; font-style: normal; cursor: pointer;
}
.statusbtns button:hover { background: rgba(246,230,207,.26); }
@keyframes pulse { 50% { opacity: .35; } }
/* Desktop windows where the pill would cover the map name: it moves below the cartouche and the
   mode switch (app.js sets the class and keeps the pill's bottom edge in --status-b). */
@media (min-width: 701px) {
  body.status-below .status {
    position: fixed; right: 16px; top: calc(var(--gap) + max(var(--cart-h, 70px), 42px) + 8px);
    max-width: max(240px, calc(100vw - 400px));
  }
  body.status-below[data-mode="spieler"][data-role="sl"] .status { top: calc(var(--gap) + max(var(--cart-h, 70px), 76px) + 8px); }
  body.status-below[data-role] { --panel-top: calc(var(--status-b, 120px) + 10px); }
}

/* Toolbar */
.tools {
  position: fixed; left: 16px; top: var(--tools-top); z-index: 5;
  display: flex; flex-direction: column; padding: 6px; gap: 4px;
  max-height: calc(100% - var(--tools-top) - var(--bl-h));
}
/* Scrolls (instead of running into the scale card) when the window is too short for all tools. */
.toolrow { display: flex; flex-direction: column; gap: 3px; min-height: 0; overflow-y: auto; scrollbar-width: thin; }
.histrow { flex: none; }
.tools button {
  width: 76px; padding: 6px 0 4px; border: 1px solid transparent; border-radius: 2px;
  background: transparent; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 1px;
  font-size: 12.5px; color: var(--sepia); line-height: 1.15;
}
.tools button svg { width: 23px; height: 23px; stroke: currentColor; fill: none; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.tools button:hover { background: var(--paper-2); }
.tools button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.histrow { display: flex; gap: 2px; border-top: 1px solid rgba(109,82,53,.3); padding-top: 5px; margin-top: 1px; }
.histrow button { width: 37px; padding: 5px 0; }
.histrow button:disabled { opacity: .35; cursor: default; background: transparent; }
/* Bastion button: its own group between the tools and Rückgängig / Wiederholen. */
.bastrow { flex: none; display: flex; border-top: 1px solid rgba(109,82,53,.3); padding-top: 3px; }
.bastrow button { position: relative; padding: 3px 0 2px; }
.bdot {
  position: absolute; top: 2px; right: 9px; min-width: 9px; height: 9px; padding: 0 1px; box-sizing: content-box;
  border-radius: 6px; background: var(--seal); box-shadow: 0 0 0 1.5px var(--paper);
  color: #f6e6cf; font: 9px/9px "EB Garamond", Georgia, serif; text-align: center; pointer-events: none;
}
.bdot[hidden] { display: none; }
.bdot[data-n]::after { content: attr(data-n); }
@media (prefers-reduced-motion: no-preference) {
  .bdot { animation: bdot-puls 1.6s ease-in-out infinite alternate; }
  @keyframes bdot-puls { from { opacity: 1; } to { opacity: .55; } }
}
.pbastion .bbadge + .btnrow { margin-top: 6px; }

/* Tool options */
.sub {
  position: fixed; left: 112px; top: var(--tools-top); z-index: 5; padding: 10px 12px 12px; width: 252px;
  max-height: calc(100% - var(--tools-top) - var(--bl-h)); overflow: auto; overscroll-behavior: contain;
}
.sub > div { display: none; }
.sub > div.on { display: block; }
.sub h4 { margin: 0 0 6px; font: 400 15px "IM Fell English SC", Georgia, serif; color: var(--sepia); letter-spacing: .03em; }
.sub h4.mt { margin-top: 12px; }
.small { font-size: 14px; font-style: italic; color: var(--sepia); margin: 8px 0 0; line-height: 1.3; }
.chips { display: flex; flex-direction: column; gap: 3px; }
.chip {
  display: flex; align-items: center; gap: 9px; text-align: left; cursor: pointer;
  border: 1px solid transparent; background: transparent; padding: 3px 8px 3px 5px; border-radius: 2px; font-size: 15px;
}
.chip:hover { background: var(--paper-2); }
.chip[aria-pressed="true"] { border-color: var(--ink); background: var(--paper-2); }
.chip small { display: block; font-style: italic; color: var(--sepia); font-size: 12.5px; line-height: 1.15; }
#terrain-chips { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; }
.sw { width: 22px; height: 22px; flex: none; border: 1px solid var(--ink); border-radius: 2px; }
.sw.explored { background: linear-gradient(135deg, #a9bf88 50%, #e6cf98 50%); }
.sw.sighted { background: repeating-linear-gradient(135deg, #e7dbbd 0 3px, #c9cfa4 3px 6px); }
.sw.hidden { background: #efe2c2; box-shadow: inset 0 0 6px rgba(61,42,24,.55); }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 15px; cursor: pointer; margin: 8px 0 0; }
.check input { margin-top: 5px; accent-color: var(--seal); flex: none; }
.check small { display: block; font-style: italic; color: var(--sepia); font-size: 13px; line-height: 1.25; }

.seg2 { display: flex; border: 1px solid var(--edge); border-radius: 2px; overflow: hidden; }
.seg2 button {
  flex: 1; border: 0; background: transparent; padding: 4px 6px; cursor: pointer; font-size: 14.5px; line-height: 1.2;
}
.seg2 button + button { border-left: 1px solid var(--edge); }
.seg2 button:hover { background: var(--paper-2); }
.seg2 button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.seg2.brush button { display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 4px 2px 3px; }
.seg2.brush svg { width: 26px; height: 22px; }
.seg2.brush small { font-size: 12.5px; }

.palette { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; }
/* Places and monsters together: a wider panel, so both palettes fit in four columns. */
@media (min-width: 701px) { .sub:has(> [data-for="places"].on) { width: 284px; } }
.palette button {
  display: flex; flex-direction: column; align-items: center; gap: 0; padding: 3px 2px 4px; border: 1px solid transparent;
  background: transparent; cursor: pointer; border-radius: 2px; font-size: 12.5px; line-height: 1.1; text-align: center;
}
.palette button:hover { background: var(--paper-2); }
.palette button[aria-pressed="true"] { border-color: var(--ink); background: var(--paper-2); }
.measure-out { margin: 2px 0 0; font-size: 16px; white-space: pre-line; }

.btnrow { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.btn {
  border: 1px solid var(--edge); background: var(--paper-3); cursor: pointer; border-radius: 2px;
  padding: 4px 12px; font-size: 15px; line-height: 1.3;
}
.btn:hover { background: var(--paper-2); }
.btn.primary { background: var(--seal); border-color: var(--seal); color: #f6e6cf; font: 400 15px "IM Fell English SC", Georgia, serif; letter-spacing: .04em; padding: 5px 16px; }
.btn.primary:hover { background: #7a2318; }
.btn.primary:disabled { opacity: .45; cursor: default; }
.btn.danger { color: var(--seal); border-color: rgba(142,42,31,.55); }
.btn.danger:hover { background: rgba(142,42,31,.1); }
.btn.icon { padding: 3px 6px; display: inline-grid; place-items: center; }
.btn.icon svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.5; }

/* Hex details */
.panel {
  position: fixed; right: 16px; top: var(--panel-top, calc(var(--gap) + 66px)); z-index: 6; width: 340px;
  max-height: calc(100% - var(--panel-top, calc(var(--gap) + 66px)) - 16px); display: flex; flex-direction: column; overflow: hidden;
}
.panelbody { overflow: auto; overscroll-behavior: contain; padding: 16px 18px 18px; min-height: 0; flex: 1 1 auto; }
body[data-mode="spieler"][data-role="sl"] { --panel-top: calc(var(--gap) + 100px); }
.panel .close {
  position: absolute; top: 6px; right: 8px; z-index: 1; border: 0; cursor: pointer; border-radius: 50%;
  background: radial-gradient(circle, var(--paper) 60%, rgba(243,231,203,0) 72%);
  font-size: 26px; line-height: 1; color: var(--sepia); padding: 4px 6px;
}
.hexno { font: 400 14px "IM Fell English SC", Georgia, serif; letter-spacing: .08em; color: var(--sepia); }
.terrainline { display: flex; align-items: center; gap: 8px; margin-top: 2px; font-size: 17px; flex-wrap: wrap; }
.terrainline .sw { width: 16px; height: 16px; }
.badge { font-size: 12.5px; font-style: italic; padding: 0 7px; border: 1px solid var(--edge); border-radius: 10px; color: var(--sepia); }
.statebtns { display: flex; gap: 4px; margin: 10px 0 2px; }
.statebtns button {
  flex: 1; padding: 4px 0; border: 1px solid var(--edge); background: transparent; cursor: pointer; border-radius: 2px; font-size: 14px;
}
.statebtns button:hover { background: var(--paper-2); }
.statebtns button[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.panel h2 { margin: 14px 0 2px; font: 400 23px/1.15 "IM Fell English", Georgia, serif; overflow-wrap: anywhere; }
.panel h3 { margin: 18px 0 6px; font: 400 15px "IM Fell English SC", Georgia, serif; letter-spacing: .04em; color: var(--sepia); }
.panel p { margin: 4px 0; }
.desc { white-space: pre-line; overflow-wrap: anywhere; }
.blurb { font-style: italic; color: var(--sepia); margin-top: 12px !important; }

.placeform { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.formhead { display: flex; align-items: center; gap: 8px; }
.formicon { width: 44px; height: 44px; flex: none; display: grid; place-items: center; background: radial-gradient(circle, rgba(242,229,198,.95) 55%, rgba(242,229,198,0) 72%); }
.title-input {
  flex: 1; min-width: 0; font: 400 22px/1.2 "IM Fell English", Georgia, serif !important;
  border: 0; border-bottom: 1px dashed var(--edge); background: transparent; padding: 2px 0;
}
.title-input:focus { outline: none; border-bottom: 1px solid var(--seal); }
.field { display: flex; flex-direction: column; gap: 2px; font-size: 14px; color: var(--sepia); font-style: italic; }
.field input, .field select, .field textarea, input.wide {
  font-style: normal; font-size: 15px; color: var(--ink); background: var(--paper-3);
  border: 1px solid var(--edge); border-radius: 2px; padding: 5px 8px; width: 100%;
}
.field textarea { resize: vertical; min-height: 44px; }
.secretfield { color: var(--seal); }
.secretfield textarea { background: #f7e9df; border-color: rgba(142,42,31,.4); }
.confirm { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; padding: 6px 8px; background: rgba(142,42,31,.08); border-left: 3px solid var(--seal); }
.confirm span { font-style: italic; margin-right: 4px; }

.notes { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.notes li {
  position: relative; background: var(--paper-3); border: 1px solid var(--paper-2); padding: 6px 30px 6px 10px;
  transform: rotate(-.4deg); box-shadow: 0 1px 2px rgba(0,0,0,.12); overflow-wrap: anywhere;
}
.notes li:nth-child(even) { transform: rotate(.5deg); }
.notes li .text { white-space: pre-line; }
.notes li cite { display: block; font-size: 13.5px; color: var(--sepia); }
.notes li button {
  position: absolute; top: 2px; right: 4px; border: 0; background: none; cursor: pointer; color: var(--sepia); font-size: 18px; line-height: 1.2;
  padding: 0 4px; border-radius: 2px;
}
.notes li button.armed { font-size: 13px; font-style: italic; background: var(--seal); color: #f6e6cf; top: 4px; padding: 1px 6px; }
.empty { font-style: italic; color: var(--sepia); font-size: 15px; margin: 0; }
form.addnote { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
form.addnote input, form.addnote textarea {
  font-size: 15px; color: var(--ink); background: var(--paper-3);
  border: 1px solid var(--edge); border-radius: 2px; padding: 6px 8px; resize: vertical;
}
form.addnote textarea { min-height: 56px; }
form.addnote button {
  align-self: flex-end; border: 0; background: var(--seal); color: #f6e6cf; padding: 6px 16px; border-radius: 2px; cursor: pointer;
  font: 400 15px "IM Fell English SC", Georgia, serif; letter-spacing: .04em;
}
form.addnote button:disabled { opacity: .5; cursor: default; }

/* Zoom, scale, compass, hint */
.zoom { position: fixed; left: 16px; bottom: max(16px, env(safe-area-inset-bottom)); z-index: 5; display: flex; flex-direction: column; padding: 4px; }
.zoom button {
  width: 38px; height: 34px; border: 0; background: transparent; cursor: pointer; font-size: 22px; line-height: 1; color: var(--ink);
  display: grid; place-items: center; border-radius: 2px;
}
.zoom button:hover { background: var(--paper-2); }
.zoom svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.6; }
.scale {
  position: fixed; left: 16px; bottom: calc(max(16px, env(safe-area-inset-bottom)) + 156px); z-index: 4;
  pointer-events: none; color: var(--ink); font-size: 12.5px; line-height: 1;
  background: rgba(243,231,203,.92); border: 1px solid var(--edge); border-radius: 3px; padding: 6px 22px 5px 9px;
  box-shadow: 0 4px 14px rgba(0,0,0,.35);
}
.scalebar { display: flex; height: 6px; border: 1px solid var(--ink); width: 100px; }
.scalebar span { flex: 1; }
.scalebar span:first-child { background: var(--ink); }
.scalebar span:last-child { background: var(--paper); }
.scalelabels { display: flex; justify-content: space-between; margin-top: 3px; gap: 8px; }
.scalelabels span:last-child { transform: translateX(50%); white-space: nowrap; }
.scaletitle { font: italic 12px "IM Fell English", Georgia, serif; margin-top: 2px; color: var(--sepia); }
.compass {
  position: fixed; right: 22px; bottom: max(18px, env(safe-area-inset-bottom)); width: 104px; height: 104px; z-index: 4;
  pointer-events: none; filter: drop-shadow(0 4px 8px rgba(0,0,0,.45));
}
.hint {
  position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 4;
  background: rgba(31,24,19,.82); color: #eadcbc; padding: 6px 14px; border-radius: 16px; font-style: italic; font-size: 14.5px;
  pointer-events: none; white-space: nowrap; max-width: calc(100vw - 300px); overflow: hidden; text-overflow: ellipsis;
}
.hint:empty { display: none; }
.toast {
  position: fixed; left: 50%; bottom: calc(max(16px, env(safe-area-inset-bottom)) + 46px); transform: translateX(-50%); z-index: 30;
  background: var(--ink); color: var(--paper); padding: 8px 16px; border-radius: 3px; font-size: 15px;
  box-shadow: 0 6px 20px rgba(0,0,0,.5); width: max-content; max-width: min(520px, calc(100vw - 32px)); text-align: center;
}
.toast.error { background: var(--seal); color: #f6e6cf; }

.notice {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 8;
  width: min(440px, calc(100vw - 32px)); padding: 22px 26px; text-align: center;
}
.notice h2 { margin: 0 0 6px; font: 400 24px/1.2 "IM Fell English SC", Georgia, serif; letter-spacing: .03em; }
.notice p { margin: 0; font-style: italic; color: var(--sepia); }

/* Dialogs */
.dlg {
  padding: 18px 22px 18px; width: min(480px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow: auto;
  color: var(--ink);
}
.dlg::backdrop { background: rgba(20,15,11,.55); }
.dlg h2 { margin: 0 0 10px; font: 400 23px/1.2 "IM Fell English SC", Georgia, serif; letter-spacing: .03em; }
.dlg form { display: flex; flex-direction: column; gap: 10px; }
.dlg .small, .dlg p:not([class]) { margin: 0; }
.dlgbtns { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.warn { margin: 0; font-size: 14.5px; font-style: italic; color: var(--seal); padding: 6px 10px; background: rgba(142,42,31,.07); border-left: 3px solid var(--seal); }
.warn .link { margin: 0; font-size: inherit; }
.err { margin: 0; color: var(--seal); font-size: 14.5px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seedrow { display: flex; gap: 6px; align-items: stretch; }
.newgrid { display: grid; grid-template-columns: 1fr 222px; gap: 14px; align-items: start; }
.newgrid > div { display: flex; flex-direction: column; gap: 8px; }
.thumb { margin: 0; }
.thumb canvas { display: block; width: 220px; height: 170px; border: 1px solid var(--edge); background: #f1e3c3; }
.thumb figcaption { font-size: 13.5px; font-style: italic; color: var(--sepia); margin-top: 3px; text-align: center; }
.exgrid { display: grid; grid-template-columns: 210px 1fr; gap: 16px; align-items: center; }
.sides {
  margin: 0; padding: 0; border: 0; display: grid; grid-template-columns: auto 1fr auto; grid-template-rows: auto 1fr auto;
  gap: 4px; align-items: center; justify-items: center;
}
.side { cursor: pointer; font-size: 14px; }
.side input { position: absolute; opacity: 0; pointer-events: none; }
.side span { display: inline-block; padding: 2px 8px; border: 1px solid var(--edge); border-radius: 2px; background: var(--paper-3); }
.side input:checked + span { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.side input:focus-visible + span { outline: 2px solid var(--seal); outline-offset: 2px; }
.side.n { grid-column: 2; grid-row: 1; }
.side.w { grid-column: 1; grid-row: 2; }
.side.o { grid-column: 3; grid-row: 2; }
.side.s { grid-column: 2; grid-row: 3; }
.exbox { grid-column: 2; grid-row: 2; position: relative; width: 96px; height: 80px; }
.exold, .exnew { position: absolute; border: 1px solid var(--ink); }
.exold { background: repeating-linear-gradient(135deg, #e2e3ad 0 4px, #a9bf88 4px 8px); }
.exnew { background: repeating-linear-gradient(45deg, rgba(142,42,31,.25) 0 3px, rgba(142,42,31,.08) 3px 6px); border-style: dashed; border-color: var(--seal); }
.expreview { font-size: 14.5px; margin: 6px 0 0; white-space: pre-line; line-height: 1.45; }

/* Laptop heights: a slimmer toolbar and a flat zoom row, so the toolbar never runs into the
   scale and zoom cards in the bottom-left corner. */
@media (min-width: 701px) and (max-height: 820px) {
  /* Zoom row (42 px) + gap + scale card with its title (45 px) + air. */
  :root { --bl-h: calc(var(--sb) + 52px + 45px + 10px); }
  .tools { padding: 5px; gap: 3px; }
  .toolrow { gap: 1px; }
  .tools button { padding: 3px 0 2px; font-size: 12px; }
  .tools button svg { width: 20px; height: 20px; }
  .histrow { padding-top: 3px; }
  .histrow button { padding: 3px 0; }
  .bastrow { padding-top: 3px; }
  .bastrow button { padding: 3px 0; }
  .bastrow button svg { width: 20px; height: 20px; }
  .zoom { flex-direction: row; }
  .zoom button { width: 36px; height: 32px; }
  .scale { bottom: calc(max(16px, env(safe-area-inset-bottom)) + 52px); }
}
/* Very short windows (small laptops, phones turned sideways): the tool names give way to the
   icons (names stay as tooltips), in two columns with Rückgängig / Wiederholen side by side. */
@media (min-width: 701px) and (max-height: 640px) {
  :root { --tools-top: calc(var(--gap) + var(--cart-h, 70px) + 8px); --bl-h: calc(var(--sb) + 52px + 35px + 8px); }
  .tools button .tl { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .tools button { width: 44px; padding: 5px 0; }
  .toolrow { display: grid; grid-template-columns: 44px 44px; gap: 2px; align-content: start; }
  .histrow { gap: 2px; }
  .histrow button { width: 44px; padding: 5px 0; }
  /* Three columns, so the Bastion button shares the last row with Rückgängig / Wiederholen. */
  .tools { flex-flow: row wrap; align-content: flex-start; gap: 2px; width: 148px; }
  .toolrow { flex: 1 1 100%; grid-template-columns: repeat(3, 44px); }
  .bastrow { padding-top: 3px; }
  .bastrow button { width: 44px; padding: 5px 0; }
  .histrow { flex: none; margin-top: 0; }
  .bdot { right: 4px; }
  .sub { left: 174px; width: 284px; }
  .scaletitle { display: none; }
}

@media (max-width: 700px) {
  :root { --gap: max(10px, env(safe-area-inset-top)); }
  .cartouche { left: 10px; padding: 6px 12px 5px; max-width: calc(100vw - 150px); }
  .cartouche h1 { font-size: 18px; }
  .cartouche p { font-size: 12.5px; line-height: 1.25; }
  .link { font-size: 13px; margin-left: 4px; }
  .menu { left: 10px; top: calc(var(--gap) + var(--cart-h, 56px) + 4px); }
  .finder { left: 10px; top: calc(var(--gap) + var(--cart-h, 56px) + 4px); width: min(280px, calc(100vw - 20px)); }
  .modebox { right: 10px; gap: 6px; }
  .toprow { flex-direction: column-reverse; align-items: flex-end; gap: 6px; }
  .seg button { padding: 5px 9px; font-size: 14px; }
  /* Below the cartouche (its height is kept in --cart-h by app.js), so the two never overlap. */
  .status {
    position: fixed; right: 10px; top: calc(var(--gap) + var(--cart-h, 60px) + 6px);
    font-size: 12.5px; padding: 3px 10px; max-width: calc(100vw - 20px);
  }
  body[data-mode="spieler"][data-role="sl"] .status { top: calc(var(--gap) + max(var(--cart-h, 60px), 68px) + 6px); }
  .ribbon { font-size: 12.5px; padding: 3px 9px; }
  .tools {
    top: auto; left: 8px; right: 8px; bottom: max(8px, env(safe-area-inset-bottom));
    flex-direction: row; padding: 4px; gap: 4px; align-items: stretch;
  }
  .tools { max-height: none; }
  .toolrow { flex-direction: row; overflow-x: auto; overflow-y: hidden; flex: 1; gap: 2px; scrollbar-width: none; }
  .toolrow::-webkit-scrollbar { display: none; }
  /* More tools beyond the edge: fade that edge out (app.js sets the classes while scrolling). */
  .toolrow.more-r { -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 34px), transparent); mask-image: linear-gradient(to right, #000 calc(100% - 34px), transparent); }
  .toolrow.more-l { -webkit-mask-image: linear-gradient(to left, #000 calc(100% - 34px), transparent); mask-image: linear-gradient(to left, #000 calc(100% - 34px), transparent); }
  .toolrow.more-l.more-r { -webkit-mask-image: linear-gradient(to right, transparent, #000 34px, #000 calc(100% - 34px), transparent); mask-image: linear-gradient(to right, transparent, #000 34px, #000 calc(100% - 34px), transparent); }
  body[data-mode="spieler"] .toolrow { justify-content: center; }
  .tools button { width: 62px; flex: none; font-size: 11.5px; padding: 5px 0 3px; }
  .tools button svg { width: 22px; height: 22px; }
  .histrow { flex-direction: column; border-top: 0; border-left: 1px solid rgba(109,82,53,.3); padding: 0 0 0 4px; margin: 0; }
  .histrow button { width: 38px; flex: 1; padding: 2px 0; }
  .histrow button svg { width: 20px; height: 20px; }
  .bastrow { border-top: 0; border-left: 1px solid rgba(109,82,53,.3); padding: 0 0 0 4px; margin: 0; }
  .bastrow button { width: 62px; padding: 5px 0 3px; }
  .bdot { right: 12px; }
  .sub {
    left: 8px; right: 8px; width: auto; top: auto; max-height: 36vh;
    bottom: calc(max(8px, env(safe-area-inset-bottom)) + 66px);
  }
  .chips { flex-direction: row; flex-wrap: wrap; }
  .chip small { display: none; }
  #terrain-chips { grid-template-columns: repeat(4, 1fr); }
  #terrain-chips .chip { flex-direction: column; gap: 2px; font-size: 12.5px; padding: 3px 2px; text-align: center; }
  .sub { padding: 8px 10px 10px; }
  /* Denser palette: six or more icons per row. */
  .palette { grid-template-columns: repeat(auto-fill, minmax(50px, 1fr)); gap: 1px; }
  .palette button { padding: 2px 0 3px; font-size: 10.5px; letter-spacing: -.01em; min-width: 0; }
  .palette canvas { width: 30px !important; height: 30px !important; }
  /* Bottom sheet: ends above the toolbar so Rückgängig and the tools stay reachable. */
  .panel {
    left: 8px; right: 8px; top: auto !important; width: auto;
    bottom: calc(max(8px, env(safe-area-inset-bottom)) + 66px);
    max-height: calc(56% - 50px) !important;
  }
  .panelbody { padding: 14px 14px 16px; }
  .zoom, .compass, .hint { display: none; }
  .hint.important { display: block; white-space: normal; text-align: center; max-width: calc(100vw - 24px); width: max-content; bottom: calc(max(8px, env(safe-area-inset-bottom)) + 76px); }
  .scale { left: 10px; bottom: calc(max(8px, env(safe-area-inset-bottom)) + 74px); padding: 4px 18px 3px 7px; }
  .scaletitle { display: none; }
  body[data-mode="spieler"] .scale { bottom: calc(max(8px, env(safe-area-inset-bottom)) + 74px); }
  .toast { bottom: calc(max(8px, env(safe-area-inset-bottom)) + 76px); }
  /* The move hint and toasts sit where the scale card is. */
  body:has(.hint.important) .scale, body:has(#toast:not([hidden])) .scale { visibility: hidden; }
  .newgrid { grid-template-columns: 1fr; }
  .thumb { justify-self: center; }
  .exgrid { grid-template-columns: 1fr; justify-items: center; }
  .dlg { padding: 14px 16px; }
}

/* The gate (tor.js): the map's UI waits behind the fog until the Losung is given */
body[data-tor="zu"] > :not(#map):not(#tor) { opacity: 0 !important; pointer-events: none !important; }
body[data-tor="auf"] > :not(#map):not(#tor) { transition: opacity .8s ease; }
.tor { position: fixed; inset: 0; z-index: 40; cursor: default; }
/* Parchment until the map has loaded and its own fog is drawn over it */
.tor::before {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(ellipse at 50% 45%, #f6ead0 0%, #f1e3c3 45%, #d6bf96 100%);
  transition: opacity .4s ease;
}
.tor.karte::before { opacity: 0; }
.tor-ui {
  position: absolute; left: 0; right: 0; top: 50%; margin-inline: auto; width: min(360px, calc(100vw - 32px));
  display: grid; justify-items: center; gap: 6px; text-align: center; color: var(--ink);
  transition: top .45s cubic-bezier(.3, .7, .2, 1), opacity .35s ease;
}
.tor-ui.weg { opacity: 0; pointer-events: none; }
.tor-h { margin: 0; font: 400 clamp(24px, 3.4vw, 32px)/1.1 "IM Fell English SC", Georgia, serif; letter-spacing: .1em; text-wrap: balance; }
.tor-sub { margin: 0 0 10px; max-width: 34ch; font: italic 17px/1.35 "IM Fell English", Georgia, serif; color: var(--sepia); }
.tor-label { font: 14px/1 "IM Fell English SC", Georgia, serif; letter-spacing: .2em; color: var(--sepia); }
.tor-row { display: flex; gap: 10px; width: 100%; align-items: end; }
.tor-row input {
  flex: 1; min-width: 0; width: 0; padding: 6px 2px 5px;
  border: 0; border-radius: 0; border-bottom: 1px solid rgba(109,82,53,.55); background: transparent;
  color: var(--ink); font: 20px/1.2 "IM Fell English", Georgia, serif; letter-spacing: .12em; text-align: center;
}
.tor-row input:focus { outline: none; border-bottom-color: var(--ink); box-shadow: 0 1px 0 var(--ink); }
.tor-row .btn.primary { flex: none; padding: 7px 14px 8px; font-size: 16px; }
.tor-msg { margin: 0; min-height: 1.4em; font: italic 15.5px/1.4 "IM Fell English", Georgia, serif; color: var(--seal); }
@media (prefers-reduced-motion: reduce) { .tor-ui { transition: opacity .2s ease; } }
