@font-face {
  font-family: 'Cinzel';
  src: url('fonts/Cinzel.ttf') format('truetype');
  font-weight: 400 900;
  font-display: swap;
}

:root {
  --panel: rgba(9, 24, 40, 0.86);
  --panel-solid: #0c2238;
  --line: rgba(255, 255, 255, 0.11);
  --text: #eef3f6;
  --muted: #9fb3c2;
  --gold: #f2c14e;
  --gold-dark: #9c7316;
  --bone: #f4ead2;
  --warn: #ff6b6b;
  --good: #7bd389;
  --info: #5ec4e8;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #081a2c;
  color: var(--text);
  font: 15px/1.35 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.hidden { display: none !important; }

#map {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  cursor: crosshair;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

/* ------------------------------------------------------------ HUD */
#hud { position: fixed; inset: 0; pointer-events: none; }
#hud .card, #hud button, #hud input, #tipBanner { pointer-events: auto; }

#topbar {
  position: absolute;
  top: calc(8px + var(--safe-top));
  left: 8px;
  right: 8px;
  display: flex;
  gap: 8px;
  align-items: flex-start;
  justify-content: space-between;
}

.me { display: flex; gap: 14px; padding: 7px 12px; align-items: center; }
.crest { width: 30px; height: 30px; flex: none; }
.crest canvas { width: 30px; height: 30px; display: block; }
.stat { display: flex; flex-direction: column; line-height: 1.1; min-width: 46px; }
.stat .lbl { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.stat b { font-size: 18px; font-variant-numeric: tabular-nums; }
.stat small { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }

.tide { width: 270px; padding: 7px 12px; }
.tide-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.tide-row b { font-variant-numeric: tabular-nums; }
.tide-row.small { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.gauge {
  position: relative;
  height: 10px;
  margin: 5px 0;
  border-radius: 5px;
  background: rgba(233, 216, 166, 0.22);
  overflow: hidden;
}
.gauge-fill {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 0;
  background: linear-gradient(90deg, #22709e, #5ec4e8);
  transition: width 0.25s linear;
}
.gauge-mark { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--gold); }
.tide.spring { border-color: rgba(255, 107, 107, 0.55); }
.tide.spring .gauge-fill { background: linear-gradient(90deg, #22709e, #ff7b6b); }
.tide.spring #springNext { color: #ffb3a8; font-weight: 700; }

.right { display: flex; gap: 8px; align-items: flex-start; }
.board { padding: 6px 9px; min-width: 156px; font-size: 13px; cursor: pointer; }
.board .row { display: grid; grid-template-columns: 11px 1fr auto; gap: 7px; align-items: center; padding: 1px 0; white-space: nowrap; }
.board .row b { font-variant-numeric: tabular-nums; }
.board .row.me span, .board .row.me b { color: var(--gold); font-weight: 700; }
.board .row.dead { opacity: 0.4; }
.sw { display: inline-block; width: 11px; height: 11px; border-radius: 3px; box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35); vertical-align: -1px; }

.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: 11px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
}
.pause-ico { width: 12px; height: 14px; border-left: 4px solid var(--text); border-right: 4px solid var(--text); }
.home-ico {
  width: 16px; height: 14px;
  background: var(--text);
  clip-path: polygon(0 100%, 0 30%, 17% 30%, 17% 0, 33% 0, 33% 30%, 67% 30%, 67% 0, 83% 0, 83% 30%, 100% 30%, 100% 100%, 62% 100%, 62% 62%, 38% 62%, 38% 100%);
}
#btnSpeed { font-size: 15px; font-weight: 700; }

#sidebtns {
  position: absolute;
  right: 8px;
  bottom: calc(150px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#bottombar {
  position: absolute;
  bottom: calc(8px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: min(660px, calc(100% - 16px));
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.sendrow { display: flex; align-items: center; gap: 10px; padding: 6px 12px; }
.sendrow label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.sendrow input { flex: 1; min-width: 60px; accent-color: var(--gold); height: 22px; }
.sendrow b { min-width: 40px; text-align: right; font-variant-numeric: tabular-nums; }
#sendTroops { font-size: 12px; color: var(--muted); min-width: 64px; font-variant-numeric: tabular-nums; }

.tools { display: flex; gap: 3px; padding: 5px; }
.tool {
  flex: 1;
  min-width: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 5px 2px 4px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  font-size: 11px;
}
.tool canvas { width: 30px; height: 30px; display: block; }
.tool .name { font-weight: 600; white-space: nowrap; }
.tool .cost { font-size: 10px; color: var(--gold); font-variant-numeric: tabular-nums; min-height: 12px; }
.tool .key { position: absolute; top: 2px; left: 5px; font-size: 9px; color: var(--muted); }
.tool.active { background: rgba(242, 193, 78, 0.16); border-color: var(--gold); }
.tool.poor .cost { color: var(--warn); }
.tool.poor canvas { opacity: 0.45; }

#feed {
  position: absolute;
  left: 8px;
  top: calc(70px + var(--safe-top));
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: min(360px, calc(100% - 70px));
  pointer-events: none;
}
.msg {
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 4px solid var(--info);
  border-radius: 9px;
  padding: 6px 10px;
  font-size: 13px;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.3);
  animation: msg-in 0.25s ease-out;
  transition: opacity 0.45s, transform 0.45s;
}
.msg.warn { border-left-color: var(--warn); }
.msg.good { border-left-color: var(--good); }
.msg.fade { opacity: 0; transform: translateX(-12px); }
@keyframes msg-in { from { opacity: 0; transform: translateX(-12px); } }

#tipBanner {
  position: absolute;
  left: 50%;
  top: calc(78px + var(--safe-top));
  transform: translateX(-50%);
  width: max-content;
  max-width: min(520px, calc(100% - 24px));
  background: rgba(242, 193, 78, 0.96);
  color: #241a04;
  padding: 9px 14px;
  border-radius: 11px;
  font-weight: 600;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  animation: msg-in 0.3s ease-out;
}

#cursorTip {
  position: fixed;
  z-index: 5;
  pointer-events: none;
  background: rgba(8, 20, 34, 0.94);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 9px;
  font-size: 12px;
  line-height: 1.35;
  max-width: 250px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
}
#cursorTip .bad { color: #ffb3a8; }
#cursorTip .dim { color: var(--muted); }

