:root {
  --cream: #fff4e0;
  --paper: #fbead0;
  --paper2: #f3d9b2;
  --wood: #6e3f24;
  --wood-dark: #3d2112;
  --ink: #3b1f10;
  --muted: #8a6a52;
  --butter: #ffcf4a;
  --berry: #e8546f;
  --mint: #3fb59a;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #3d2112; color: var(--ink); font-family: var(--sans); -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; cursor: pointer; }
kbd { font: 600 0.85em var(--sans); background: #fff; border: 1px solid #d9bf98; border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; }

#app { position: fixed; inset: 0; display: flex; }
#scene-wrap { position: relative; flex: 1; min-width: 0; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: manipulation; }

#hud { position: absolute; top: 10px; right: 10px; display: flex; gap: 8px; z-index: 2; }
.hud-btn { width: 40px; height: 40px; border-radius: 12px; border: 2px solid rgba(255,230,190,0.5); background: rgba(61,33,18,0.7); color: #ffe9c4; display: grid; place-items: center; backdrop-filter: blur(4px); }
.hud-btn:hover { background: rgba(110,63,36,0.85); }
#mute-x { display: none; }
.muted #mute-x { display: inline; }
.muted #mute-waves { display: none; }

#toasts { position: absolute; left: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 8px; z-index: 3; pointer-events: none; max-width: min(320px, 80%); }
.toast { display: flex; gap: 10px; align-items: center; background: linear-gradient(180deg, #fff8ea, #f6e0bb); border: 2px solid #c89a5e; border-radius: 14px; padding: 8px 12px; box-shadow: 0 8px 22px rgba(40,15,0,0.35); animation: toast-in 0.35s cubic-bezier(.2,1.4,.4,1) both; font-size: 13px; }
.toast.out { animation: toast-out 0.4s ease forwards; }
.toast img { width: 34px; height: 34px; flex: none; }
.toast b { display: block; font-family: var(--serif); font-size: 15px; }
@keyframes toast-in { from { transform: translateY(20px) scale(0.9); opacity: 0; } }
@keyframes toast-out { to { transform: translateX(-30px); opacity: 0; } }

/* ---------- store ---------- */
#store { width: 360px; flex: none; display: flex; flex-direction: column; background: linear-gradient(180deg, var(--paper), var(--paper2)); border-left: 6px solid var(--wood); box-shadow: -10px 0 30px rgba(0,0,0,0.25); min-height: 0; }
#tabs { display: flex; background: var(--wood); padding: 8px 8px 0; gap: 4px; flex: none; }
.tab { flex: 1; border: 0; padding: 9px 4px 10px; border-radius: 10px 10px 0 0; background: #8a5232; color: #f6dcb4; font-weight: 700; font-size: 13px; position: relative; white-space: nowrap; }
.tab.active { background: var(--paper); color: var(--ink); }
.badge { display: none; position: absolute; top: -4px; right: 2px; min-width: 18px; height: 18px; border-radius: 9px; background: var(--berry); color: #fff; font-size: 11px; line-height: 18px; padding: 0 5px; }
.badge.on { display: inline-block; }
.panel { display: none; overflow-y: auto; flex: 1; min-height: 0; padding: 10px; overscroll-behavior: contain; }
.panel.active { display: block; }

.buy-amt { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; font-size: 12px; font-weight: 700; color: var(--muted); }
.buy-amt span { margin-right: 4px; text-transform: uppercase; letter-spacing: 0.05em; }
.buy-amt button { flex: 1; border: 2px solid #d9bf98; background: #fffaf0; border-radius: 9px; padding: 5px 0; font-weight: 700; font-size: 13px; }
.buy-amt button.on { background: var(--wood); color: var(--cream); border-color: var(--wood); }

.list { display: flex; flex-direction: column; gap: 6px; }
.item { display: flex; align-items: center; gap: 10px; text-align: left; width: 100%; padding: 7px 10px 7px 7px; border-radius: 14px; border: 2px solid #e2c79f; background: linear-gradient(180deg, #fffaf0, #fbecd2); transition: transform 0.08s, box-shadow 0.15s, opacity 0.2s; position: relative; }
.item:hover { box-shadow: 0 4px 12px rgba(110,63,36,0.18); }
.item:active { transform: scale(0.98); }
.item.poor { opacity: 0.55; }
.item.poor .cost { color: #b4483a; }
.item.locked { pointer-events: none; opacity: 0.7; }
.item.locked img { filter: brightness(0) opacity(0.3); }
.item.locked .name { color: var(--muted); }
.item img { width: 48px; height: 48px; flex: none; }
.item .txt { flex: 1; min-width: 0; }
.item .name { font: 700 16px var(--serif); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .cost { font-size: 13px; font-weight: 700; color: #6b8a2a; display: block; }
.item .desc { font-size: 12px; color: var(--muted); display: block; line-height: 1.3; }
.item .own { font: 700 26px var(--serif); color: rgba(110,63,36,0.35); flex: none; }
.item.flash { animation: flash 0.35s; }
@keyframes flash { 50% { background: #fff3b0; } }
.item .rate { font-size: 11px; color: var(--muted); display: block; }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 16px 8px; }

h4.sub { margin: 16px 2px 6px; font: 700 12px var(--sans); text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
.grid img { width: 100%; aspect-ratio: 1; border-radius: 10px; background: rgba(255,255,255,0.45); padding: 3px; cursor: pointer; }
.grid img.sel { outline: 3px solid var(--wood); }
.note { margin: 2px 2px 8px; font-size: 13px; color: var(--muted); }
.info { min-height: 52px; background: #fffaf0; border: 2px solid #e2c79f; border-radius: 12px; padding: 8px 10px; font-size: 13px; margin-bottom: 10px; }
.info b { font-family: var(--serif); font-size: 15px; }

#heirloom-box { background: linear-gradient(180deg, #4e2a17, #2f170b); color: var(--cream); border-radius: 16px; padding: 14px; text-align: center; }
#heirloom-box h3 { margin: 0 0 4px; font: 700 20px var(--serif); color: var(--butter); }
#heirloom-box p { margin: 6px 0; font-size: 13px; color: #f0d3a4; }
#heirloom-box .big { font: 700 32px var(--serif); color: #fff; }
#stats { margin-top: 12px; display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 13px; }
#stats span:nth-child(odd) { color: var(--muted); }
#stats span:nth-child(even) { font-weight: 700; text-align: right; }

/* ---------- overlay ---------- */
#overlay { position: fixed; inset: 0; display: none; place-items: center; background: radial-gradient(ellipse at center, rgba(40,15,0,0.45), rgba(20,8,0,0.75)); z-index: 10; padding: 16px; overflow-y: auto; }
#overlay.show { display: grid; }
.card { display: none; width: min(460px, 100%); background: linear-gradient(180deg, #fff8ea, #f6e0bb); border: 3px solid #c89a5e; border-radius: 24px; padding: 24px 24px 20px; text-align: center; box-shadow: 0 24px 60px rgba(0,0,0,0.45), inset 0 2px 0 #fff; animation: card-in 0.4s cubic-bezier(.2,1.3,.4,1); }
.card.show { display: block; }
@keyframes card-in { from { transform: translateY(24px) scale(0.94); opacity: 0; } }
.logo { font: 700 clamp(44px, 11vw, 66px)/0.9 var(--serif); color: #fff3d6; letter-spacing: -0.01em; text-shadow: 0 3px 0 #a0582c, 0 6px 0 #6e3f24, 0 12px 24px rgba(60,20,0,0.4); margin: 4px 0 10px; }
.logo span { color: var(--butter); font-size: 0.8em; }
.card h2 { margin: 0 0 8px; font: 700 30px var(--serif); color: var(--wood); }
.tag { margin: 0 0 12px; color: var(--muted); font-size: 15px; }
.welcome { display: none; margin: 0 0 14px; padding: 10px; border-radius: 12px; background: #fff; border: 2px dashed var(--mint); font-size: 14px; }
.welcome.on { display: block; }
.btn { border: 0; border-radius: 14px; padding: 12px 22px; font-weight: 800; font-size: 16px; background: #f1dcc0; color: var(--ink); box-shadow: 0 4px 0 #c9a77c; transition: transform 0.08s; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #c9a77c; }
.btn.primary { background: linear-gradient(180deg, #ff7a8e, #e0485f); color: #fff; font-size: 19px; padding: 14px 34px; box-shadow: 0 5px 0 #a82d41, 0 10px 20px rgba(200,50,70,0.3); }
.btn.danger { background: #f6d0c8; color: #8a2a1a; box-shadow: 0 4px 0 #d39a8c; }
.btn.small { font-size: 12px; padding: 6px 12px; margin-top: 14px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.row { display: flex; gap: 10px; justify-content: center; margin-top: 12px; flex-wrap: wrap; }
.how { text-align: left; margin: 18px 0 10px; padding: 0 0 0 18px; font-size: 14px; line-height: 1.5; }
.controls { font-size: 12px; color: var(--muted); line-height: 1.9; margin: 6px 0 0; }
.best { font-size: 13px; color: var(--wood); font-weight: 700; margin: 8px 0 0; }
.summary { display: grid; grid-template-columns: 1fr auto; gap: 6px 14px; text-align: left; margin: 10px 0 18px; font-size: 15px; }
.summary span:nth-child(even) { font-weight: 800; text-align: right; }
.summary .hl { color: #b8680a; }
.in-hub-only { display: none; }
.hub .in-hub-only { display: inline-block; }

/* ---------- narrow screens: store at the bottom ---------- */
@media (max-width: 820px) {
  #app { flex-direction: column; }
  #scene-wrap { flex: 0 0 50%; }
  #store { width: auto; flex: 1; border-left: 0; border-top: 6px solid var(--wood); box-shadow: 0 -10px 30px rgba(0,0,0,0.25); }
  .tab { font-size: 12px; }
  .item img { width: 40px; height: 40px; }
  .item .name { font-size: 15px; }
  .item .own { font-size: 22px; }
  .card { padding: 18px 16px 16px; }
  .how { font-size: 13px; margin: 12px 0 8px; }
}
@media (max-width: 820px) and (min-aspect-ratio: 4/3) {
  #app { flex-direction: row; }
  #scene-wrap { flex: 1; }
  #store { width: 300px; flex: none; border-top: 0; border-left: 6px solid var(--wood); }
}
@media (max-height: 640px) {
  #overlay.show { display: block; }
  .card.show { margin: 12px auto; }
}
@media (prefers-reduced-motion: reduce) {
  .toast, .card, .item.flash { animation: none; }
}
