:root {
    --bg-dark: #0d0211;
    --panel-bg: #1a0a24;
    --accent: #ffd700;
    --text: #e0d0e0;
    --border: #4a2a5a;
    --highlight: #8a4af3;
    --rarity-common: #999;
    --rarity-uncommon: #2ecc71;
    --rarity-rare: #3498db;
    --rarity-legendary: #f1c40f;
    --rarity-mythic: #a335ee;
    --hp-bar: #2ecc71;
    --hp-yellow: #f1c40f;
    --hp-red: #e74c3c;
    --font-main: "Courier New", Courier, monospace;
}

* { box-sizing: border-box; touch-action: manipulation; }

body {
    margin: 0;
    background-color: var(--bg-dark);
    color: var(--text);
    font-family: var(--font-main);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 100vh;
    image-rendering: pixelated;
}

#game-container {
    display: grid;
    grid-template-columns: 240px 1fr 300px;
    grid-template-rows: 1fr;
    height: 100vh;
    gap: 4px;
    padding: 4px;
}

.sidebar {
    background: var(--panel-bg);
    border: 2px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 10px;
    overflow-y: auto;
}

.section-title {
    color: var(--accent);
    border-bottom: 1px solid var(--border);
    margin-bottom: 8px;
    padding-bottom: 4px;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 0.9em;
}

.run-title, .equipment-title, .legacy-title { margin-top: 15px; }

#stats-panel .stat-row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 4px;
    font-size: 0.85em;
}

.hp-container {
    width: 100%;
    height: 12px;
    background: #300;
    border: 1px solid #600;
    margin: 8px 0;
    position: relative;
}

#hp-fill {
    height: 100%;
    width: 100%;
    background: var(--hp-bar);
    transition: width 0.3s, background 0.3s;
}

.xp-container {
    width: 100%;
    height: 6px;
    background: #222;
    margin-bottom: 10px;
}

#xp-fill { height: 100%; width: 0%; background: #3498db; }

.equipment-slot {
    background: #251333;
    border: 1px solid var(--border);
    padding: 6px;
    margin-bottom: 6px;
    font-size: 0.8em;
    cursor: pointer;
    position: relative;
}

.equipment-slot:hover { border-color: var(--accent); }
.slot-label { font-size: 0.7em; color: #888; display: block; }

#inventory-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    margin-bottom: 10px;
}

.inv-slot {
    aspect-ratio: 1;
    background: #251333;
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5em;
    cursor: pointer;
    position: relative;
    min-width: 0;
}

.inv-slot:hover { border-color: var(--accent); }
.inv-slot.empty { cursor: default; }

.choice-number {
    min-width: 18px;
    height: 18px;
    border: 1px solid var(--border);
    color: var(--accent);
    background: rgba(0, 0, 0, 0.25);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    line-height: 1;
    flex: 0 0 auto;
}

.inv-number {
    position: absolute;
    top: 2px;
    left: 2px;
    min-width: 15px;
    height: 15px;
    font-size: 0.58rem;
}

.enchant-indicator {
    position: absolute;
    top: 2px;
    right: 2px;
    font-size: 10px;
    text-shadow: 0 0 3px var(--highlight);
}

.keyboard-selected {
    border-color: var(--accent) !important;
    box-shadow: 0 0 0 2px rgba(247, 197, 102, 0.25);
}

.item-count {
    position: absolute;
    bottom: 1px;
    right: 2px;
    font-size: 10px;
    color: white;
    text-shadow: 1px 1px black;
}

#message-log {
    flex-grow: 1;
    min-height: 0;
    font-size: 0.75em;
    overflow-y: auto;
    border-top: 1px solid var(--border);
    padding-top: 8px;
    line-height: 1.4;
    overscroll-behavior: contain;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#message-log::-webkit-scrollbar {
    display: none;
}

