:root {
  --ink: #20211f;
  --paper: #f1efe6;
  --panel: #e4e0d2;
  --grid: rgba(32, 33, 31, 0.13);
  --acid: #d9f43b;
  --coral: #ff6b4a;
  --teal: #0f8b8d;
  --white: #fffef8;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  background:
    linear-gradient(rgba(32, 33, 31, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(32, 33, 31, 0.035) 1px, transparent 1px),
    var(--paper);
  background-size: 24px 24px;
  font-family: "Bahnschrift", "DIN Alternate", sans-serif;
}

button { font: inherit; }

.game-shell {
  width: min(1180px, calc(100% - 32px));
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding: 16px 0 10px;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  margin-bottom: 10px;
  border-bottom: 3px solid var(--ink);
  padding-bottom: 12px;
}

.eyebrow {
  margin: 0 0 3px;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.16em;
}

h1, h2, p { margin-top: 0; }

h1 {
  margin-bottom: 0;
  font-family: Georgia, serif;
  font-size: clamp(2rem, 5vw, 4.4rem);
  line-height: 0.9;
  font-weight: 400;
}

h2 {
  margin-bottom: 0;
  font-family: Georgia, serif;
  font-size: 1.35rem;
  font-weight: 400;
}

.stats { display: flex; gap: 28px; }
.stats div { display: grid; gap: 2px; text-align: right; }
.stats span { font-size: 0.7rem; font-weight: 700; text-transform: uppercase; }
.stats strong { font-family: Georgia, serif; font-size: 1.65rem; font-weight: 400; }

.build-strip {
  display: flex;
  justify-content: flex-end;
  gap: 18px;
  margin: -9px 0 10px;
  font-size: 0.68rem;
  text-transform: uppercase;
}
.build-strip strong { color: var(--teal); }

.play-area {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 310px;
  gap: 22px;
  align-items: stretch;
  min-height: 0;
}

.board-wrap {
  position: relative;
  aspect-ratio: 1;
  width: min(100%, calc(100dvh - 180px));
  max-height: 100%;
  min-width: 0;
  justify-self: end;
  align-self: start;
  border: 3px solid var(--ink);
  background: var(--white);
  box-shadow: 9px 9px 0 var(--ink);
}

canvas { display: block; width: 100%; height: 100%; }

.program-panel {
  height: 100%;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: thin;
  border-top: 3px solid var(--ink);
  border-bottom: 3px solid var(--ink);
  padding: 16px 0;
}

.panel-heading { display: flex; justify-content: space-between; align-items: start; }
.icon-button {
  width: 38px;
  height: 38px;
  border: 2px solid var(--ink);
  background: transparent;
  cursor: pointer;
  font-size: 1.35rem;
}

.route-steps {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin-top: 12px;
  border: 1px solid var(--ink);
  background: var(--white);
  font-size: 0.62rem;
  text-transform: uppercase;
}
.route-steps > * { padding: 6px; border-right: 1px solid var(--ink); }
.route-steps > *:last-child { border-right: 0; }
.route-steps strong { background: var(--acid); }

.magnitude-picker { margin: 20px 0 10px; }
.magnitude-picker > span { display: block; margin-bottom: 6px; font-size: 0.68rem; font-weight: 800; text-transform: uppercase; }
.magnitude-picker > div { display: grid; grid-template-columns: repeat(4, 1fr); }
.magnitude-picker button { height: 38px; border: 2px solid var(--ink); border-right: 0; background: var(--white); cursor: pointer; }
.magnitude-picker button:last-child { border-right: 2px solid var(--ink); }
.magnitude-picker button.active { position: relative; z-index: 1; background: var(--acid); box-shadow: inset 0 0 0 2px var(--ink); font-weight: 900; }
.magnitude-picker button:disabled { background: var(--panel); color: rgba(32, 33, 31, 0.45); cursor: not-allowed; }

.segments { display: grid; gap: 8px; margin: 10px 0 22px; }
.segment {
  display: grid;
  grid-template-columns: 42px 1fr 48px;
  align-items: center;
  width: 100%;
  height: 54px;
  padding: 0 12px;
  border: 2px solid var(--ink);
  background: var(--white);
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}
.segment:disabled { cursor: default; }
.segment.assigned { background: var(--panel); }
.segment-number { font-family: Georgia, serif; font-size: 1.7rem; }
.segment-state { font-size: 0.78rem; font-weight: 800; text-transform: uppercase; }
.segment-arrow { font-size: 1.55rem; text-align: right; }

.direction-pad {
  display: grid;
  grid-template-columns: repeat(3, 54px);
  grid-template-rows: repeat(3, 48px);
  justify-content: center;
  margin: 12px 0 20px;
}
.direction-pad button {
  border: 2px solid var(--ink);
  background: var(--white);
  cursor: pointer;
  font-size: 1.25rem;
}
.direction-pad button:hover, .icon-button:hover { background: var(--acid); }
.direction-pad [data-direction="up"] { grid-column: 2; }
.direction-pad [data-direction="left"] { grid-column: 1; grid-row: 2; }
.direction-pad .skip-button { grid-column: 2; grid-row: 2; font-size: 0.62rem; font-weight: 800; }
.direction-pad [data-direction="right"] { grid-column: 3; grid-row: 2; }
.direction-pad [data-direction="down"] { grid-column: 2; grid-row: 3; }

.execute-button {
  display: flex;
  justify-content: space-between;
  width: 100%;
  min-height: 58px;
  padding: 17px 18px;
  border: 3px solid var(--ink);
  background: var(--coral);
  color: var(--ink);
  font-weight: 900;
  cursor: pointer;
}
.execute-button:disabled { background: var(--panel); cursor: not-allowed; opacity: 0.65; }
.execute-button.ready { box-shadow: 4px 4px 0 var(--ink); transform: translate(-2px, -2px); }
.status { min-height: 38px; margin: 10px 0 20px; font-size: 0.78rem; line-height: 1.4; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; border-top: 1px solid var(--ink); padding-top: 14px; font-size: 0.7rem; }
.legend span { display: flex; align-items: center; gap: 6px; }
.gem { width: 11px; height: 11px; background: var(--teal); transform: rotate(45deg); }
.gem.urgent { background: var(--coral); }
.tax-cell { width: 12px; height: 12px; border: 1px solid var(--ink); background: rgba(255, 107, 74, 0.1); }
.tax-cell.high { background: rgba(255, 107, 74, 0.33); }

footer {
  display: flex;
  flex-wrap: wrap;
  gap: 18px;
  align-items: center;
  min-height: 34px;
  margin-top: 6px;
  border-top: 1px solid var(--ink);
  padding-top: 6px;
  background: var(--paper);
  font-size: 0.72rem;
}
kbd { display: inline-block; min-width: 22px; margin-right: 2px; border: 1px solid var(--ink); padding: 3px 5px; background: var(--white); text-align: center; }
.keyboard-status { display: flex; align-items: center; min-width: 120px; font-size: 0.62rem; text-transform: uppercase; }
.keyboard-status b { font-weight: 900; }
.keyboard-status.active b { background: var(--acid); }
footer.shortcuts-active { box-shadow: inset 0 3px 0 var(--acid); }
footer.shortcuts-active > span:not(.keyboard-status) { background: rgba(217, 244, 59, 0.42); }
footer.shortcuts-active kbd { background: var(--acid); font-weight: 900; }

.game-over {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  overflow-y: auto;
  padding: 22px;
  background: rgba(32, 33, 31, 0.94);
  color: var(--white);
}
.game-over[hidden] { display: none; }
.ledger { width: min(620px, 100%); }
.ledger h2 { margin-bottom: 16px; font-size: 2rem; }
.ledger dl { margin: 0 0 18px; border-top: 1px solid var(--white); }
.ledger dl div { display: flex; justify-content: space-between; gap: 20px; padding: 7px 0; border-bottom: 1px solid rgba(255, 254, 248, 0.3); }
.ledger dt, .ledger dd { margin: 0; }
.ledger dd { font-variant-numeric: tabular-nums; }
.ledger .ledger-total { padding-top: 11px; color: var(--acid); font-size: 1.2rem; font-weight: 900; }
.capital-history { display: grid; gap: 3px; margin-bottom: 18px; font-size: 0.72rem; }
.capital-history span { opacity: 0.7; text-transform: uppercase; }
.capital-history strong { overflow-wrap: anywhere; color: var(--acid); font-variant-numeric: tabular-nums; }
.choice-label { margin-bottom: 8px; font-size: 0.68rem; font-weight: 900; letter-spacing: 0.14em; }
.upgrade-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.upgrade-grid button, .restart-button {
  border: 2px solid var(--white);
  padding: 12px;
  background: transparent;
  color: var(--white);
  cursor: pointer;
  text-align: left;
}
.upgrade-grid button:hover { border-color: var(--acid); background: var(--acid); color: var(--ink); }
.upgrade-grid strong, .upgrade-grid span { display: block; }
.upgrade-grid strong { margin-bottom: 6px; }
.upgrade-grid span { font-size: 0.68rem; line-height: 1.3; }
.restart-button { margin-top: 14px; background: var(--acid); color: var(--ink); font-weight: 900; }
.restart-button[hidden] { display: none; }

@media (min-width: 821px) {
  .program-panel { padding: 10px 0; }
  .magnitude-picker { margin: 12px 0 8px; }
  .segments { gap: 5px; margin: 6px 0 12px; }
  .segment { height: 46px; }
  .direction-pad {
    grid-template-rows: repeat(3, 40px);
    margin: 6px 0 10px;
  }
  .execute-button { min-height: 50px; padding: 13px 18px; }
  .status { min-height: 32px; margin: 6px 0 10px; }
  .legend { padding-top: 10px; }
}

@media (min-width: 821px) and (max-height: 800px) {
  .game-over { padding: 16px; }
  .ledger h2 { margin-bottom: 8px; }
  .ledger dl { margin-bottom: 10px; }
  .ledger dl div { padding: 5px 0; }
  .capital-history { margin-bottom: 10px; }
  .program-panel { padding: 2px 0; }
  .segments { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .segment {
    grid-template-columns: 26px minmax(0, 1fr) 24px;
    height: 42px;
    padding: 0 6px;
  }
  .segment-number { font-size: 1.3rem; }
  .segment-state { overflow: hidden; font-size: 0.62rem; text-overflow: ellipsis; white-space: nowrap; }
  .segment-arrow { font-size: 1.15rem; }
  .direction-pad { grid-template-rows: repeat(3, 36px); margin: 4px 0 6px; }
  .legend { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 6px; font-size: 0.62rem; }
}

@media (max-width: 820px) {
  .game-shell {
    width: min(100% - 20px, 620px);
    height: auto;
    min-height: 100dvh;
    padding: 14px 0 72px;
    display: block;
    overflow: visible;
  }
  .topbar { align-items: center; }
  .stats { gap: 13px; }
  .stats strong { font-size: 1.2rem; }
  .build-strip { justify-content: flex-start; }
  .play-area { grid-template-columns: 1fr; }
  .board-wrap { width: 100%; max-height: none; justify-self: stretch; box-shadow: 5px 5px 0 var(--ink); }
  .program-panel { height: auto; min-height: 0; overflow: visible; }
  .segments { grid-template-columns: repeat(2, 1fr); }
  .upgrade-grid { grid-template-columns: 1fr; }
  footer {
    position: fixed;
    z-index: 20;
    right: 0;
    bottom: 0;
    left: 0;
    min-height: 42px;
    margin: 0;
    padding: 7px 10px;
    justify-content: center;
    gap: 6px;
    box-shadow: 0 -2px 0 var(--ink);
    font-size: 0.56rem;
  }
  .keyboard-status { min-width: auto; }
  kbd { min-width: 19px; padding: 2px 3px; }
}

@media (max-width: 440px) {
  .game-shell { width: calc(100% - 14px); }
  .topbar { align-items: end; }
  h1 { font-size: 2.4rem; }
  .stats { gap: 8px; }
  .stats div:first-child { min-width: 44px; }
  .stats span { font-size: 0.57rem; }
  .stats strong { font-size: 1rem; }
  .segment { grid-template-columns: 32px 1fr 28px; padding: 0 8px; }
}