:root {
  color-scheme: dark;
  --ink: #eef1df;
  --muted: #aeb8a8;
  --glass: rgba(27, 37, 34, .82);
  --line: rgba(236, 239, 218, .16);
  --rust: #d08a59;
  --sand: #d7c49d;
  --safe-bottom: max(14px, env(safe-area-inset-bottom));
  font-family: "Avenir Next", "Trebuchet MS", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body, #game {
  width: 100%; height: 100%; margin: 0; overflow: hidden;
  background: #263431; color: var(--ink); touch-action: none;
  user-select: none; -webkit-user-select: none; overscroll-behavior: none;
}

button { font: inherit; color: inherit; }
button:focus-visible { outline: 3px solid #ead49e; outline-offset: 3px; }

#world {
  position: absolute; inset: 0; width: 100%; height: 100%;
  image-rendering: pixelated; image-rendering: crisp-edges;
  cursor: grab; touch-action: none;
}
#world:active { cursor: grabbing; }

.topbar {
  position: absolute; z-index: 5; top: 0; left: 0; right: 0;
  min-height: 64px; padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left));
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  pointer-events: none;
  background: linear-gradient(rgba(18,27,25,.48), transparent);
}
.topbar button { pointer-events: auto; }
.brand { display: flex; align-items: center; gap: 9px; font-size: 13px; font-weight: 900; letter-spacing: .18em; text-shadow: 0 2px 8px #18221e; }
.brand-mark { width: 19px; height: 19px; border: 2px solid var(--ink); border-radius: 50%; position: relative; }
.brand-mark::after { content: ""; position: absolute; left: 4px; top: 4px; width: 7px; height: 7px; border-left: 2px solid var(--ink); border-bottom: 2px solid var(--ink); transform: rotate(-18deg); }
.seed-chip { border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; background: var(--glass); backdrop-filter: blur(12px); display: flex; align-items: baseline; gap: 7px; box-shadow: 0 3px 15px rgba(0,0,0,.16); }
.seed-chip span { font-size: 8px; letter-spacing: .16em; color: var(--muted); }
.seed-chip b { font: 700 11px ui-monospace, monospace; letter-spacing: .06em; }
.top-actions { justify-self: end; display: flex; align-items: center; gap: 7px; }
.round-button, .map-tools button { width: 38px; height: 38px; border: 1px solid var(--line); border-radius: 50%; background: var(--glass); backdrop-filter: blur(12px); font-weight: 900; font-size: 16px; }
.save-status { font-size: 9px; letter-spacing: .15em; color: rgba(238,241,223,.75); text-shadow: 0 1px 4px #17211e; transition: opacity .3s; }

.target-readout { position: absolute; z-index: 4; top: 72px; left: 50%; transform: translateX(-50%); display: flex; gap: 7px; align-items: center; padding: 5px 9px; border-radius: 4px; background: rgba(20,28,26,.62); color: #e6ead8; font: 700 9px ui-monospace, monospace; letter-spacing: .09em; pointer-events: none; }
.target-icon { width: 7px; height: 7px; background: var(--sand); transform: rotate(45deg); }
.target-readout.blocked .target-icon { background: var(--rust); }
.target-readout.blocked { color: #ffd0ae; box-shadow: inset 0 0 0 1px rgba(235,132,91,.45); }

.cargo-panel { position: absolute; z-index: 7; bottom: 18px; left: 50%; transform: translateX(-50%); min-width: 250px; display: grid; justify-items: center; gap: 7px; pointer-events: none; }
.cargo-panel button, .cargo-readout, .recipe-tray { pointer-events: auto; }
.cargo-readout { min-width: 250px; min-height: 46px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 12px; background: var(--glass); box-shadow: 0 6px 20px rgba(0,0,0,.23); backdrop-filter: blur(10px); }
.mode-buttons { display: flex; justify-content: center; gap: 8px; }
.cargo-label { display: flex; justify-content: space-between; font-size: 8px; line-height: 12px; letter-spacing: .15em; color: var(--muted); }
.cargo-label b { color: var(--ink); }
.cargo-slots { min-height: 17px; display: flex; gap: 4px; align-items: center; margin-top: 3px; }
.cargo-slot { width: 17px; height: 17px; border: 1px solid rgba(255,255,255,.17); border-radius: 3px; position: relative; box-shadow: inset 0 -3px rgba(0,0,0,.16); }
.cargo-slot.top { z-index: 1; border-color: #fff4c6; outline: 2px solid #e8bd64; outline-offset: 2px; box-shadow: inset 0 -3px rgba(0,0,0,.24), 0 0 8px rgba(232,189,100,.72); }
.cargo-slot.top::after { content: "TOP"; position: absolute; left: 50%; top: -17px; transform: translateX(-50%); font: 800 7px ui-monospace, monospace; color: #fff4c6; text-shadow: 0 1px 2px #000; }
.cargo-slot.empty { background: rgba(255,255,255,.035); box-shadow: none; }
.cargo-slot.soil, .material.soil { background: #79563b; }
.cargo-slot.sand, .material.sand { background: #e0bd70; }
.cargo-slot.stone, .material.stone { background: #6f7e8a; }
.cargo-slot.wood, .material.wood { background: #3f7048; }
.cargo-slot.animal-0 { background: #e3c785; border-radius: 50%; }
.cargo-slot.animal-1 { background: #b87558; border-radius: 50%; }
.cargo-slot.animal-2 { background: #657d76; border-radius: 50%; }
.material.animal { background: #e3c785; border-radius: 50%; }
.recipe-toggle { min-width: 112px; min-height: 44px; border: 1px solid rgba(230,184,121,.28); border-radius: 13px; padding: 7px 12px; background: rgba(31,42,38,.92); display: flex; justify-content: center; gap: 9px; align-items: center; box-shadow: 0 4px 15px rgba(0,0,0,.2); }
.recipe-toggle span { font-size: 9px; font-weight: 900; letter-spacing: .12em; color: #e6b879; }
.recipe-toggle b { font-size: 7px; letter-spacing: .1em; color: var(--muted); }
.recipe-tray { position: absolute; bottom: 104px; width: min(450px, calc(100vw - 26px)); max-height: min(380px, calc(100vh - 280px)); overflow: auto; padding: 8px; border: 1px solid var(--line); border-radius: 15px; background: rgba(27,37,34,.96); box-shadow: 0 15px 45px rgba(0,0,0,.38); display: grid; grid-template-columns: 1fr 1fr; gap: 6px; backdrop-filter: blur(14px); touch-action: pan-y; }
.recipe-tray button { min-height: 48px; border: 1px solid transparent; border-radius: 9px; background: rgba(255,255,255,.04); padding: 7px 9px; display: flex; align-items: center; gap: 9px; text-align: left; }
.recipe-tray button.selected { border-color: rgba(230,184,121,.65); background: rgba(230,184,121,.1); }
.recipe-tray span { display: grid; gap: 2px; }
.recipe-tray b { font-size: 10px; }
.recipe-tray small { color: var(--muted); font-size: 8px; }
.material { display: inline-block; flex: 0 0 auto; width: 13px; height: 13px; border: 1px solid rgba(255,255,255,.16); border-radius: 2px; vertical-align: -2px; margin-right: 5px; box-shadow: inset 0 -2px rgba(0,0,0,.18); }

.compass { position: absolute; z-index: 3; top: 76px; right: max(16px, env(safe-area-inset-right)); display: grid; justify-items: center; gap: 2px; filter: drop-shadow(0 2px 4px rgba(0,0,0,.6)); pointer-events: none; }
.compass span { display: block; color: #e6b879; font-size: 17px; transform-origin: 50% 60%; }
.compass small { font-size: 7px; letter-spacing: .16em; font-weight: 900; }

.map-tools { position: absolute; z-index: 5; right: max(15px, env(safe-area-inset-right)); top: 50%; transform: translateY(-50%); display: grid; gap: 7px; }
.map-tools button { font-size: 20px; }

.controls { position: absolute; z-index: 6; left: 0; right: 0; bottom: 0; min-height: 154px; padding: 0 max(18px, env(safe-area-inset-right)) var(--safe-bottom) max(18px, env(safe-area-inset-left)); display: flex; justify-content: space-between; align-items: flex-end; pointer-events: none; background: linear-gradient(transparent, rgba(16,24,22,.22)); }
.joystick, .action { pointer-events: auto; }
.joystick { width: 134px; height: 134px; border-radius: 50%; position: relative; border: 1px solid rgba(238,241,223,.2); background: rgba(23,32,29,.58); box-shadow: inset 0 0 30px rgba(0,0,0,.2), 0 8px 30px rgba(0,0,0,.16); backdrop-filter: blur(7px); }
.joystick i { position: absolute; width: 54px; height: 54px; left: 39px; top: 39px; border-radius: 50%; background: rgba(229,232,211,.89); box-shadow: 0 4px 12px rgba(0,0,0,.3); transform: translate(0,0); transition: transform .05s linear; }
.joy-arrow { position: absolute; font-size: 10px; color: rgba(238,241,223,.58); }
.joy-arrow.north { left: 62px; top: 10px; }
.joy-arrow.south { left: 62px; bottom: 10px; transform: rotate(180deg); }
.joy-arrow.east { right: 11px; top: 61px; transform: rotate(90deg); }
.joy-arrow.west { left: 11px; top: 61px; transform: rotate(-90deg); }
.action-buttons { display: flex; gap: 12px; align-items: flex-end; }
.action { width: 105px; height: 76px; border: 1px solid rgba(255,255,255,.2); border-radius: 24px; background: rgba(28,37,34,.84); box-shadow: 0 7px 22px rgba(0,0,0,.28); font-size: 13px; font-weight: 900; letter-spacing: .08em; display: flex; align-items: center; justify-content: center; gap: 8px; }
.action span { display: grid; place-items: center; width: 22px; height: 22px; border: 1px solid rgba(255,255,255,.2); border-radius: 6px; color: var(--muted); font-size: 9px; }
.action.deposit { background: rgba(92,104,76,.9); }
.action:active, .action.active { transform: translateY(2px); filter: brightness(1.18); }
.action:disabled { opacity: .35; }

.repair-panel { position: absolute; z-index: 7; bottom: 176px; left: 50%; transform: translateX(-50%); width: min(360px, calc(100% - 30px)); padding: 10px 11px 10px 15px; border: 1px solid rgba(230,184,121,.32); border-radius: 14px; background: var(--glass); display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 8px; box-shadow: 0 10px 28px rgba(0,0,0,.27); backdrop-filter: blur(12px); }
.repair-panel > div:first-child { display: grid; gap: 2px; }
.repair-panel strong { font-size: 11px; letter-spacing: .12em; color: #e6b879; }
.repair-panel span { font-size: 10px; color: var(--muted); }
.repair-panel button, .flight-button { border: 0; border-radius: 9px; padding: 12px 15px; background: #d29a64; color: #222b27; font-size: 11px; font-weight: 1000; letter-spacing: .08em; }
.repair-progress { grid-column: 1 / -1; height: 4px; overflow: hidden; border-radius: 4px; background: rgba(255,255,255,.1); }
.repair-progress i { display: block; height: 100%; width: 0; background: #e6b879; }
.flight-button { position: static; min-width: 96px; min-height: 44px; box-shadow: 0 4px 15px rgba(0,0,0,.24); }
.flight-button.landing-blocked { background: #a87864; }

.toast { position: absolute; z-index: 10; left: 50%; top: 122px; transform: translate(-50%, -8px); padding: 8px 12px; border-radius: 999px; background: rgba(25,33,31,.86); font-size: 10px; letter-spacing: .05em; opacity: 0; transition: .22s ease; pointer-events: none; white-space: nowrap; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog { width: min(500px, calc(100% - 30px)); max-height: min(90vh, 760px); overflow-y: auto; border: 1px solid var(--line); border-radius: 22px; color: var(--ink); background: #26332f; padding: 27px; box-shadow: 0 24px 70px rgba(0,0,0,.55); user-select: none; touch-action: pan-y; }
dialog::backdrop { background: rgba(9,15,14,.68); backdrop-filter: blur(5px); }
.dialog-close { position: absolute; right: 13px; top: 13px; border: 0; background: transparent; font-size: 24px; color: var(--muted); }
dialog h1 { margin: 2px 0 12px; font: 800 clamp(26px, 6vw, 38px)/1.05 Georgia, serif; }
dialog p { color: #c2cabb; line-height: 1.55; font-size: 14px; }
.eyebrow { margin: 0 0 7px; font-size: 9px; font-weight: 900; letter-spacing: .2em; color: #e6b879; }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin: 18px 0; }
.help-grid div { min-height: 68px; border: 1px solid var(--line); border-radius: 12px; padding: 10px; display: flex; gap: 5px; flex-wrap: wrap; align-content: center; }
.help-grid span { display: block; flex-basis: 100%; color: var(--muted); font-size: 10px; margin-top: 5px; }
kbd { padding: 4px 6px; border: 1px solid #59645d; border-radius: 5px; background: #1d2825; font: 700 9px ui-monospace, monospace; }
.soft { font-size: 11px; }
.field-guide { margin: 16px 0 10px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: rgba(16,25,22,.23); }
.field-guide h2 { margin: 0 0 8px; font-size: 10px; letter-spacing: .14em; color: #e6b879; }
.field-guide p { margin: 7px 0; font-size: 11px; line-height: 1.4; }
.primary { width: 100%; margin-top: 8px; padding: 13px; border: 0; border-radius: 10px; background: #dde2c8; color: #222b27; font-size: 11px; font-weight: 1000; letter-spacing: .12em; }
.menu-choice { width: 100%; margin-top: 10px; padding: 14px; border: 1px solid var(--line); border-radius: 12px; background: #1f2b28; text-align: left; display: grid; gap: 4px; }
.menu-choice b { font-size: 13px; }
.menu-choice span { font-size: 10px; color: var(--muted); }
.menu-choice.danger b { color: #e1a083; }

.debug-panel { position: absolute; z-index: 8; top: 110px; left: 14px; width: 180px; padding: 10px; border: 1px solid var(--line); border-radius: 9px; background: rgba(12,18,17,.82); font: 9px/1.5 ui-monospace, monospace; pointer-events: none; }
.debug-panel b, .debug-panel span { display: block; white-space: pre-line; }
.debug-panel b { color: #e6b879; margin-bottom: 4px; }
.hidden { display: none !important; }

@media (max-width: 680px) {
  .brand span:last-child { display: none; }
  .save-status { display: none; }
  .topbar { min-height: 58px; padding-top: max(8px, env(safe-area-inset-top)); }
  .target-readout { top: 60px; }
  .compass { top: 64px; }
  .map-tools { top: 43%; }
  .controls { min-height: 137px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .cargo-panel { bottom: calc(var(--safe-bottom) + 137px); min-width: 210px; }
  .cargo-readout { min-width: 210px; }
  .recipe-tray { bottom: 104px; grid-template-columns: 1fr 1fr; }
  .joystick { width: 118px; height: 118px; }
  .joystick i { width: 48px; height: 48px; left: 34px; top: 34px; }
  .joy-arrow.north { left: 54px; top: 8px; }
  .joy-arrow.south { left: 54px; bottom: 8px; }
  .joy-arrow.east { right: 9px; top: 53px; }
  .joy-arrow.west { left: 9px; top: 53px; }
  .action { width: 86px; height: 68px; border-radius: 21px; font-size: 11px; display: grid; gap: 0; }
  .action span { display: none; }
  .action-buttons { gap: 8px; }
  .repair-panel { bottom: calc(var(--safe-bottom) + 260px); }
}

@media (min-width: 681px) {
  .cargo-panel { bottom: 20px; }
  .repair-panel { bottom: 130px; }
}

@media (max-width: 390px) {
  .action { width: 78px; }
  .joystick { width: 110px; height: 110px; }
  .joystick i { left: 31px; top: 31px; }
  .joy-arrow.north { left: 50px; }
  .joy-arrow.south { left: 50px; }
  .joy-arrow.east, .joy-arrow.west { top: 49px; }
}

@media (pointer: fine) and (min-width: 800px) {
  .joystick { opacity: .42; transform: scale(.78); transform-origin: bottom left; }
}