.log-entry { margin-bottom: 4px; border-left: 2px solid var(--border); padding-left: 5px; }
.log-atk { color: #ffaa00; }
.log-def { color: #ff5555; }
.log-get { color: #55ff55; }
.log-info { color: #aaaaff; }

#canvas-wrapper {
    position: relative;
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--border);
    overflow: hidden;
}

canvas {
    display: block;
    image-rendering: pixelated;
    max-width: 100%;
    max-height: 100%;
}

#run-strip {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 12px;
    align-items: center;
    padding: 6px 10px;
    background: rgba(26, 10, 36, 0.86);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 0.78em;
    z-index: 20;
    pointer-events: none;
    white-space: nowrap;
}

#strip-boss {
    color: var(--hp-red);
    font-weight: bold;
    text-transform: uppercase;
}

.overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.85);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 20px;
    text-align: center;
}

.hidden { display: none !important; }

.dialog {
    background: var(--panel-bg);
    border: 2px solid var(--accent);
    padding: 20px;
    max-width: 480px;
}

#title-screen h1 {
    color: var(--accent);
    font-size: 2.5em;
    margin-bottom: 0;
}

#title-screen h3 {
    color: var(--border);
    margin-top: 0;
}

#title-screen p {
    max-width: 400px;
    font-size: 0.9em;
    color: #aaa;
}

.hint {
    font-size: 0.7em;
    margin-top: 20px;
}

.btn {
    background: var(--border);
    color: white;
    border: 2px solid var(--accent);
    padding: 10px 20px;
    font-family: inherit;
    cursor: pointer;
    margin: 10px;
    font-size: 1em;
}

.btn:hover { background: var(--highlight); }
.btn.secondary { border-color: var(--border); }
.btn.danger { border-color: var(--hp-red); color: #ffd7d7; }
.btn.danger:hover { background: #5a202b; }
.compact-btn {
    width: 100%;
    margin: 6px 0 0;
    padding: 7px 10px;
    font-size: 0.82em;
}

#shop-ui {
    width: 80%;
    max-width: 600px;
    background: var(--panel-bg);
    border: 4px solid var(--accent);
    padding: 20px;
}

.shop-tabs { display: flex; margin-bottom: 15px; }
.tab-btn { flex: 1; padding: 10px; background: #251333; border: 1px solid var(--border); cursor: pointer; color: var(--text); }
.tab-btn.active { background: var(--border); border-color: var(--accent); }

.shop-items {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    max-height: 300px;
    overflow-y: auto;
    padding: 5px;
}

.shop-item {
    background: #251333;
    border: 1px solid var(--border);
    padding: 8px;
    display: flex;
    align-items: center;
    cursor: pointer;
    font-size: 0.8em;
    gap: 8px;
}

.shop-item:hover { border-color: var(--accent); }
.shop-item-icon { font-size: 1.5em; }
.shop-item-info { flex-grow: 1; text-align: left; }
.shop-item-price, #shop-gold-val { color: var(--accent); }

.shop-footer {
    margin-top: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

#curse-meter { font-size: 0.8em; }

.merchant-dialogue {
    font-style: italic;
    color: var(--accent);
    margin-bottom: 15px;
    min-height: 40px;
}

#death-stats p { margin: 7px 0; }
.death-title { color: var(--hp-red); }
.new-best { color: var(--accent); font-weight: bold; }
.legacy-earned { color: var(--rarity-uncommon); font-weight: bold; }

.legacy-summary {
    color: #b8a7c8;
    font-size: 0.95em;
    margin: 14px 0;
}

#legacy-overlay {
    padding: 12px;
}

.legacy-dialog {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.legacy-dialog h2 {
    margin-top: 0;
    color: var(--accent);
}

.legacy-stats {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 8px;
    margin: 15px 0;
    font-size: 0.88em;
}

.legacy-stats span {
    background: #251333;
    border: 1px solid var(--border);
    padding: 10px;
}

#legacy-upgrades {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    margin-top: 15px;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
    padding-right: 4px;
}

.legacy-upgrade {
    display: grid;
    grid-template-columns: 24px 40px 1fr auto;
    grid-template-rows: auto auto;
    gap: 4px 10px;
    align-items: center;
    background: #251333;
    color: var(--text);
    border: 1px solid var(--border);
    padding: 12px;
    text-align: left;
    font-family: inherit;
    font-size: 1.02em;
    cursor: pointer;
}

.legacy-upgrade:hover:not(:disabled) { border-color: var(--accent); background: #311842; }
.legacy-upgrade:disabled { opacity: 0.55; cursor: default; }
.legacy-number { grid-row: 1 / 3; align-self: center; font-size: 1.14em; }
.legacy-icon { grid-column: 2; grid-row: 1 / 3; font-size: 1.6em; text-align: center; color: var(--accent); }
.legacy-copy { min-width: 0; }
.legacy-copy b { display: block; color: white; font-size: 1.08em; }
.legacy-copy small { display: block; color: #b8a7c8; font-size: 0.92em; line-height: 1.4; margin-top: 3px; }
.legacy-rank { color: var(--accent); font-size: 0.94em; white-space: nowrap; }
.legacy-cost { grid-column: 3 / 5; color: var(--rarity-uncommon); font-size: 0.92em; }

.legacy-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 12px;
}

.dialogue-dialog {
    width: min(560px, 92vw);
    text-align: left;
}

#dialogue-kicker {
    color: #b8a7c8;
    font-size: 0.74em;
    text-transform: uppercase;
    margin-bottom: 6px;
}

#dialogue-title {
    margin: 0 0 12px;
    font-size: 1.45em;
}

#dialogue-lines {
    color: var(--text);
    line-height: 1.45;
    font-size: 0.9em;
}

