:root {
  --parchment: #f3e6c8; --parchment-hi: #faf1dc; --parchment-lo: #e6d3a8;
  --ink: #1f2a2a; --sepia: #5a4630; --rule: #8a6f49;
  --teal: #145059; --teal-deep: #0f434c; --teal-line: rgba(243,230,200,.2);
  --oxblood: #7c1a24; --oxblood-deep: #631219;
  --land: #dcc698; --land-edge: #6d5533; --target: #fbf3df;
  --serif: "Iowan Old Style", "New York", ui-serif, Palatino, Georgia, serif;
  --gutter: 16px;
}
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh; display: flex; flex-direction: column;
  font: 17px/1.5 var(--serif); color: var(--ink);
  background-color: var(--parchment);
  background-image:
    radial-gradient(120% 70% at 50% 0, rgba(250,241,220,.9), transparent 70%),
    radial-gradient(90% 60% at 100% 100%, rgba(160,122,70,.2), transparent 70%),
    radial-gradient(70% 50% at 0 80%, rgba(160,122,70,.14), transparent 70%);
  font-variant-numeric: oldstyle-nums;
}
h1, h2, p, dl, dd, ol, figure, pre { margin: 0; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--oxblood); outline-offset: 2px; }

#site-header {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 8px var(--gutter) 9px;
  border-bottom: 1px solid var(--ink); box-shadow: 0 3px 0 -2px var(--parchment), 0 3px 0 -1px var(--rule);
}
.mark { width: 36px; height: 36px; display: block; }
#site-title { font-size: 1.45rem; line-height: 1.1; font-weight: 600; letter-spacing: -.005em; color: var(--teal-deep); }

main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#load-status { padding: 48px var(--gutter); text-align: center; font-style: italic; color: var(--sepia); }

