:root {
  --bg: #2a1a0a;
  --panel: rgba(28, 16, 6, 0.78);
  --panel-solid: #3a2410;
  --text: #f6ecd0;
  --muted: #d9c59a;
  --gold: #f0c44a;
  --gold-dark: #c4921a;
  --wood: #6b3e26;
  --leaf: #4f8a32;
  --danger: #c44b2b;
  --safe-t: max(10px, env(safe-area-inset-top));
  --safe-r: max(10px, env(safe-area-inset-right));
  --safe-b: max(12px, env(safe-area-inset-bottom));
  --safe-l: max(10px, env(safe-area-inset-left));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

#app, canvas#c {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.hidden, [hidden] { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 12px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 12px);
  background:
    radial-gradient(ellipse at 50% 20%, rgba(240, 196, 74, 0.16), transparent 55%),
    rgba(18, 10, 2, 0.32);
  pointer-events: auto;
  overflow: auto;
  touch-action: pan-y;
}

.screen.overlay { background: rgba(12, 6, 0, 0.62); z-index: 30; }

.menu-card, .sheet {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid rgba(240, 196, 74, 0.28);
  border-radius: 22px;
  padding: 22px 18px 18px;
  backdrop-filter: blur(10px);
  box-shadow: 0 16px 40px rgba(0,0,0,0.35);
  text-align: center;
}

.menu-card.slim, .sheet.slim { width: min(380px, 100%); }
.sheet {
  width: min(520px, 100%);
  max-height: calc(100dvh - var(--safe-t) - var(--safe-b) - 24px);
  overflow: auto;
  touch-action: pan-y;
  text-align: left;
  padding: 16px;
}

h1, h2 { margin: 0 0 8px; line-height: 1.15; font-weight: 750; }
h1 { font-size: clamp(2rem, 9vw, 3rem); color: var(--gold); text-shadow: 0 2px 0 #6a4a10; }
h2 { font-size: clamp(1.4rem, 6vw, 2rem); color: var(--gold); }

.kicker { margin: 0 0 4px; letter-spacing: 0.12em; text-transform: uppercase; font-size: 0.72rem; color: var(--gold); }
.tagline { margin: 0 0 18px; font-size: 1.15rem; }
.hint { margin: 14px 0 0; color: var(--muted); font-size: 0.95rem; line-height: 1.4; }
.controls-hint { list-style: none; padding: 0; margin: 16px 0 0; color: var(--muted); font-size: 0.9rem; line-height: 1.45; }
.controls-hint li { margin: 0; }

@media (pointer: fine) {
  .controls-hint .mob { display: none; }
}
@media (pointer: coarse) {
  .controls-hint .desk { display: none; }
}

.btn, .btn-action, .shop-item button, .btn-icon {
  font: inherit;
  cursor: pointer;
  border: 0;
  color: var(--text);
  appearance: none;
}

.btn {
  display: block;
  width: 100%;
  margin: 10px 0 0;
  padding: 14px 16px;
  min-height: 56px;
  border-radius: 16px;
  font-size: 1.15rem;
  font-weight: 700;
}

.btn-primary { background: linear-gradient(#f0c44a, #c4921a); color: #2a1a0a; }
.btn-primary:active { filter: brightness(0.95); }
.btn-secondary { background: #5b3a22; }
.btn-ghost { background: transparent; border: 1px solid rgba(246, 236, 208, 0.3) !important; }
.btn-close { width: auto; min-height: 48px; margin: 0; padding: 8px 14px; background: #5b3a22; font-size: 1rem; }
.btn:disabled { opacity: 0.45; cursor: default; }

#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; }
#hud .interactive { pointer-events: auto; }

.hud-panel {
  background: var(--panel);
  border: 1px solid rgba(240, 196, 74, 0.22);
  border-radius: 14px;
  padding: 8px 12px;
  backdrop-filter: blur(8px);
  font-variant-numeric: tabular-nums;
}

#hud-top {
  position: absolute;
  top: var(--safe-t);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  justify-content: center;
  max-width: calc(100% - var(--safe-l) - var(--safe-r) - 72px);
  font-size: 0.92rem;
  font-weight: 650;
}

#hud-tool {
  position: absolute;
  top: calc(var(--safe-t) + 52px);
  left: var(--safe-l);
  font-size: 0.9rem;
}

#detector {
  position: absolute;
  top: calc(var(--safe-t) + 52px);
  right: var(--safe-r);
  width: min(220px, 46vw);
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.8rem;
}

.det-bar { height: 10px; background: #2a1a0a; border-radius: 99px; overflow: hidden; }
#det-fill { height: 100%; width: 0; background: linear-gradient(90deg, #4f8a32, #f0c44a, #c44b2b); border-radius: 99px; }

#crosshair {
  position: absolute;
  left: 50%;
  top: 48%;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border: 2px solid rgba(246, 236, 208, 0.85);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.3);
}

#toast {
  position: absolute;
  left: 50%;
  top: 28%;
  transform: translateX(-50%);
  min-width: 0;
  max-width: min(90vw, 360px);
  text-align: center;
  font-weight: 700;
  font-size: 1.05rem;
  text-shadow: 0 2px 8px #000;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
}
#toast.show { opacity: 1; }

#prompt {
  position: absolute;
  left: 50%;
  bottom: 38%;
  transform: translateX(-50%);
  font-size: 0.95rem;
}

.btn-icon {
  position: absolute;
  top: var(--safe-t);
  right: var(--safe-r);
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid rgba(240, 196, 74, 0.22);
  font-size: 1.3rem;
  color: var(--text);
}

