/* ============================================================
   ROOT VARIABLES
   ============================================================ */
:root {
    --ocean-deep: #060d14;
    --ocean-mid: #0a1820;
    --ocean-teal: #0c2230;

    --pane-bg: #060d14;
    --pane-header-bg: #0a1820;
    --pane-border: #1a4a5a;
    --pane-border-dim: #1a3848;

    --gold: #38d878;
    --gold-bright: #58f098;
    --gold-dim: #228850;
    --cyan: #40d8d0;
    --red: #d84040;

    --text-primary: #c8d8d4;
    --text-secondary: #8aa8a4;
    --text-dim: #5a7878;
    --text-faint: #3a5858;
    --text-muted: #4a6868;

    --font-pixel: 'Rajdhani', sans-serif;
    --font-system: 'VT323', monospace;
    --font-mono: 'Space Mono', monospace;
    --font-serif: 'Chakra Petch', sans-serif;
}

/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    background: var(--ocean-deep);
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.5;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

/* Scanline overlay */
body::after {
    content: '';
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 10001;
    background: repeating-linear-gradient(
        0deg,
        transparent, transparent 2px,
        rgba(0,0,0,0.04) 2px, rgba(0,0,0,0.04) 4px
    );
}

/* ============================================================
   SCROLLBAR
   ============================================================ */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--ocean-deep); }
::-webkit-scrollbar-thumb { background: var(--pane-border-dim); border-radius: 0; }
::-webkit-scrollbar-thumb:hover { background: var(--pane-border); }

/* ============================================================
   TERMINAL LAYOUT — full screen
   ============================================================ */
#terminalPane {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    background: var(--pane-bg);
}

/* ============================================================
   FLOATING PANEL LAYER
   ============================================================ */
#panelLayer {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 100;
}

/* ============================================================
   PANE HEADERS (shared)
   ============================================================ */
.pane-header {
    padding: 5px 12px;
    background: var(--pane-header-bg);
    border-bottom: 1px solid var(--pane-border);
    font-family: var(--font-pixel);
    font-weight: 600;
    font-size: 11px;
    letter-spacing: 0.15em;
    color: var(--gold);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

/* ============================================================
   WINDOW PANELS — floating windows
   ============================================================ */
.window-panel {
    position: fixed;
    display: flex;
    flex-direction: column;
    background: var(--pane-bg);
    border: 1px solid var(--pane-border);
    border-radius: 0;
    box-shadow: 0 4px 20px rgba(0,0,0,0.6), 0 0 1px rgba(56,216,120,0.15);
    pointer-events: auto;
    min-width: 200px;
    max-width: 600px;
    overflow: hidden;
}

.window-panel .pane-header {
    cursor: grab;
    position: relative;
    user-select: none;
    flex-shrink: 0;
}
.window-panel .pane-header:active {
    cursor: grabbing;
}

.panel-close {
    background: none;
    border: none;
    color: var(--text-dim);
    font-family: var(--font-system);
    font-size: 14px;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}
.panel-close:hover { color: var(--gold-bright); }

/* Collapsed state */
.panel-collapsed .panel-body { display: none; }

.panel-body {
    padding: 8px;
    overflow-y: auto;
    overflow-x: hidden;
}

/* Panel size presets */
.panel-sm .panel-body { max-height: 180px; }
.panel-md .panel-body { max-height: 320px; }
.panel-lg .panel-body { max-height: 480px; }

/* ============================================================
   TERMINAL LOG
   ============================================================ */
#terminalLog {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 10px 14px;
    font-size: 13px;
    line-height: 1.55;
}

.log-line {
    white-space: pre-wrap;
    word-break: break-word;
    min-height: 20px;
}

/* ============================================================
   TERMINAL INPUT
   ============================================================ */
#terminalInput {
    padding: 8px 14px;
    border-top: 1px solid var(--pane-border);
    display: flex;
    align-items: center;
    gap: 8px;
    background: #080e16;
    flex-shrink: 0;
}

#terminalPrompt {
    color: var(--gold);
    font-family: var(--font-system);
    font-size: 14px;
    white-space: nowrap;
    min-width: 42px;
}

#terminalField {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 13px;
    caret-color: var(--gold);
}

/* ============================================================
   CREATURE RENDERING
   ============================================================ */
.creature-grid {
    font-family: var(--font-mono);
    line-height: 1;
    display: inline-block;
}
.creature-row {
    display: flex;
    white-space: pre;
}
.creature-cell {
    display: inline-block;
    text-align: center;
}