/* Controls */
.primary, .secondary, .teal, .quiet {
  display: block; width: 100%; min-height: 52px; padding: 10px 18px;
  font-size: 1.1rem; font-weight: 600; cursor: pointer; border-radius: 2px;
}
.primary { background: var(--oxblood); color: var(--parchment-hi); border: 1px solid var(--oxblood-deep); box-shadow: inset 0 0 0 2px var(--oxblood), inset 0 0 0 3px rgba(243,230,200,.55); }
.teal { background: var(--teal); color: var(--parchment-hi); border: 1px solid var(--teal-deep); box-shadow: inset 0 0 0 2px var(--teal), inset 0 0 0 3px rgba(243,230,200,.55); }
.secondary { background: transparent; color: var(--teal-deep); border: 1px solid var(--teal-deep); }
.quiet { background: none; border: 0; color: var(--sepia); text-decoration: underline; text-underline-offset: 3px; font-weight: 400; min-height: 44px; font-size: 1rem; }
.primary:disabled { background: var(--parchment-lo); color: #5a4630; border-color: var(--rule); box-shadow: none; cursor: not-allowed; }
@media (hover: hover) {
  .primary:enabled:hover { background: var(--oxblood-deep); box-shadow: inset 0 0 0 2px var(--oxblood-deep), inset 0 0 0 3px rgba(243,230,200,.55); }
  .teal:hover { background: var(--teal-deep); box-shadow: inset 0 0 0 2px var(--teal-deep), inset 0 0 0 3px rgba(243,230,200,.55); }
  .secondary:hover { background: rgba(20,80,89,.09); }
}

/* Home */
#home { flex: 1; display: flex; flex-direction: column; }
.portolan { height: 200px; background: var(--teal); border-bottom: 1px solid var(--ink); overflow: hidden; }
.portolan svg { display: block; width: 100%; height: 100%; }
.rhumb path { stroke: rgba(243,230,200,.2); stroke-width: 1; fill: none; }
.grat circle { fill: none; stroke: rgba(243,230,200,.14); }
.voyage { fill: none; stroke: #d98b7c; stroke-width: 2; stroke-dasharray: 9 6; }
.rose circle { fill: none; stroke: var(--parchment); stroke-width: 1; }
.rose .dark { fill: var(--parchment); }
.rose .light { fill: var(--teal-deep); stroke: var(--parchment); stroke-width: 1; }
.rose .pts-b path { fill: #b9a57c; }
.rose .hub { fill: var(--oxblood); stroke: var(--parchment); stroke-width: 1.5; }
.home-copy { padding: 22px var(--gutter) 28px; max-width: 34rem; width: 100%; margin: 0 auto; }
#intro { font-size: 1.32rem; line-height: 1.36; margin-bottom: 20px; }
.home-actions { display: grid; gap: 10px; }
#home h2 {
  display: flex; align-items: center; gap: 12px; margin: 30px 0 4px;
  font-size: .82rem; font-weight: 600; letter-spacing: .14em; text-transform: uppercase; color: var(--sepia);
}
#home h2::after { content: ""; flex: 1; border-top: 1px solid var(--rule); }
#stats > div { display: flex; justify-content: space-between; align-items: baseline; padding: 7px 0; border-bottom: 1px dotted var(--rule); }
#stats dt { color: var(--sepia); }
#stats dd { font-size: 1.2rem; font-weight: 600; color: var(--teal-deep); font-variant-numeric: lining-nums tabular-nums; }

/* Game */
#game { flex: 1; display: flex; flex-direction: column; min-height: calc(100dvh - 58px); }
.log { padding: 9px var(--gutter) 10px; }
.tally { display: flex; justify-content: space-between; gap: 12px; font-size: .8rem; letter-spacing: .08em; text-transform: uppercase; color: var(--sepia); font-variant-numeric: lining-nums; }
#running-score { color: var(--oxblood); font-weight: 600; white-space: nowrap; }
#clue-type { margin-top: 4px; font-style: italic; color: var(--sepia); line-height: 1.25; }
#clue { font-size: 1.42rem; line-height: 1.2; font-weight: 600; color: var(--ink); }

/* The neatline: alternating ink and parchment, as on a printed sheet. */
.chart {
  position: relative; flex: 1; display: flex; min-height: 280px; padding: 6px;
  --tick: repeating-linear-gradient(90deg, var(--ink) 0 34px, var(--parchment-hi) 34px 68px);
  --tickv: repeating-linear-gradient(0deg, var(--ink) 0 34px, var(--parchment-hi) 34px 68px);
  background:
    var(--tick) top / 100% 5px no-repeat, var(--tick) bottom / 100% 5px no-repeat,
    var(--tickv) left / 5px 100% no-repeat, var(--tickv) right / 5px 100% no-repeat,
    var(--ink);
}
.chart::after { content: ""; position: absolute; inset: 6px; pointer-events: none; box-shadow: inset 0 0 44px rgba(6,32,38,.42); }
#game-map-slot, #results-map-slot { flex: 1; min-width: 0; position: relative; }
#map {
  position: absolute; inset: 0; display: block; width: 100%; height: 100%;
  touch-action: none; user-select: none; -webkit-user-select: none; cursor: crosshair;
  background: repeating-linear-gradient(0deg, rgba(243,230,200,.045) 0 1px, transparent 1px 5px), var(--teal);
}
.map-graticule { stroke: var(--teal-line); stroke-width: .75; }
.map-land { fill: var(--land); stroke: var(--land-edge); stroke-width: .6; }
.map-target { fill: var(--target); stroke: var(--oxblood); stroke-width: 1.4; }
.map-glyph { stroke: var(--parchment-hi); stroke-width: 1.5; paint-order: stroke; stroke-linejoin: round; }
.map-guess .map-glyph { fill: var(--oxblood); transform-origin: 0 0; animation: drop .22s cubic-bezier(.2,1.4,.5,1); }
.map-answer .map-glyph { fill: var(--teal-deep); }
.map-marker text {
  font: 600 15px var(--serif); fill: var(--ink); stroke: var(--parchment-hi); stroke-width: 3.5px;
  paint-order: stroke; stroke-linejoin: round; pointer-events: none;
}
.map-line { stroke: var(--oxblood); stroke-width: 2; stroke-dasharray: 7 5; }
@keyframes drop { from { transform: translateY(-14px) scale(.8); opacity: 0; } }

#map-controls { position: absolute; top: 16px; right: 16px; z-index: 2; display: flex; flex-direction: column; background: var(--parchment-hi); border: 1px solid var(--ink); border-radius: 2px; box-shadow: 0 1px 4px rgba(6,32,38,.4); }
#map-controls button { width: 46px; height: 46px; border: 0; background: none; font-size: 1.55rem; line-height: 1; color: var(--ink); cursor: pointer; font-variant-numeric: lining-nums; }
#map-controls button + button { border-top: 1px solid var(--rule); }
@media (hover: hover) { #map-controls button:hover { background: var(--parchment-lo); } }

.helm { padding: 8px var(--gutter) calc(12px + env(safe-area-inset-bottom)); display: grid; gap: 7px; }
#feedback { min-height: 1.5em; text-align: center; font-size: 1.05rem; font-weight: 600; color: var(--oxblood); line-height: 1.3; }
#feedback:empty::before { content: "Tap the map to place your pin"; font-weight: 400; font-style: italic; color: var(--sepia); }
#game:has(#btn-confirm:enabled) #feedback:empty::before { content: "Tap again to move your pin"; }

/* Results */
#results { flex: 1; display: flex; flex-direction: column; }
.res-head { padding: 18px var(--gutter) 14px; text-align: center; }
#results-title { font-size: 1rem; font-weight: 400; font-style: italic; color: var(--sepia); }
#results-score { font-size: 2.7rem; line-height: 1.1; font-weight: 600; color: var(--teal-deep); font-variant-numeric: lining-nums; }
#results .chart { flex: none; height: clamp(250px, 44vh, 420px); }
.res-body { padding: 14px var(--gutter) 24px; max-width: 34rem; width: 100%; margin: 0 auto; }
#results-list { list-style: none; padding: 0; counter-reset: leg; margin-bottom: 18px; }
#results-list li {
  counter-increment: leg; display: grid; grid-template-columns: 1.7rem 1fr auto; column-gap: 8px;
  padding: 8px 0; border-bottom: 1px dotted var(--rule);
}
#results-list li::before { content: counter(leg); grid-row: span 2; font-weight: 600; color: var(--oxblood); font-variant-numeric: lining-nums; }
.r-place { font-weight: 600; grid-column: 2; }
.r-pts { grid-column: 3; grid-row: 1; font-weight: 600; color: var(--teal-deep); text-align: right; font-variant-numeric: lining-nums tabular-nums; }
.r-type { grid-column: 3; grid-row: 2; font-size: .9rem; font-style: italic; color: var(--sepia); text-align: right; }
.r-km { grid-column: 2; grid-row: 2; font-size: .9rem; color: var(--sepia); font-variant-numeric: lining-nums; }
#share-status { min-height: 1.5em; margin-top: 8px; text-align: center; font-style: italic; color: var(--sepia); }
#share-text { white-space: pre-wrap; font: 1rem/1.4 ui-monospace, "American Typewriter", Courier, monospace; padding-top: 8px; }
#share-text:empty { display: none; }

#site-footer { padding: 10px var(--gutter) 16px; border-top: 1px solid var(--rule); font-size: .82rem; line-height: 1.4; color: var(--sepia); text-align: center; }

@media (min-width: 900px) {
  :root { --gutter: 28px; }
  #site-title { font-size: 1.7rem; }
  .mark { width: 44px; height: 44px; }
  #home { flex-direction: row; }
  .portolan { flex: 1 1 58%; height: auto; border-bottom: 0; border-right: 1px solid var(--ink); }
  .home-copy { flex: 0 0 min(460px, 42%); margin: 0; padding: 40px 40px; align-self: center; }
  #intro { font-size: 1.5rem; }
  .home-actions { grid-template-columns: 1fr 1fr; }
  #game { display: grid; grid-template-columns: 330px 1fr; grid-template-rows: 1fr auto; min-height: calc(100dvh - 66px - 58px); }
  .log { grid-column: 1; grid-row: 1; padding: 34px 26px 16px; }
  .chart { grid-column: 2; grid-row: 1 / span 2; }
  .helm { grid-column: 1; grid-row: 2; padding: 16px 26px 28px; }
  #clue { font-size: 2rem; margin-top: 8px; }
  #clue-type { margin-top: 26px; font-size: 1.1rem; }
  #feedback { text-align: left; font-size: 1.2rem; }
  #results { display: grid; grid-template-columns: 1fr 400px; grid-template-rows: auto 1fr; }
  #results .chart { grid-column: 1; grid-row: 1 / span 2; height: auto; min-height: 520px; }
  .res-head { grid-column: 2; padding-top: 30px; }
  .res-body { grid-column: 2; padding: 0 28px 24px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