#toast {
  position: fixed;
  z-index: 6;
  left: 50%;
  bottom: calc(150px + var(--safe-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  width: max-content;
  background: rgba(8, 20, 34, 0.95);
  border: 1px solid var(--line);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  pointer-events: none;
  text-align: center;
}

/* ------------------------------------------------------------ screens */
#screens { position: fixed; inset: 0; pointer-events: none; }
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(14px + var(--safe-top)) 14px calc(14px + var(--safe-bottom));
  overflow-y: auto;
  pointer-events: auto;
  background: radial-gradient(ellipse at 50% 45%, rgba(8, 26, 44, 0.25), rgba(4, 12, 22, 0.8));
  touch-action: pan-y;
}
#scrBusy { background: rgba(4, 12, 22, 0.6); }
.busy { font-weight: 600; font-size: 18px; }

.panel {
  margin: auto;
  width: min(420px, 100%);
  padding: 24px 22px;
  border-radius: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  text-align: center;
}
.panel.wide { width: min(760px, 100%); }
.title-panel { background: rgba(9, 24, 40, 0.72); }

.logo {
  font-family: Cinzel, Georgia, serif;
  font-weight: 800;
  font-size: clamp(38px, 9vw, 60px);
  line-height: 1.05;
  margin: 4px 0 0;
  letter-spacing: 0.03em;
  color: var(--bone);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.35), 0 0 34px rgba(94, 196, 232, 0.35);
}
.logo .amp { color: var(--gold); }
.tagline { margin: 6px 0 20px; color: var(--muted); font-style: italic; }
h2 {
  font-family: Cinzel, Georgia, serif;
  font-size: 27px;
  margin: 0 0 16px;
  color: var(--bone);
}