/* Scale presets */
.creature-lg .creature-cell { width: 13.2px; height: 16px; font-size: 14px; line-height: 16px; }
.creature-md .creature-cell { width: 9px; height: 11px; font-size: 9.5px; line-height: 11px; }
.creature-sm .creature-cell { width: 5.4px; height: 7px; font-size: 6px; line-height: 7px; }
.creature-xs .creature-cell { width: 3.8px; height: 5px; font-size: 4.2px; line-height: 5px; }

/* Creature card wrapper */
.creature-card {
    position: relative;
    border-radius: 0;
    overflow: hidden;
    display: inline-block;
}
.creature-card-scanline {
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0,0,0,0.06) 2px, rgba(0,0,0,0.06) 4px);
}

/* Stats diamond overlay */
.creature-stats {
    position: absolute;
    top: 6px;
    left: 8px;
    z-index: 2;
    font-family: var(--font-pixel);
    font-weight: 700;
    font-size: 16px;
    line-height: 1.1;
    color: #fff;
    text-shadow: 0 0 4px #000, 0 0 2px #000, 1px 1px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000;
    text-align: center;
}
.creature-stats .stat-lr { display: flex; gap: 12px; justify-content: center; }

/* Name bar — overlays bottom of card */
.creature-namebar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 3px 8px;
    font-family: var(--font-pixel);
    font-weight: 600;
    font-size: 12px;
    letter-spacing: 0.08em;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 4px;
    z-index: 3;
}
.creature-namebar span:first-child {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    min-width: 0;
}

/* ============================================================
   BATTLE GRID
   ============================================================ */
.battle-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
}

.battle-cell {
    aspect-ratio: 1;
    background: rgba(10,26,40,0.5);
    border: 2px solid var(--pane-border-dim);
    border-radius: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
}
.battle-cell-player {
    background: rgba(56,216,120,0.10);
    border-color: rgba(56,216,120,0.50);
    box-shadow: inset 0 0 12px rgba(56,216,120,0.15);
}
.battle-cell-opponent {
    background: rgba(64,216,208,0.10);
    border-color: rgba(64,216,208,0.50);
    box-shadow: inset 0 0 12px rgba(64,216,208,0.15);
}

.battle-cell-coord {
    font-family: var(--font-system);
    font-size: 11px;
    color: var(--text-faint);
}

/* Clickable empty cells during player's turn */
.battle-cell-playable {
    cursor: pointer;
    transition: border-color 0.15s ease, background 0.15s ease;
}
.battle-cell-playable:hover {
    border-color: rgba(56,216,120,0.6);
    background: rgba(56,216,120,0.12);
    box-shadow: inset 0 0 16px rgba(56,216,120,0.15);
}
.battle-cell-playable:hover .battle-cell-coord {
    color: var(--gold);
}

/* Owner label on placed cards */
.battle-cell-owner {
    position: absolute;
    bottom: 2px;
    right: 4px;
    font-family: var(--font-pixel);
    font-size: 8px;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.battle-score {
    display: flex;
    justify-content: space-between;
    margin-bottom: 6px;
    font-family: var(--font-pixel);
    font-weight: 600;
    font-size: 13px;
}

/* ============================================================
   PANEL RESIZE HANDLE
   ============================================================ */
.panel-resize {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 14px;
    height: 14px;
    cursor: nwse-resize;
    opacity: 0.3;
    z-index: 5;
}
.panel-resize:hover { opacity: 0.6; }
.panel-resize::after {
    content: '';
    position: absolute;
    bottom: 3px;
    right: 3px;
    width: 8px;
    height: 8px;
    border-right: 2px solid var(--text-dim);
    border-bottom: 2px solid var(--text-dim);
}

/* ============================================================
   HAND GRID (in panel)
   ============================================================ */
.hand-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(82px, 1fr));
    gap: 4px;
}
.hand-card {
    cursor: default;
}
.hand-card-clickable {
    cursor: pointer;
    transition: transform 0.1s ease;
}
.hand-card-clickable:hover {
    transform: scale(1.03);
}
.hand-card-selected {
    transform: scale(1.05);
}
.hand-card-label {
    text-align: center;
    margin-top: 3px;
    font-family: var(--font-system);
    font-size: 10px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
.hand-card-number {
    display: inline-block;
    font-family: var(--font-pixel);
    font-weight: 700;
    font-size: 11px;
    color: var(--gold);
    min-width: 14px;
}
.hand-card-played {
    height: 60px;
    background: rgba(10,26,40,0.3);
    border: 1px dashed var(--pane-border-dim);
    border-radius: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-system);
    font-size: 10px;
    color: #2a4040;
}

