:root {
    --bg-color: #f9f9f9;
    --surface-color: #ffffff;
    --primary-gold: #835100;
    --gold-highlight: #c17f24;
    --gold-tint: #fdf3e3;
    --on-surface: #1a1c1c;
    --muted-text: #656464;
    --border-color: #e2e2e2;
    --error-color: #ba1a1a;
    
    --board-light: #b0b0b0;
    --board-dark: #575757;
    
    --font-serif: 'DM Serif Display', serif;
    --font-mono: 'DM Mono', monospace;
    --font-body: 'Inter', sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    background-color: var(--bg-color);
    color: var(--on-surface);
    font-family: var(--font-body);
    overflow: hidden;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

#app { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; }

.screen { width: 100%; height: 100%; display: flex; transition: opacity 0.3s ease; }

.hidden { display: none !important; }

/* ── HOME SCREEN ──────────────────────────────────────────────── */
#mode-selection {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: var(--bg-color);
    overflow-y: auto;
}

.home-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2rem;
    padding: 40px 20px;
    width: 100%;
    max-width: 400px;
}

/* Branding */
.home-brand { text-align: center; }

.logo {
    font-family: var(--font-serif);
    font-size: 3.6rem;
    margin-bottom: 0.4rem;
    line-height: 1;
}

.tagline {
    font-family: var(--font-mono);
    font-size: 9px;
    letter-spacing: 0.25rem;
    text-transform: uppercase;
    color: var(--muted-text);
}