#joystick {
  position: absolute;
  left: var(--safe-l);
  bottom: var(--safe-b);
  width: 148px;
  height: 148px;
}

#joy-base {
  width: 132px;
  height: 132px;
  margin: 8px;
  border-radius: 50%;
  background: rgba(20, 10, 2, 0.4);
  border: 3px solid rgba(246, 236, 208, 0.35);
  position: relative;
}

#joy-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 62px;
  height: 62px;
  margin: -31px 0 0 -31px;
  border-radius: 50%;
  background: rgba(240, 196, 74, 0.85);
  border: 3px solid #f6ecd0;
  box-shadow: 0 4px 10px rgba(0,0,0,0.35);
  will-change: transform;
}

#actions {
  position: absolute;
  right: var(--safe-r);
  bottom: var(--safe-b);
  display: grid;
  grid-template-columns: auto auto;
  grid-template-areas:
    "sell sell"
    "tool shop"
    "search search";
  gap: 8px;
  justify-items: end;
  align-items: end;
}

#btn-sell { grid-area: sell; }
#btn-tool { grid-area: tool; }
#btn-shop { grid-area: shop; }
#btn-search { grid-area: search; }

.btn-action {
  min-width: 72px;
  min-height: 72px;
  padding: 8px 12px;
  border-radius: 18px;
  background: rgba(28, 16, 6, 0.82);
  border: 2px solid rgba(240, 196, 74, 0.45);
  color: var(--text);
  font-weight: 750;
  font-size: 0.95rem;
  backdrop-filter: blur(8px);
}

.btn-action:active, .btn-action.held {
  background: #c4921a;
  color: #2a1a0a;
  transform: scale(0.97);
}

.btn-search {
  min-width: 122px;
  min-height: 122px;
  border-radius: 50%;
  font-size: 1.2rem;
  background: radial-gradient(circle at 40% 30%, #f0c44a, #c4921a 70%);
  color: #2a1a0a;
  border-color: #f6ecd0;
}

.btn-sell {
  background: #4f8a32;
  border-color: #cfe8b8;
  min-height: 58px;
  min-width: 132px;
  border-radius: 16px;
}

.touch-only { display: none; }
body.touch-ui .touch-only { display: block; }
body.touch-ui #crosshair { opacity: 0.55; }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.sheet-head h2 { margin: 0; }

.sell-box {
  background: rgba(79, 138, 50, 0.18);
  border: 1px solid rgba(79, 138, 50, 0.45);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 14px;
  font-size: 1rem;
}

.shop-list { display: flex; flex-direction: column; gap: 10px; }

.shop-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 10px;
  align-items: center;
  background: rgba(0,0,0,0.22);
  border-radius: 14px;
  padding: 12px;
}
.shop-item h3 { margin: 0; font-size: 1.05rem; color: var(--gold); }
.shop-item p { margin: 0; grid-column: 1 / -1; color: var(--muted); font-size: 0.9rem; }
.shop-item .price { font-weight: 700; }
.shop-item button {
  min-height: 48px;
  min-width: 96px;
  border-radius: 12px;
  background: linear-gradient(#f0c44a, #c4921a);
  color: #2a1a0a;
  font-weight: 700;
  padding: 8px 12px;
}
.shop-item button.secondary { background: #5b3a22; color: var(--text); }
.shop-item.is-active { outline: 2px solid var(--gold); }

.progress {
  height: 14px;
  background: #2a1a0a;
  border-radius: 99px;
  overflow: hidden;
  margin: 16px 0 8px;
}
#load-bar { height: 100%; width: 8%; background: linear-gradient(90deg, #c4921a, #f0c44a); transition: width 0.2s; }

.quality-line {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 16px;
  text-align: left;
  font-weight: 650;
}
.quality-line select {
  font: inherit;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid rgba(240,196,74,0.3);
  background: #2a1a0a;
  color: var(--text);
  padding: 8px 10px;
  touch-action: manipulation;
}

.win-stats { list-style: none; padding: 0; margin: 16px 0; font-size: 1.1rem; line-height: 1.6; }

/* Landscape: controls on the sides, more room to see the pile */
@media (orientation: landscape) and (max-height: 500px) {
  .menu-card, .sheet { max-height: calc(100dvh - 16px); }
  h1 { font-size: 1.8rem; }
  #hud-top { font-size: 0.82rem; padding: 6px 10px; gap: 6px 10px; }
  #hud-tool, #detector { top: calc(var(--safe-t) + 42px); }
  #joystick { width: 126px; height: 126px; bottom: var(--safe-b); }
  #joy-base { width: 110px; height: 110px; }
  #joy-knob { width: 50px; height: 50px; margin: -25px 0 0 -25px; }
  #actions {
    grid-template-columns: 1fr;
    grid-template-areas:
      "sell"
      "tool"
      "shop"
      "search";
    bottom: max(8px, var(--safe-b));
  }
  .btn-action { min-width: 64px; min-height: 52px; font-size: 0.85rem; }
  .btn-search { min-width: 86px; min-height: 86px; font-size: 1rem; }
  #prompt { bottom: 22%; }
}

@media (orientation: landscape) and (min-height: 501px) {
  #actions {
    right: var(--safe-r);
    bottom: var(--safe-b);
  }
}

@media (max-width: 360px) {
  #joystick { transform: scale(0.9); transform-origin: left bottom; }
  .btn-search { min-width: 104px; min-height: 104px; }
}