/* ============================================================
   BOOK GRID (in panel)
   ============================================================ */
.book-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
    gap: 6px;
}
.book-card {
    cursor: pointer;
}
.book-card:hover { opacity: 0.85; }
.book-card-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 3px;
    padding: 0 2px;
}
.book-card-name {
    font-family: var(--font-system);
    font-size: 9px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: 70px;
}
.book-card-level {
    font-family: var(--font-system);
    font-size: 8px;
}

/* ============================================================
   RULES PANEL
   ============================================================ */
.rules-content {
    font-family: var(--font-system);
    font-size: 13px;
    line-height: 1.6;
    color: var(--text-secondary);
}
.rules-content .rules-heading {
    color: var(--gold);
    font-family: var(--font-pixel);
    font-weight: 600;
    font-size: 13px;
    letter-spacing: 0.1em;
    margin-top: 12px;
    margin-bottom: 4px;
}
.rules-content .rules-heading:first-child { margin-top: 0; }
.rules-content p { margin-bottom: 6px; }

/* ============================================================
   LOG + LEADERBOARD PANELS
   ============================================================ */
.log-entry {
    padding: 4px 0;
    border-bottom: 1px solid rgba(26,56,72,0.4);
    font-family: var(--font-system);
    font-size: 12px;
    color: var(--text-secondary);
}
.lb-row {
    padding: 4px 0;
    display: flex;
    align-items: center;
    gap: 8px;
    font-family: var(--font-system);
    font-size: 12px;
    border-bottom: 1px solid rgba(26,56,72,0.3);
}
.lb-rank {
    min-width: 20px;
    text-align: center;
    color: var(--text-dim);
    font-weight: 700;
}
.lb-rank-top { color: var(--gold); }
.lb-name { flex: 1; color: var(--text-primary); }
.lb-streak { color: var(--gold); }
.lb-record { color: var(--text-dim); font-size: 11px; min-width: 40px; text-align: right; }

/* ============================================================
   EMPTY / PLACEHOLDER STATES
   ============================================================ */
.panel-empty {
    padding: 30px 16px;
    text-align: center;
    font-family: var(--font-system);
    font-size: 13px;
    color: var(--text-faint);
}

/* ============================================================
   PRISMATIC EFFECT — Legendary (Lv.10) cards
   ============================================================ */
.creature-prismatic {
    position: relative;
    animation: prismatic-shift 4s ease-in-out infinite;
}
.creature-prismatic::before {
    content: '';
    position: absolute;
    inset: -1px;
    border-radius: 0;
    background: linear-gradient(
        135deg,
        rgba(216,168,56,0.6),
        rgba(168,88,216,0.6),
        rgba(64,216,208,0.6),
        rgba(56,216,120,0.6),
        rgba(216,168,56,0.6)
    );
    background-size: 300% 300%;
    animation: prismatic-border 3s linear infinite;
    z-index: -1;
    pointer-events: none;
}
.creature-prismatic .creature-grid {
    animation: prismatic-hue 6s linear infinite;
}
@keyframes prismatic-hue {
    0%   { filter: hue-rotate(0deg); }
    25%  { filter: hue-rotate(40deg); }
    50%  { filter: hue-rotate(-30deg); }
    75%  { filter: hue-rotate(20deg); }
    100% { filter: hue-rotate(0deg); }
}
@keyframes prismatic-border {
    0%   { background-position: 0% 50%; }
    50%  { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}
@keyframes prismatic-shift {
    0%, 100% { box-shadow: 0 0 12px rgba(216,168,56,0.3), 0 0 24px rgba(216,168,56,0.1); }
    33%      { box-shadow: 0 0 12px rgba(168,88,216,0.3), 0 0 24px rgba(168,88,216,0.1); }
    66%      { box-shadow: 0 0 12px rgba(64,216,208,0.3), 0 0 24px rgba(64,216,208,0.1); }
}

/* ============================================================
   LIBRARY GRID (in panel)
   ============================================================ */
.library-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
    gap: 6px;
}
.library-card {
    cursor: default;
}
.library-card-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 3px;
    padding: 0 2px;
}
.library-card-name {
    font-family: var(--font-system);
    font-size: 9px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: 55px;
}
.library-card-creator {
    font-family: var(--font-system);
    font-size: 8px;
    color: var(--text-dim);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: 40px;
}

/* ============================================================
   PVP LOBBY
   ============================================================ */