.menu { display: flex; flex-direction: column; gap: 10px; }
.btn {
  padding: 12px 18px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.07);
  font-weight: 700;
  font-size: 17px;
  cursor: pointer;
  transition: background 0.15s, transform 0.08s;
}
.btn:active { transform: translateY(1px); }
.btn small { font-size: 12px; color: var(--muted); font-weight: 600; margin-left: 6px; }
.btn.primary {
  background: linear-gradient(180deg, #f7d26a, #e0a92f);
  border-color: #f7d26a;
  color: #241a04;
  box-shadow: 0 4px 0 var(--gold-dark);
}
.btn.primary small { color: #5a4310; }
.btn.ghost { background: transparent; }
.soon { background: rgba(94, 196, 232, 0.2); color: #9be3ff !important; padding: 1px 7px; border-radius: 6px; }

.chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.07);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.chip[aria-pressed='false'] { color: var(--muted); text-decoration: line-through; }
.row2 { display: flex; gap: 8px; justify-content: center; }
.title-foot { display: flex; gap: 8px; justify-content: center; align-items: center; margin-top: 16px; flex-wrap: wrap; }
#progressLine { width: 100%; font-size: 12px; color: var(--muted); }
.links { margin-top: 12px; font-size: 12px; color: var(--muted); }
.links a { color: var(--muted); }

.diffs { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.diff {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 12px 8px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
  text-align: center;
}
.diff .pips { color: var(--gold); letter-spacing: 2px; font-size: 11px; }
.diff .name { font-family: Cinzel, Georgia, serif; font-weight: 700; font-size: 17px; }
.diff .desc { font-size: 12px; color: var(--muted); line-height: 1.3; }
.diff .badge { font-size: 11px; color: var(--gold); min-height: 14px; }
.diff.sel { border-color: var(--gold); background: rgba(242, 193, 78, 0.13); box-shadow: inset 0 0 0 1px var(--gold); }
.diff.locked { opacity: 0.45; cursor: not-allowed; }
.diff.locked .badge { color: var(--muted); }

.setup-row { display: flex; gap: 16px; margin: 16px 0 6px; align-items: center; justify-content: space-between; flex-wrap: wrap; text-align: left; }
.setup-opts { display: flex; flex-direction: column; gap: 12px; flex: 1; min-width: 220px; }
.opt .lbl { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-bottom: 5px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { padding: 7px 13px; border: 0; border-right: 1px solid var(--line); background: transparent; font-weight: 700; cursor: pointer; }
.seg button:last-child { border-right: 0; }
.seg button.sel { background: var(--gold); color: #241a04; }
.mapline { display: flex; gap: 10px; align-items: center; }
#mapName { font-weight: 700; }
#mapPreview {
  width: 256px;
  height: 160px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #0c3050;
  image-rendering: pixelated;
}
.actions { display: flex; gap: 10px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
.actions .btn { min-width: 140px; }

.how .steps { text-align: left; padding-left: 22px; margin: 0; line-height: 1.45; }
.how .steps li { margin: 7px 0; }
.how .steps b { color: var(--gold); }
.controls { text-align: left; color: var(--muted); font-size: 13px; margin: 14px 0 0; }

.end-sub { color: var(--muted); font-size: 16px; margin: -6px 0 12px; }
.end-bar { display: flex; height: 14px; border-radius: 7px; overflow: hidden; margin: 0 0 14px; background: #1d4f73; }
.end-bar i { display: block; height: 100%; }
.end-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.end-stats div { background: rgba(255, 255, 255, 0.05); border-radius: 10px; padding: 8px 4px; display: flex; flex-direction: column; }
.end-stats b { font-size: 18px; font-variant-numeric: tabular-nums; }
.end-stats span { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.unlock { color: var(--gold); font-weight: 700; margin: 0 0 12px; }

.field { display: block; text-align: left; margin-bottom: 12px; }
.field .lbl, #mpLobby .lbl, #mpHostOpts .lbl { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-bottom: 5px; }
#mpName, #mpCode {
  width: 100%;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  font: 600 16px system-ui, sans-serif;
  -webkit-user-select: text;
  user-select: text;
}
.joinrow { display: flex; gap: 8px; margin-top: 12px; }
.joinrow #mpCode { text-transform: uppercase; letter-spacing: 0.25em; text-align: center; }
.code { font-family: Cinzel, Georgia, serif; font-size: 40px; font-weight: 800; letter-spacing: 0.18em; color: var(--gold); margin: 2px 0 4px; -webkit-user-select: text; user-select: text; }
.muted { color: var(--muted); font-size: 14px; }
.mp-players { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; text-align: left; }
.mp-players div { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 9px; background: rgba(255, 255, 255, 0.05); }
.mp-players small { margin-left: auto; color: var(--gold); font-weight: 700; }
#mpHostOpts { display: flex; flex-direction: column; gap: 10px; align-items: center; }
#mpHostOpts .opt { text-align: center; }

@media (hover: hover) {
  .btn:hover { background: rgba(255, 255, 255, 0.13); }
  .btn.primary:hover { background: linear-gradient(180deg, #fadb7e, #e8b33c); }
  .tool:hover { background: rgba(255, 255, 255, 0.07); }
  .tool.active:hover { background: rgba(242, 193, 78, 0.2); }
  .diff:not(.locked):hover { border-color: rgba(242, 193, 78, 0.6); }
  .chip:hover, .icon-btn:hover { background: rgba(255, 255, 255, 0.14); }
}
@media (hover: none) {
  .tool .key { display: none; }
}

/* ------------------------------------------------------------ small screens */
@media (max-width: 760px) {
  #topbar { flex-wrap: wrap; }
  .me { gap: 10px; padding: 6px 9px; }
  .stat { min-width: 40px; }
  .stat b { font-size: 15px; }
  .tide { order: 3; width: 100%; padding: 6px 10px; }
  .board { min-width: 0; font-size: 12px; padding: 5px 7px; }
  .board.collapsed .row:not(.me):not(.top) { display: none; }
  .icon-btn { width: 38px; height: 38px; font-size: 20px; }
  #feed { top: calc(128px + var(--safe-top)); }
  #tipBanner { top: calc(128px + var(--safe-top)); font-size: 13px; }
  .tool .name { display: none; }
  .tool canvas { width: 28px; height: 28px; }
  .diffs { grid-template-columns: 1fr; }
  .diff { display: grid; grid-template-columns: 62px 1fr; text-align: left; column-gap: 10px; row-gap: 1px; padding: 9px 12px; align-items: center; }
  .diff .pips { grid-row: 1 / span 3; font-size: 10px; letter-spacing: 1px; }
  .diff .name, .diff .desc, .diff .badge { grid-column: 2; }
  .diff .name { font-size: 15px; }
  .diff .badge { min-height: 0; }
  #mapPreview { width: 100%; height: auto; aspect-ratio: 8 / 5; }
  .end-stats b { font-size: 16px; }
}
@media (max-width: 400px) {
  .me { gap: 7px; }
  .crest { display: none; }
  .sendrow { gap: 7px; padding: 5px 9px; }
  #sendTroops { display: none; }
}
@media (max-height: 520px) and (orientation: landscape) {
  #bottombar { width: min(560px, calc(100% - 120px)); }
  .tool .name { display: none; }
  .tool canvas { width: 24px; height: 24px; }
  .sendrow { padding: 3px 10px; }
  #sidebtns { bottom: auto; top: calc(60px + var(--safe-top)); flex-direction: row; right: auto; left: 8px; }
  #feed { top: calc(110px + var(--safe-top)); }
  .panel { padding: 16px; }
  .logo { font-size: 36px; }
  .menu { gap: 7px; }
  .btn { padding: 9px 14px; font-size: 15px; }
}
@media (prefers-reduced-motion: reduce) {
  .msg, #tipBanner { animation: none; }
  .gauge-fill { transition: none; }
}