/* Settings card */
.home-card {
    width: 100%;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    padding: 28px 24px 24px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.home-setting {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.home-label {
    font-family: var(--font-mono);
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.1rem;
    color: var(--muted-text);
    display: flex;
    align-items: center;
    gap: 6px;
}

/* Segmented toggle groups */
.seg-group {
    display: flex;
    border: 1px solid var(--border-color);
}

.seg-btn {
    flex: 1;
    padding: 10px 8px;
    border: none;
    border-right: 1px solid var(--border-color);
    background: var(--surface-color);
    cursor: pointer;
    font-family: var(--font-mono);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.05rem;
    color: var(--muted-text);
    transition: all 0.15s ease;
}

.seg-btn:last-child { border-right: none; }
.seg-btn.active { background: var(--on-surface); color: #fff; }
.seg-btn:not(.active):hover { background: #f0f0f0; color: var(--on-surface); }

/* Info button (ⓘ) */
.info-btn {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 1.5px solid var(--muted-text);
    background: transparent;
    cursor: pointer;
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 700;
    color: var(--muted-text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    padding: 0;
    transition: all 0.15s;
}
.info-btn:hover { background: var(--on-surface); color: #fff; border-color: var(--on-surface); }

/* CTA buttons */
.home-ctas {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 4px;
}
.home-btn { width: 100%; padding: 14px; font-size: 11px; letter-spacing: 0.12rem; }
.rules-link-btn {
    width: 100%;
    padding: 12px;
    background: transparent;
    border: 1.5px dashed var(--surface-alt, #444);
    border-radius: 8px;
    color: var(--on-surface);
    font-size: 11px;
    letter-spacing: 0.08rem;
    cursor: pointer;
    transition: background 0.18s, border-color 0.18s;
    font-family: inherit;
    text-transform: uppercase;
}
.rules-link-btn:hover { background: rgba(255,255,255,0.06); border-color: var(--on-surface); }

/* ── SWAP RULES MODAL ─────────────────────────────────────────── */
.swap-modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    justify-content: center;
    align-items: flex-end;
    z-index: 300;
    backdrop-filter: blur(4px);
    animation: overlay-fade-in 0.25s ease;
}

@media (min-height: 600px) {
    .swap-modal { align-items: center; }
}

.swap-modal-card {
    background: var(--surface-color);
    width: 100%;
    max-width: 520px;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    border-radius: 16px 16px 0 0;
    overflow: hidden;
    animation: card-slide-up 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}

@media (min-height: 600px) {
    .swap-modal-card { border-radius: 12px; max-height: 85vh; }
}

.swap-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px 16px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.swap-modal-title {
    font-family: var(--font-serif);
    font-size: 1.5rem;
    margin: 0;
}

.modal-close-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: transparent;
    cursor: pointer;
    font-size: 14px;
    color: var(--muted-text);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    flex-shrink: 0;
}
.modal-close-btn:hover { background: #f0f0f0; color: var(--on-surface); }

.swap-modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 28px;
}

.swap-modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}
.swap-modal-footer .primary-btn { width: 100%; padding: 14px; font-size: 11px; }

/* Rule blocks */
.rule-block {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.rule-heading {
    display: flex;
    align-items: center;
    gap: 10px;
}

.rule-num {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--on-surface);
    color: #fff;
    font-family: var(--font-mono);
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.rule-heading h3 {
    font-family: var(--font-body);
    font-size: 14px;
    font-weight: 600;
    margin: 0;
    color: var(--on-surface);
}

.rule-desc {
    font-family: var(--font-body);
    font-size: 13px;
    color: var(--muted-text);
    line-height: 1.6;
    margin: 0;
}

.rule-caption {
    font-family: var(--font-mono);
    font-size: 10px;
    color: var(--muted-text);
    background: #f5f5f5;
    padding: 8px 12px;
    border-left: 3px solid var(--border-color);
    margin: 0;
    line-height: 1.5;
}

/* Swap scenario visuals */
.scenario-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: #f9f9f9;
    border: 1px solid var(--border-color);
    padding: 16px 12px;
    flex-wrap: wrap;
}

.scenario-piece {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.scenario-piece img { width: 44px; height: 44px; }

.scenario-arrow {
    font-size: 20px;
    color: var(--primary-gold);
    font-weight: 700;
}

.scenario-spacer {
    font-family: var(--font-mono);
    font-size: 9px;
    color: var(--muted-text);
    text-transform: uppercase;
    padding: 0 4px;
}

.piece-name {
    font-family: var(--font-mono);
    font-size: 9px;
    text-transform: uppercase;
    color: var(--muted-text);
    text-align: center;
}

/* Swap pairs grid */
.swap-pairs-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.swap-pair-card {
    background: #f9f9f9;
    border: 1px solid var(--border-color);
    padding: 12px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.swap-pair-pieces {
    display: flex;
    align-items: center;
    gap: 4px;
}

.swap-pair-pieces img { width: 28px; height: 28px; }

.swap-dbl-arrow {
    font-size: 14px;
    color: var(--primary-gold);
}

.swap-pair-label {
    font-family: var(--font-mono);
    font-size: 8px;
    text-transform: uppercase;
    color: var(--muted-text);
    text-align: center;
    line-height: 1.4;
}

/* Protected pieces */
.protected-row {
    display: flex;
    gap: 12px;
    justify-content: center;
    background: #f9f9f9;
    border: 1px solid var(--border-color);
    padding: 16px;
}

.protected-piece {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex: 1;
}

.protected-piece img { width: 40px; height: 40px; }

.protected-badge {
    font-family: var(--font-mono);
    font-size: 8px;
    text-transform: uppercase;
    background: #d4edda;
    color: #276535;
    padding: 2px 6px;
    border-radius: 3px;
    border: 1px solid #b8dfc1;
}

/* Badge demo */
.badge-demo-row {
    display: flex;
    gap: 12px;
    justify-content: center;
    background: var(--board-dark);
    padding: 20px 12px;
    border-radius: 4px;
}

.badge-demo-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    flex: 1;
}

.badge-demo-piece {
    position: relative;
    width: 48px;
    height: 48px;
}

.badge-demo-piece img {
    width: 100%;
    height: 100%;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.4));
}

.demo-badge {
    position: absolute;
    bottom: -2px;
    right: -4px;
    background: rgba(255,255,255,0.95);
    color: #333;
    font-family: var(--font-mono);
    font-size: 9px;
    font-weight: 700;
    padding: 1px 4px;
    border-radius: 2px;
    border: 1px solid #999;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    line-height: 1.2;
}

.badge-demo-item .piece-name {
    color: #ddd;
    font-size: 9px;
}

/* Mode comparison */
.mode-compare {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.mode-col {
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: #f9f9f9;
    border: 1px solid var(--border-color);
    padding: 12px;
}

.mode-col-header {
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1rem;
    padding: 4px 8px;
    text-align: center;
    border-radius: 3px;
}

.mode-col-header.shared { background: #e8f4fd; color: #1a5f8a; }
.mode-col-header.separate { background: #fdf3e3; color: var(--primary-gold); }

.mode-col p {
    font-family: var(--font-body);
    font-size: 11px;
    color: var(--muted-text);
    line-height: 1.5;
    margin: 0;
}

.mode-visual {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 4px;
}

.mode-visual-row {
    display: flex;
    align-items: center;
    gap: 4px;
    background: #fff;
    border: 1px solid var(--border-color);
    padding: 6px 8px;
}

.mode-visual-row img { width: 22px; height: 22px; }
.mode-visual-row span { font-size: 12px; color: var(--muted-text); }

.mode-tag {
    font-family: var(--font-mono);
    font-size: 8px;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 2px;
    margin-left: auto;
    text-transform: uppercase;
}
.white-tag { background: #eee; color: #444; }
.black-tag { background: #333; color: #fff; }

.mode-same-label {
    font-family: var(--font-mono);
    font-size: 8px;
    color: var(--muted-text);
    text-align: center;
    font-style: italic;
}

/* Game Interface Layout */
#game-view { 
    display: grid; 
    grid-template-columns: 320px 1fr 320px; 
    grid-template-rows: 1fr 60px; 
    width: 100vw; 
    height: 100vh; 
    padding: 40px; 
    gap: 40px; 
    transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.panel { background: var(--surface-color); display: flex; flex-direction: column; box-shadow: 0 0 20px rgba(0,0,0,0.02); }

.mobile-header { display: none; }

.side-panel { padding: 25px; }

/* Left Panel: Swap Station */
.station-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; }


.swap-row { padding: 15px; margin-bottom: 10px; border-left: 0px solid transparent; transition: all 0.4s ease; }

.swap-row.active { border-left: 3px solid var(--gold-highlight); background: var(--gold-tint); }

.swap-row.anticipate { animation: anticipate-pulse 1s infinite alternate; }

@keyframes anticipate-pulse {
    from { border-left-width: 3px; background: var(--gold-tint); }
    to { border-left-width: 6px; background: #fae8ca; }
}

.label { font-family: var(--font-mono); font-size: 9px; text-transform: uppercase; letter-spacing: 0.1rem; color: var(--muted-text); margin-bottom: 12px; }

.swap-display { font-family: var(--font-serif); font-size: 28px; margin-bottom: 5px; }

.swap-display-small { font-family: var(--font-serif); font-size: 24px; }

.swap-display-tiny { font-family: var(--font-serif); font-size: 16px; opacity: 0.5; }

.swap-preview { font-family: var(--font-serif); font-size: 20px; opacity: 0.5; }

.swap-desc, .swap-desc-muted, .swap-desc-tiny { font-family: var(--font-mono); font-size: 9px; color: var(--muted-text); line-height: 1.4; display: flex; align-items: center; gap: 6px; }

.legend-badge { background: #eee; padding: 1px 4px; border-radius: 2px; font-size: 8px; color: #333; }

.note { font-family: var(--font-body); font-size: 8px; font-style: italic; color: var(--muted-text); margin-top: 5px; }

.diff-cols { display: flex; gap: 20px; }

.transformed-legend { margin-top: auto; padding-top: 20px; border-top: 1px solid var(--border-color); }

/* Center Panel: Board */
#board-container { display: grid; grid-template-columns: 30px 1fr 30px; grid-template-rows: 30px 1fr 30px; justify-self: center; align-self: center; width: 600px; height: 600px; }

.board-labels { display: flex; font-family: var(--font-mono); font-size: 11px; color: var(--muted-text); }
.board-labels.files { justify-content: space-around; align-items: center; }
.board-labels.ranks { flex-direction: column; justify-content: space-around; align-items: center; }

#board-labels-top { grid-row: 1; grid-column: 2; }
#board-labels-bottom { grid-row: 3; grid-column: 2; }
#board-labels-left { grid-row: 2; grid-column: 1; }
#board-labels-right { grid-row: 2; grid-column: 3; }

#chess-board { grid-column: 2; grid-row: 2; display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); width: 540px; height: 540px; border: 1px solid var(--border-color); }

.square { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; position: relative; cursor: pointer; }

.square.light { background-color: var(--board-light); }

.square.dark { background-color: var(--board-dark); }

.square.selected { background-color: rgba(44, 44, 44, 0.12) !important; }

.square.last-move { background-color: rgba(193, 127, 36, 0.15) !important; }

.square.check { animation: check-pulse 1s infinite alternate; }

@keyframes check-pulse {
    from { background-color: rgba(204, 51, 51, 0.1); }
    to { background-color: rgba(204, 51, 51, 0.3); }
}

.move-hint { width: 8px; height: 8px; border-radius: 50%; background-color: rgba(44, 44, 44, 0.2); }

.capture-hint { width: 80%; height: 80%; border-radius: 50%; border: 2px solid rgba(44, 44, 44, 0.2); }

/* Pieces */
.piece {
    width: 90%;
    height: 90%;
    user-select: none;
    z-index: 2;
    transition: transform 0.15s ease;
    display: flex;
    justify-content: center;
    align-items: center;
}

.piece img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
    display: block;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.35));
}

.badge { 
    position: absolute; 
    bottom: 2px; 
    right: 2px; 
    background: rgba(255, 255, 255, 0.9); 
    color: #333;
    padding: 0px 3px; 
    border-radius: 2px; 
    font-size: 8px; 
    font-weight: bold;
    font-family: var(--font-mono); 
    box-shadow: 0 1px 2px rgba(0,0,0,0.3); 
    z-index: 3; 
    border: 1px solid #999;
}

/* Right Panel: Battle Log */
.log-header { margin-bottom: 20px; }

.turn-indicator { display: flex; align-items: center; gap: 10px; margin-bottom: 5px; }

.serif-text { font-family: var(--font-serif); font-size: 16px; }

.dot { width: 10px; height: 10px; border-radius: 50%; border: 1px solid #ccc; }

.dot.white { background: #fff; }

.dot.black { background: #000; }

.game-meta { font-family: var(--font-mono); font-size: 9px; color: var(--muted-text); }

.log-container { flex: 1; min-height: 200px; max-height: 350px; overflow-y: auto; display: flex; flex-direction: column; gap: 5px; border: 1px solid var(--border-color); padding: 10px; margin: 10px 0; }

.log-entry { font-family: var(--font-mono); font-size: 11px; padding: 5px 0; animation: fade-in 0.3s ease forwards; opacity: 0; }

@keyframes fade-in { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.log-entry.swap-event { background: rgba(193, 127, 36, 0.05); color: var(--primary-gold); font-weight: 500; text-align: center; }

.captured-section { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--border-color); padding-top: 15px; margin-top: auto; }

.captured-row { font-family: var(--font-mono); font-size: 14px; min-height: 20px; }

.material-diff { font-family: var(--font-mono); font-size: 9px; color: var(--muted-text); }

/* Buttons & Overlays */
.primary-btn, .secondary-btn, .tertiary-btn {
    padding: 12px 24px;
    border: none;
    cursor: pointer;
    font-family: var(--font-mono);
    font-size: 10px;
    letter-spacing: 0.1rem;
    text-transform: uppercase;
    transition: all 0.2s ease;
}

.primary-btn { background: var(--on-surface); color: white; }
.primary-btn:hover { background: #333; }

.secondary-btn { background: transparent; border: 1px solid var(--border-color); color: var(--on-surface); }
.secondary-btn:hover { background: #eee; }

.tertiary-btn { background: transparent; border: 1px solid var(--border-color); color: var(--muted-text); font-size: 9px; padding: 8px 16px; }
.tertiary-btn:hover { color: var(--on-surface); background: #eee; }

#game-controls { grid-row: 2; grid-column: 1 / span 3; display: flex; justify-content: center; gap: 20px; align-items: center; border-top: 1px solid var(--border-color); padding-top: 10px; }

/* Shared overlay base (promotion modal reuses this) */
.overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.55);
    display: flex; justify-content: center; align-items: center;
    z-index: 100; backdrop-filter: blur(6px);
}

.overlay-content { text-align: center; max-width: 400px; background: #fff; padding: 40px; }
.overlay-content h1, .overlay-content h3 { font-family: var(--font-serif); font-size: 2.5rem; margin-bottom: 1rem; }

.promotion-options { display: flex; gap: 20px; justify-content: center; margin-top: 20px; }
.promotion-options button { font-size: 32px; padding: 10px 20px; border: 1px solid var(--border-color); background: white; cursor: pointer; }
.promotion-options button:hover { background: #eee; }

/* ── GAME OVER SCREEN ─────────────────────────────────────────── */
.result-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.65);
    display: flex; justify-content: center; align-items: center;
    z-index: 200;
    backdrop-filter: blur(8px);
    animation: overlay-fade-in 0.4s ease forwards;
}

@keyframes overlay-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.result-card {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    padding: 48px 52px 44px;
    text-align: center;
    max-width: 380px;
    width: 90%;
    box-shadow: 0 24px 64px rgba(0,0,0,0.35);
    animation: card-slide-up 0.4s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@keyframes card-slide-up {
    from { opacity: 0; transform: translateY(28px) scale(0.97); }
    to   { opacity: 1; transform: translateY(0)   scale(1);    }
}

.result-piece-wrap {
    width: 72px;
    height: 72px;
    margin: 0 auto 20px;
    border-radius: 50%;
    background: #f0f0f0;
    border: 2px solid var(--border-color);
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.result-piece-img {
    width: 80%;
    height: 80%;
    object-fit: contain;
    display: block;
}

.result-status {
    font-family: var(--font-mono);
    font-size: 10px;
    letter-spacing: 0.25rem;
    text-transform: uppercase;
    color: var(--muted-text);
    margin-bottom: 10px;
}

.result-headline {
    font-family: var(--font-serif);
    font-size: 2.6rem;
    color: var(--on-surface);
    line-height: 1.1;
    margin-bottom: 8px;
}

.result-detail {
    font-family: var(--font-mono);
    font-size: 10px;
    color: var(--muted-text);
    letter-spacing: 0.1rem;
    margin-bottom: 32px;
    min-height: 16px;
}

.result-buttons {
    display: flex;
    gap: 12px;
    justify-content: center;
    flex-wrap: wrap;
}

.result-buttons .primary-btn,
.result-buttons .secondary-btn {
    flex: 1;
    min-width: 120px;
}

.flash-gold { animation: gold-flash 0.5s ease-out; }

@keyframes gold-flash {
    0% { background-color: rgba(193, 127, 36, 0); }
    50% { background-color: rgba(193, 127, 36, 0.4); }
    100% { background-color: rgba(193, 127, 36, 0); }
}

.animating { pointer-events: none; }

/* ── MOBILE OPTIMIZATION ──────────────────────────────────────── */

@media (max-width: 1024px) {
    body, html {
        overflow-y: auto !important;
        overflow-x: hidden;
        height: auto !important;
        min-height: 100%;
        position: relative;
    }

    #app {
        height: auto !important;
        min-height: 100dvh;
        display: block; /* Allow natural flow */
    }

    .screen {
        height: auto !important;
        min-height: 100dvh;
    }

    #game-view {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto auto auto;
        padding: 5px;
        gap: 8px;
        overflow: visible; /* Let the body handle scrolling */
        height: auto;
    }

    /* Move Turn Indicator to Top */
    .mobile-header {
        display: flex;
        grid-row: 1;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding: 5px 15px;
        margin-bottom: 0;
        box-shadow: none;
        border-bottom: 1px solid var(--border-color);
        min-height: 40px;
    }


    .log-header {
        display: none; /* Hide original log header on mobile */
    }

    .turn-indicator {
        justify-content: center;
        width: 100%;
        margin-bottom: 5px;
    }

    #swap-station {
        grid-row: 2;
        padding: 5px 10px;
        box-shadow: none;
        border-bottom: 1px solid var(--border-color);
    }

    #swap-layouts {
        display: flex;
        gap: 10px;
    }

    .swap-layout {
        width: 100%;
        display: flex;
        gap: 10px;
    }

    /* Different Mode: Side-by-side swaps on mobile */
    #layout-different {
        flex-direction: row;
    }

    /* Same Mode: Horizontal on mobile */
    #layout-same {
        flex-direction: row;
    }

    #layout-same .swap-row {
        margin-bottom: 0;
        flex: 1;
        padding: 5px;
    }

    #layout-different .swap-row, #layout-same .swap-row {
        margin-bottom: 0;
        flex: 1;
        padding: 3px 8px;
    }

    .swap-display, .swap-display-small {
        font-size: 18px;
    }
    .swap-preview { font-size: 14px; }
    .label { font-size: 7px; margin-bottom: 4px; }
    .note { display: none; } /* Hide note on mobile to save space */

    .diff-cols {
        gap: 10px;
        justify-content: space-between;
    }

    #board-container {
        grid-row: 3;
        width: 82vmin;
        height: 82vmin;
        aspect-ratio: 1 / 1;
        max-width: 400px;
        max-height: 400px;
        margin: 0 auto;
        padding: 0;
        position: relative;
        display: block;
    }

    #chess-board {
        width: 100%;
        height: 100%;
        display: grid;
        grid-template-columns: repeat(8, 1fr);
        grid-template-rows: repeat(8, 1fr);
        /* Force perfect squares */
        grid-auto-rows: 1fr;
    }

    .square {
        aspect-ratio: 1 / 1;
        width: 100%;
        height: 100%;
    }

    .board-labels {
        position: absolute;
        pointer-events: none;
        z-index: 10;
        font-weight: bold;
        text-shadow: 0 0 2px rgba(255,255,255,0.8);
    }

    #board-labels-top, #board-labels-right {
        display: none !important;
    }

    #board-labels-left {
        left: 2px;
        top: 0;
        height: 100%;
        width: auto;
        flex-direction: column;
        justify-content: space-around;
    }

    #board-labels-left div {
        height: 12.5%;
        display: flex;
        align-items: center;
        font-size: 8px;
    }

    #board-labels-bottom {
        bottom: 2px;
        left: 0;
        width: 100%;
        height: auto;
        flex-direction: row;
        justify-content: space-around;
    }

    #board-labels-bottom div {
        width: 12.5%;
        display: flex;
        justify-content: flex-end;
        padding-right: 2px;
        font-size: 8px;
    }

    #game-controls {
        grid-row: 4;
        padding: 15px 10px 40px 10px; /* Extra bottom padding for mobile browsers */
        position: relative;
        background: var(--bg-color);
        z-index: 10;
        width: 100%;
        display: flex;
        justify-content: center;
        gap: 15px;
    }

    .tertiary-btn {
        padding: 10px 20px;
        font-size: 10px;
        min-width: 100px;
    }

    /* Drawer Overlay */
    .drawer-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 14;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s ease;
    }

    #battle-log.open + #drawer-overlay {
        opacity: 1;
        pointer-events: auto;
    }
    
    #battle-log {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 60vh;
        z-index: 15; /* Higher than overlay */
        transform: translateY(100%);
        transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        box-shadow: 0 -10px 30px rgba(0,0,0,0.1);
    }

    #battle-log.open {
        transform: translateY(0);
    }

    .hidden-mobile {
        display: none !important;
    }

    .toggle-icon {
        font-weight: bold;
        transition: transform 0.3s ease;
    }

    #battle-log.open ~ #game-controls #btn-history-toggle {
        transform: rotate(-90deg);
    }

    /* Adjust station header for mobile */
    .station-header {
        display: none;
    }

    .transformed-legend {
        display: none; /* Hide on mobile to save space, or move into history? */
    }
}

@media (max-width: 480px) {
    .logo { font-size: 2.5rem; }
    #board-container { width: 98vw; height: 98vw; }
    .board-labels { font-size: 9px; }
    .piece img { width: 100%; height: 100%; }
}