#dialogue-lines p {
    margin: 0 0 10px;
}

.discard-dialog {
    max-width: 520px;
}

#discard-choices {
    display: grid;
    gap: 8px;
}

.discard-choice {
    text-align: left;
}

#tooltip {
    position: fixed;
    background: #2a1a3a;
    border: 1px solid var(--accent);
    padding: 8px;
    pointer-events: none;
    z-index: 1000;
    font-size: 0.75em;
    max-width: 220px;
    box-shadow: 5px 5px 15px rgba(0,0,0,0.5);
}

#mobile-controls {
    display: none;
    position: absolute;
    bottom: 20px;
    right: 20px;
    grid-template-columns: repeat(3, 60px);
    grid-template-rows: repeat(2, 60px);
    gap: 5px;
}

.ctrl-btn {
    background: rgba(74, 42, 90, 0.6);
    border: 2px solid var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    font-weight: bold;
    user-select: none;
}

.help-dialog {
    text-align: left;
}

.close-hint {
    text-align: center;
    color: var(--accent);
}

@media (max-width: 900px) {
    #game-container {
        grid-template-columns: 180px 1fr;
        grid-template-rows: 1fr 200px;
    }
    #inventory-panel {
        grid-column: 1 / 3;
        flex-direction: row;
    }
    #message-log {
        height: 100%;
        border-top: none;
        border-left: 1px solid var(--border);
        margin-left: 10px;
    }
    #run-strip { max-width: calc(100% - 16px); overflow: hidden; }
    .legacy-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    #legacy-upgrades { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
    #game-container { display: block; }
    .sidebar { display: none; }
    #canvas-wrapper { height: 100vh; }
    #mobile-controls { display: grid; }
    #run-strip {
        top: 6px;
        gap: 8px;
        font-size: 0.68em;
    }
}

@keyframes float-up {
    0% { transform: translateY(0); opacity: 1; }
    100% { transform: translateY(-40px); opacity: 0; }
}
.damage-num {
    position: absolute;
    color: var(--hp-red);
    font-weight: bold;
    pointer-events: none;
    animation: float-up 0.8s ease-out forwards;
    text-shadow: 1px 1px 0 #000;
    z-index: 1000;
}

.damage-num.crit {
    color: #ffcc00;
    font-size: 1.35em;
    text-shadow: 2px 2px 0 #000, 0 0 8px rgba(255, 204, 0, 0.6);
}

@keyframes shake {
    0%, 100% { transform: translate(0,0); }
    25% { transform: translate(-5px, 5px); }
    50% { transform: translate(5px, -5px); }
    75% { transform: translate(-5px, -5px); }
}

.shake { animation: shake 0.2s linear 2; }