.lobby-btn {
    display: inline-block;
    padding: 6px 14px;
    font-family: var(--font-pixel);
    font-weight: 700;
    font-size: 11px;
    letter-spacing: 0.1em;
    border: 1px solid;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease;
}
.lobby-btn-create {
    background: rgba(56,216,120,0.15);
    border-color: rgba(56,216,120,0.5);
    color: #38d878;
    width: 100%;
}
.lobby-btn-create:hover {
    background: rgba(56,216,120,0.25);
    border-color: rgba(56,216,120,0.8);
}
.lobby-btn-accept {
    background: rgba(64,216,208,0.15);
    border-color: rgba(64,216,208,0.5);
    color: #40d8d0;
    flex-shrink: 0;
}
.lobby-btn-accept:hover {
    background: rgba(64,216,208,0.25);
    border-color: rgba(64,216,208,0.8);
}
.lobby-btn-cancel {
    background: rgba(216,64,64,0.12);
    border-color: rgba(216,64,64,0.4);
    color: #d84040;
}
.lobby-btn-cancel:hover {
    background: rgba(216,64,64,0.22);
    border-color: rgba(216,64,64,0.7);
}

.lobby-pending {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    border: 1px solid var(--pane-border-dim);
    background: rgba(10,26,40,0.4);
}
.lobby-pending-text {
    font-family: var(--font-system);
    font-size: 12px;
    color: var(--text-dim);
}

.lobby-heading {
    font-family: var(--font-pixel);
    font-weight: 600;
    font-size: 10px;
    letter-spacing: 0.15em;
    color: var(--text-dim);
    padding: 4px 0;
    border-bottom: 1px solid var(--pane-border-dim);
    margin-bottom: 4px;
}

.lobby-challenge {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 4px;
    border-bottom: 1px solid rgba(26,56,72,0.3);
}
.lobby-challenge-info {
    display: flex;
    flex-direction: column;
    gap: 1px;
}
.lobby-challenge-name {
    font-family: var(--font-system);
    font-size: 13px;
    color: var(--text-primary);
}
.lobby-challenge-detail {
    font-family: var(--font-system);
    font-size: 10px;
    color: var(--text-dim);
}

/* ============================================================
   HAND SELECTION & CLAIM GRIDS
   ============================================================ */
.handselect-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
    gap: 6px;
}
.handselect-card {
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.12s ease, transform 0.1s ease;
    padding: 2px;
}
.handselect-card:hover {
    border-color: rgba(56,216,120,0.4);
    transform: scale(1.02);
}
.handselect-card-selected {
    border-color: rgba(56,216,120,0.8) !important;
    box-shadow: 0 0 10px rgba(56,216,120,0.2);
}
.claim-card:hover {
    border-color: rgba(216,168,56,0.6);
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 720px) {
    .window-panel {
        max-width: calc(100vw - 16px);
        min-width: 160px;
    }
    /* Stack permanent panels at bottom-right, narrower */
    .window-panel[data-permanent="true"] {
        width: 240px !important;
        max-width: 60vw;
    }
    /* Battle grid cells — bigger touch targets */
    .battle-cell { min-height: 44px; }
    /* Hand cards — wider columns for touch */
    .hand-grid { grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); }
    .hand-card-clickable:active { transform: scale(0.97); }
    /* Lobby buttons — bigger touch targets */
    .lobby-btn { padding: 10px 16px; font-size: 12px; }
    /* Hand select grid — bigger cards on mobile */
    .handselect-grid { grid-template-columns: repeat(auto-fill, minmax(85px, 1fr)); }
    .handselect-card { padding: 3px; }
    /* Terminal input — larger on mobile */
    #terminalField { font-size: 16px; } /* prevents iOS zoom */
    #terminalInput { padding: 10px 14px; }
}

@media (max-width: 480px) {
    .window-panel {
        max-width: calc(100vw - 8px);
    }
    .window-panel[data-permanent="true"] {
        width: 200px !important;
    }
    /* Collapse all permanent panels by default on very small screens */
    .creature-md .creature-cell { width: 7px; height: 9px; font-size: 7.5px; line-height: 9px; }
    .battle-grid { gap: 3px; }
    .hand-grid { grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 3px; }
    /* Panel resize handle — bigger touch target */
    .panel-resize { width: 22px; height: 22px; }
    .panel-resize::after { width: 10px; height: 10px; }
    /* Panel close button — bigger touch target */
    .panel-close { padding: 4px 8px; font-size: 16px; }
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}
