:root {
    --terra: #d97724;
    --ochre: #eab308;
    --palm: #16a34a;
    --indigo: #1e3a8a;
    --sun: #f97316;
    --earth: #78350f;
    --gold: #f59e0b;
    --ink: #1c1206;
    --paper: #fff7e8;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    padding: 0;
    color: var(--ink);
    background: #2a1608;
    font-family: 'Segoe UI', 'Trebuchet MS', Arial, Helvetica, sans-serif;
    -webkit-text-size-adjust: 100%;
}

#app {
    position: relative;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background: radial-gradient(circle at 50% 20%, #f9b95d, #c96a1e 70%, #78350f);
}

#game-container {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
}

#hud {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    pointer-events: none;
}

#hud > * { pointer-events: auto; }

/* ----------------------------- top bar ----------------------------- */
.topbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    margin: 8px;
    border-radius: 14px;
    background: rgba(28, 18, 6, 0.86);
    color: var(--paper);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
    flex-wrap: wrap;
}

.stat {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 12px 4px 8px;
    border-radius: 10px;
    background: rgba(255, 247, 232, 0.08);
    min-width: 84px;
}

.stat.cash { background: linear-gradient(135deg, #b45309, #f59e0b); color: #fff; }
.stat-ico { display: inline-flex; color: #ffd27a; }
.stat.cash .stat-ico { color: #fff; }
.stat small { display: block; font-size: 10px; opacity: 0.85; text-transform: uppercase; letter-spacing: 0.04em; }
.stat strong { font-size: 16px; font-variant-numeric: tabular-nums; }

.repbar {
    width: 72px;
    height: 7px;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.2);
    overflow: hidden;
    margin-top: 3px;
}
.repbar i { display: block; height: 100%; background: linear-gradient(90deg, #dc2626, #eab308, #16a34a); transition: width 0.4s; }

.mute-btn {
    margin-left: auto;
    width: 44px;
    height: 44px;
    border-radius: 10px;
    border: none;
    background: rgba(255, 247, 232, 0.12);
    color: var(--paper);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.mute-btn:hover { background: rgba(255, 247, 232, 0.25); }

/* ----------------------------- overlays ---------------------------- */
.overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    z-index: 20;
}
.overlay.center { text-align: center; }
.overlay.dim { background: rgba(20, 10, 2, 0.55); backdrop-filter: blur(2px); }

.panel {
    background: var(--paper);
    border-radius: 18px;
    padding: 22px;
    max-width: 640px;
    width: 100%;
    max-height: calc(100dvh - 96px);
    overflow-y: auto;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
    border: 3px solid var(--gold);
}

.menu-panel { max-width: 520px; }
.logo-row { display: flex; align-items: center; justify-content: center; gap: 12px; }
.logo-emoji { font-size: 44px; }
.panel h1 { margin: 4px 0; font-size: 38px; letter-spacing: 1px; color: var(--earth); }
.tagline { margin: 0 0 14px; color: var(--terra); font-weight: 700; }
.story p { margin: 8px 0; font-size: 15px; line-height: 1.5; }
.goal { background: #fef3c7; border-left: 4px solid var(--gold); padding: 8px 12px; border-radius: 8px; }
.hint { font-size: 12px; opacity: 0.7; margin-top: 10px; }
kbd {
    background: #1c1206; color: #ffd27a; border-radius: 5px; padding: 1px 6px;
    font-size: 11px; font-family: inherit;
}

/* ----------------------------- buttons ----------------------------- */
.btn {
    border: none;
    border-radius: 12px;
    padding: 12px 18px;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    min-height: 48px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: transform 0.08s, filter 0.15s;
    color: #fff;
    background: #6b7280;
}
.btn:active { transform: scale(0.96); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: linear-gradient(135deg, var(--terra), var(--sun)); box-shadow: 0 4px 0 #9a4a10; }
.btn.primary:disabled { box-shadow: none; }
.btn.big { font-size: 18px; padding: 14px 26px; width: 100%; margin-top: 12px; }
.btn.ghost { background: transparent; color: var(--earth); border: 2px solid #d6c3a3; }
.btn.ghost.small { font-size: 13px; min-height: 40px; margin-top: 8px; padding: 6px 14px; }

/* ----------------------------- play layout ------------------------- */
.play-wrap {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 76px;
    display: flex;
    flex-direction: column;
    padding: 0 10px 10px;
    pointer-events: none;
}
.play-wrap > * { pointer-events: auto; }

.news-bar {
    background: rgba(30, 58, 138, 0.92);
    color: #fff;
    border-radius: 10px;
    padding: 8px 12px;
    font-size: 13px;
    margin-bottom: 8px;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}
.news-bar.slim { background: var(--indigo); margin-bottom: 12px; }

.tabs {
    display: flex;
    gap: 6px;
    margin-bottom: 8px;
}
.tab {
    flex: 1;
    min-height: 48px;
    border-radius: 12px 12px 0 0;
    border: none;
    background: rgba(28, 18, 6, 0.7);
    color: var(--paper);
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}
.tab.active { background: var(--paper); color: var(--ink); }

.tab-body {
    flex: 1;
    overflow-y: auto;
    background: rgba(255, 247, 232, 0.96);
    border-radius: 0 0 14px 14px;
    padding: 12px;
    min-height: 0;
}

.actionbar {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 8px;
}
.rep-chip {
    background: rgba(28, 18, 6, 0.8);
    color: #ffd27a;
    padding: 8px 14px;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 700;
}
.actionbar .btn.primary { margin-left: auto; }

/* ----------------------------- market ------------------------------ */
.market-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 12px;
}
.good-card {
    background: #fff;
    border: 2px solid #eadfc9;
    border-radius: 14px;
    padding: 12px;
    box-shadow: 0 3px 10px rgba(120, 53, 15, 0.08);
}
.good-head { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
.good-emoji { font-size: 30px; }
.good-head h3 { margin: 0; font-size: 16px; color: var(--earth); }
.good-head small { color: #8a7a5f; }
.price-row { display: flex; gap: 8px; margin-bottom: 6px; }
.price {
    flex: 1;
    border-radius: 10px;
    padding: 6px 10px;
    text-align: center;
}
.price small { display: block; font-size: 10px; text-transform: uppercase; opacity: 0.8; }
.price b { font-size: 17px; font-variant-numeric: tabular-nums; }
.price.buy { background: #fee2e2; color: #b91c1c; }
.price.sell { background: #dcfce7; color: #15803d; }
.owned { font-size: 13px; color: #6b5a3e; margin-bottom: 8px; }
.trade-row { display: flex; gap: 6px; align-items: stretch; }
.qty { display: flex; align-items: center; }
.qty button {
    width: 36px; height: 44px; border: 2px solid #eadfc9; background: #fff7e8;
    font-size: 18px; font-weight: 700; cursor: pointer; color: var(--earth);
}
.qty button:first-child { border-radius: 8px 0 0 8px; }
.qty button:last-child { border-radius: 0 8px 8px 0; }
.qty input {
    width: 46px; height: 44px; text-align: center; border: 2px solid #eadfc9; border-inline: none;
    font-size: 15px; font-weight: 700; -moz-appearance: textfield;
}
.qty input::-webkit-outer-spin-button, .qty input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.buy-btn { background: linear-gradient(135deg, #dc2626, #f97316); flex: 1; }
.sell-btn { background: linear-gradient(135deg, #15803d, #16a34a); flex: 1; }
.trade-hint { font-size: 11px; color: #9a8a6e; margin-top: 6px; }

/* ----------------------------- Ajo --------------------------------- */
.ajo-wrap { max-width: 620px; margin: 0 auto; }
.ajo-hero {
    display: flex; gap: 14px; align-items: center;
    background: linear-gradient(135deg, var(--indigo), #3b5bbf);
    color: #fff; border-radius: 14px; padding: 14px; margin-bottom: 12px;
}
.ajo-pot {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    background: rgba(245, 158, 11, 0.9); border-radius: 12px; padding: 10px 16px; min-width: 96px;
}
.ajo-pot b { font-size: 20px; }
.ajo-pot small { font-size: 10px; text-transform: uppercase; }
.ajo-info h3 { margin: 0 0 4px; }
.ajo-info p { margin: 0; font-size: 13px; opacity: 0.92; }
.ajo-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 8px; }
.ajo-card {
    display: flex; align-items: center; gap: 8px;
    background: #fff; border: 2px solid #eadfc9; border-radius: 12px; padding: 8px 10px;
}
.ajo-card.receiver { border-color: var(--gold); background: #fef3c7; box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.35); }
.ajo-card.you { outline: 2px dashed var(--terra); }
.ajo-avatar { font-size: 26px; }
.ajo-meta { flex: 1; min-width: 0; }
.ajo-meta b { display: block; font-size: 14px; color: var(--earth); }
.ajo-meta small { display: block; font-size: 11px; color: #8a7a5f; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
    display: inline-flex; align-items: center; gap: 4px;
    border-radius: 999px; padding: 3px 8px; font-size: 10px; font-weight: 800;
}
.badge.ok { background: #dcfce7; color: #15803d; }
.badge.wait { background: #f1f5f9; color: #64748b; }
.badge.recv { background: var(--gold); color: #422006; }
.ajo-note { font-size: 12px; color: #6b5a3e; margin-top: 10px; line-height: 1.5; }

/* ----------------------------- Stall ------------------------------- */
.stall-wrap { max-width: 640px; margin: 0 auto; }
.stall-summary {
    display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px;
}
.stall-summary > div {
    flex: 1; min-width: 130px; background: #fff; border: 2px solid #eadfc9;
    border-radius: 12px; padding: 10px;
}
.stall-summary small { display: block; font-size: 11px; text-transform: uppercase; color: #8a7a5f; }
.stall-summary b { font-size: 15px; color: var(--earth); }
.stall-wrap h3 { color: var(--earth); margin: 8px 0; }
.upgrade-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.upgrade-card {
    background: #fff; border: 2px solid #eadfc9; border-radius: 14px; padding: 12px; text-align: center;
}
.upgrade-card.owned { border-color: var(--palm); background: #f0fdf4; }
.upgrade-card.avail { border-color: var(--gold); }
.upgrade-card.locked { opacity: 0.6; }
.up-tier { font-size: 11px; font-weight: 800; color: var(--terra); text-transform: uppercase; }
.upgrade-card h4 { margin: 4px 0; font-size: 15px; color: var(--earth); }
.upgrade-card p { margin: 0 0 10px; font-size: 12px; color: #6b5a3e; }
.owned-badge { justify-content: center; margin-top: 6px; }

/* ----------------------------- Haggle ------------------------------ */
.haggle-panel { max-width: 560px; }
.haggle-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.npc-portrait {
    width: 64px; height: 64px; border-radius: 50%;
    background: linear-gradient(135deg, var(--terra), var(--gold));
    display: flex; align-items: center; justify-content: center; font-size: 34px;
    border: 3px solid var(--earth);
}
.haggle-head h2 { margin: 0; font-size: 20px; color: var(--earth); }
.deal-type { font-size: 13px; color: #6b5a3e; }
.round-chip {
    margin-left: auto; background: var(--indigo); color: #fff;
    border-radius: 999px; padding: 6px 12px; font-size: 12px; font-weight: 800;
}
.patience { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; font-size: 13px; }
.patience label { font-weight: 700; color: var(--earth); }
.patience-bar { flex: 1; height: 14px; border-radius: 8px; background: #eadfc9; overflow: hidden; }
.patience-bar i { display: block; height: 100%; transition: width 0.4s, background 0.4s; }
.price-compare { display: flex; gap: 8px; margin-bottom: 12px; }
.pc {
    flex: 1; background: #fff; border: 2px solid #eadfc9; border-radius: 10px;
    padding: 8px; text-align: center;
}
.pc small { display: block; font-size: 10px; text-transform: uppercase; color: #8a7a5f; }
.pc b { font-size: 17px; color: var(--earth); font-variant: tabular-nums; }
.pc.now { border-color: var(--gold); background: #fffbeb; }
.haggle-log {
    background: #1c1206; color: #fde9c8; border-radius: 12px; padding: 10px 12px;
    min-height: 84px; max-height: 130px; overflow-y: auto; margin-bottom: 12px; text-align: left;
}
.haggle-log p { margin: 4px 0; font-size: 13px; line-height: 1.4; opacity: 0.75; }
.haggle-log p.latest { opacity: 1; font-weight: 600; }
.tactics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tactic { flex-direction: column; gap: 2px; padding: 10px 6px; font-size: 14px; }
.tactic small { font-weight: 500; font-size: 10px; opacity: 0.9; }
.tactic kbd { margin: 2px 0; }
.tactic.friendly { background: linear-gradient(135deg, #15803d, #22c55e); }
.tactic.firm { background: linear-gradient(135deg, #b45309, #f59e0b); }
.tactic.walk { background: linear-gradient(135deg, #991b1b, #ef4444); }
.haggle-result { margin-top: 4px; }
.result-good { background: #dcfce7; color: #14532d; border-radius: 10px; padding: 12px; font-size: 14px; }
.result-bad { background: #fee2e2; color: #7f1d1d; border-radius: 10px; padding: 12px; font-size: 14px; }
.result-actions { display: flex; gap: 8px; margin-top: 10px; }
.result-actions .btn { flex: 1; }

/* ----------------------------- Summary ----------------------------- */
.summary-panel { max-width: 480px; }
.summary-panel h2 { color: var(--earth); margin: 0 0 10px; }
.ledger { width: 100%; border-collapse: collapse; font-size: 14px; margin-bottom: 12px; }
.ledger td { padding: 8px 6px; border-bottom: 1px solid #eadfc9; text-align: left; }
.ledger td:last-child { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.ledger tr.good td:last-child { color: #15803d; }
.ledger tr.bad td:last-child { color: #b91c1c; }
.ledger tr.net td { border-top: 2px solid var(--earth); font-weight: 800; font-size: 15px; }
.ledger tr.jackpot td { background: #fef3c7; color: #92400e; font-weight: 800; }
.cash-line {
    display: flex; align-items: center; justify-content: center; gap: 6px;
    background: #fff7e8; border-radius: 10px; padding: 10px; margin-bottom: 6px; color: var(--earth);
}

/* ----------------------------- End screens ------------------------- */
.end-panel { max-width: 460px; }
.end-panel h2 { font-size: 30px; margin: 0 0 10px; }
.end-panel.good { border-color: var(--gold); }
.end-panel.good h2 { color: #b45309; display: flex; align-items: center; justify-content: center; gap: 8px; }
.end-panel.bad h2 { color: #b91c1c; }
.end-panel p { line-height: 1.5; }
.end-stat { font-size: 16px; color: var(--earth); }

/* ----------------------------- toast ------------------------------- */
.toast {
    position: absolute;
    left: 50%;
    bottom: 18px;
    transform: translateX(-50%);
    padding: 12px 20px;
    border-radius: 12px;
    font-weight: 700;
    font-size: 14px;
    color: #fff;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    z-index: 60;
    max-width: 90%;
    text-align: center;
    animation: toast-in 0.25s ease;
}
.toast.good { background: linear-gradient(135deg, #15803d, #16a34a); }
.toast.bad { background: linear-gradient(135deg, #b91c1c, #ef4444); }
.toast.info { background: linear-gradient(135deg, #1e3a8a, #3b5bbf); }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ----------------------------- responsive -------------------------- */
@media (max-width: 560px) {
    .panel h1 { font-size: 30px; }
    .tactics { grid-template-columns: 1fr; }
    .tactic { flex-direction: row; justify-content: space-between; }
    .price-compare { flex-direction: column; }
    .stat { min-width: 70px; padding: 4px 8px; }
    .stat strong { font-size: 14px; }
    .topbar { gap: 4px; padding: 6px; }
    .play-wrap { top: 110px; }
}

@media (pointer: coarse) {
    .btn { min-height: 56px; }
    .tab { min-height: 54px; }
}

/* ----------------------------- Difficulty selector ------------------ */
.diff-label {
    font-weight: 800; color: var(--earth); text-transform: uppercase;
    letter-spacing: 0.04em; font-size: 13px; margin: 14px 0 8px;
}
.diff-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.diff-card {
    text-align: left; cursor: pointer; font: inherit; color: var(--ink);
    background: #fff; border: 2px solid #eadfc9; border-radius: 14px; padding: 12px;
    display: flex; flex-direction: column; gap: 6px; transition: transform 0.12s, box-shadow 0.12s, border-color 0.12s;
}
.diff-card:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(120, 53, 15, 0.18); }
.diff-card.active { box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.55); transform: translateY(-2px); }
.diff-card.easy.active { border-color: var(--palm); box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.4); }
.diff-card.normal.active { border-color: var(--gold); }
.diff-card.hard.active { border-color: #dc2626; box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.4); }
.diff-head { display: flex; align-items: center; justify-content: space-between; }
.diff-badge {
    font-size: 11px; font-weight: 900; text-transform: uppercase; letter-spacing: 0.05em;
    padding: 3px 10px; border-radius: 999px; color: #fff;
}
.diff-badge.easy { background: var(--palm); }
.diff-badge.normal { background: var(--gold); color: #422006; }
.diff-badge.hard { background: #dc2626; }
.diff-check { color: var(--palm); display: inline-flex; }
.diff-cash { font-size: 22px; font-weight: 900; color: var(--earth); font-variant-numeric: tabular-nums; }
.diff-meta { font-size: 11px; color: #6b5a3e; font-weight: 700; }
.diff-risk { display: flex; align-items: center; gap: 6px; }
.diff-risk small { font-size: 10px; text-transform: uppercase; color: #8a7a5f; }
.risk-bar { flex: 1; height: 8px; border-radius: 6px; background: #eadfc9; overflow: hidden; }
.risk-bar i { display: block; height: 100%; border-radius: 6px; }
.risk-bar .lvl-EASY { width: 33%; background: var(--palm); }
.risk-bar .lvl-NORMAL { width: 66%; background: var(--gold); }
.risk-bar .lvl-HARD { width: 100%; background: #dc2626; }
.diff-blurb { margin: 0; font-size: 11px; line-height: 1.4; color: #6b5a3e; }

/* ----------------------------- End screens (win/loss) --------------- */
.end-badge {
    display: inline-flex; align-items: center; gap: 6px; justify-content: center;
    font-size: 12px; font-weight: 900; letter-spacing: 0.12em; text-transform: uppercase;
    padding: 5px 14px; border-radius: 999px; margin-bottom: 8px;
}
.end-badge.bad { background: #fee2e2; color: #b91c1c; }
.end-badge.good { background: var(--gold); color: #422006; }
.loss-reason {
    background: #fef2f2; border-left: 4px solid #dc2626; color: #7f1d1d;
    border-radius: 8px; padding: 10px 12px; font-size: 14px; font-weight: 600; text-align: left; margin: 4px 0 12px;
}
.stat-grid {
    display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 6px 0 14px;
}
.stat-cell {
    background: #fff; border: 2px solid #eadfc9; border-radius: 12px; padding: 10px; text-align: center;
}
.stat-cell small { display: block; font-size: 10px; text-transform: uppercase; color: #8a7a5f; letter-spacing: 0.03em; }
.stat-cell b { display: block; font-size: 17px; color: var(--earth); font-variant-numeric: tabular-nums; margin-top: 2px; }
.diff-tag {
    display: inline-block; margin-bottom: 12px; font-size: 12px; font-weight: 800;
    padding: 4px 12px; border-radius: 999px; color: #fff;
}
.diff-tag.easy { background: var(--palm); }
.diff-tag.normal { background: var(--gold); color: #422006; }
.diff-tag.hard { background: #dc2626; }

/* ----------------------------- Victory confetti --------------------- */
.victory { position: relative; overflow: hidden; }
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.confetti i {
    position: absolute; top: -12px; width: 8px; height: 14px; border-radius: 2px;
    background: var(--gold); opacity: 0.9; animation: confetti-fall 2.6s linear infinite;
}
.confetti i:nth-child(3n) { background: var(--palm); }
.confetti i:nth-child(3n + 1) { background: var(--terra); }
@keyframes confetti-fall {
    0% { transform: translateY(-10px) rotate(0deg); }
    100% { transform: translateY(360px) rotate(320deg); }
}

@media (max-width: 560px) {
    .diff-grid { grid-template-columns: 1fr; }
    .stat-grid { grid-template-columns: repeat(2, 1fr); }
}