/* ============ Базовые стили ============ */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}

body {
    background: #0d0d0d;
    color: #e0e0e0;
    font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    font-size: 14px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
}

img { display: block; max-width: 100%; }
button { font-family: inherit; }
input, select, button { color: inherit; }

/* ============ Topbar ============ */
.topbar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    padding: 14px 24px;
    border-bottom: 1px solid #1f1f1f;
    background: #0d0d0d;
    position: sticky;
    top: 0;
    z-index: 20;
}
.topbar-left { justify-self: start; }
.topbar-center { justify-self: center; }
.topbar-right { justify-self: end; }

.label {
    text-transform: uppercase;
    letter-spacing: 3px;
    font-size: 13px;
    color: #888;
    font-weight: 500;
}
.site-name {
    color: #e0e0e0;
    text-decoration: none;
    font-weight: 600;
    font-size: 15px;
    letter-spacing: 0.5px;
    padding: 6px 14px;
    border: 1px solid #2a2a2a;
    border-radius: 4px;
    transition: border-color .2s ease, color .2s ease;
}
.site-name:hover { border-color: #555; color: #fff; }

/* ============ Основная сетка ============ */
.wrap {
    max-width: 1400px;
    margin: 0 auto;
    padding: 24px;
    display: grid;
    grid-template-columns: minmax(320px, 1fr) 360px;
    gap: 28px;
}

/* ============ Колонка с доской ============ */
.board-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
}

/* ============ Рамка и доска ============ */
.board-frame {
    position: relative;
    padding: 28px;
    border-radius: 6px;
    background: linear-gradient(135deg, #5c3d1e 0%, #3b2510 100%);
    box-shadow:
        0 8px 32px rgba(0,0,0,0.7),
        inset 0 1px 0 rgba(255,255,255,0.06);
    display: grid;
    grid-template-columns: 20px auto 20px;
    grid-template-rows: 20px auto 20px;
    grid-template-areas:
        "top   top    top"
        "left  board  right"
        "bot   bot    bot";
    gap: 0;
    user-select: none;
    -webkit-user-select: none;
}

.board {
    grid-area: board;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    aspect-ratio: 1 / 1;
    width: clamp(300px, min(65vh, 720px), 720px);
    height: clamp(300px, min(65vh, 720px), 720px);
    border-radius: 2px;
    overflow: hidden;
    box-shadow: inset 0 0 0 1px rgba(0,0,0,0.35);
}

.coords-top,
.coords-bottom {
    display: grid;
    grid-template-columns: 20px auto 20px;
}
.coords-top { grid-area: top; }
.coords-bottom { grid-area: bot; }
.coords-top .coord-spacer,
.coords-bottom .coord-spacer {
    /* пусто */
}

.coords-left {
    grid-area: left;
    display: grid;
    grid-template-rows: repeat(8, 1fr);
    align-items: center;
    justify-items: center;
}
.coords-right {
    grid-area: right;
    display: grid;
    grid-template-rows: repeat(8, 1fr);
    align-items: center;
    justify-items: center;
}

.coord {
    font-size: 11px;
    font-weight: 700;
    font-family: 'Inter', system-ui, sans-serif;
    letter-spacing: 1px;
    text-transform: uppercase;
    user-select: none;
}
.coord.file { color: #e6d4b6; }
.coord.rank { color: #e6d4b6; }

/* ============ Клетки ============ */
.square {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color .15s ease;
}
.square.light { background: #f0d9b5; }
.square.dark  { background: #b58863; }

.square.selected {
    background-color: rgba(255, 255, 0, 0.35) !important;
}
.square.last-move.light { background: #f5e9a8; }
.square.last-move.dark  { background: #b59a55; }
.square.check.light,
.square.check.dark {
    box-shadow: inset 0 0 0 3px rgba(255, 50, 50, 0.6);
}

.piece-img {
    width: 85%;
    height: 85%;
    pointer-events: none;
    user-select: none;
    -webkit-user-drag: none;
    filter: drop-shadow(0 2px 3px rgba(0,0,0,0.4));
    transition: transform .12s ease;
}
.square.selected .piece-img {
    transform: scale(1.06);
}

.dot {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 30%;
    height: 30%;
    border-radius: 50%;
    pointer-events: none;
    background: rgba(0, 0, 0, 0.2);
}
.dot.capture {
    width: 92%;
    height: 92%;
    background: transparent;
    border: 5px solid rgba(255, 0, 0, 0.25);
    border-radius: 50%;
}

/* ============ Взятые фигуры ============ */
.captured-panel {
    width: clamp(300px, min(65vh, 720px), 720px);
    min-height: 40px;
    background: #1a1a1a;
    border-radius: 6px;
    display: flex;
    align-items: center;
    padding: 6px 12px;
    gap: 10px;
    border: 1px solid #262626;
}
.captured-pieces {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    align-items: center;
    flex: 1;
}
.captured-pieces img {
    width: 22px;
    height: 22px;
    pointer-events: none;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.5));
}
.captured-score {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: #9fd3ff;
    min-width: 24px;
    text-align: right;
}

/* ============ Status line ============ */
.status-line {
    font-size: 14px;
    color: #bfbfbf;
    text-align: center;
    min-height: 22px;
}

/* ============ Сайдбар ============ */
.sidebar {
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.panel {
    background: #151515;
    border: 1px solid #242424;
    border-radius: 8px;
    padding: 14px;
}
.panel-title {
    font-size: 12px;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: #888;
    margin-bottom: 10px;
    font-weight: 600;
}

/* Mode switch */
.mode-switch {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    background: #0d0d0d;
    padding: 4px;
    border-radius: 6px;
    border: 1px solid #242424;
}
.mode-btn {
    background: transparent;
    border: none;
    color: #999;
    padding: 8px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: background .2s, color .2s;
}
.mode-btn:hover { color: #fff; }
.mode-btn.active {
    background: #2d2d2d;
    color: #fff;
}
.mode-extra { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.row-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    color: #bfbfbf;
}
.row-label select {
    background: #0d0d0d;
    border: 1px solid #2a2a2a;
    color: #e0e0e0;
    padding: 6px 8px;
    border-radius: 4px;
    font-size: 13px;
    min-width: 140px;
}

/* Online panel */
.online-actions { display: flex; flex-direction: column; gap: 8px; }
.code-row { display: grid; grid-template-columns: 1fr auto; gap: 6px; }
.code-input {
    background: #0d0d0d;
    border: 1px solid #2a2a2a;
    border-radius: 4px;
    padding: 10px 12px;
    color: #fff;
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 16px;
    letter-spacing: 4px;
    text-transform: uppercase;
    text-align: center;
    width: 100%;
}
.code-input:focus { outline: none; border-color: #5b8fb9; }
.my-code {
    text-align: center;
    font-family: 'Consolas', monospace;
    font-size: 20px;
    letter-spacing: 5px;
    color: #9fd3ff;
    padding: 8px;
    background: #0d0d0d;
    border: 1px dashed #2a2a2a;
    border-radius: 4px;
    min-height: 38px;
}
.conn-status {
    font-size: 12px;
    text-align: center;
    padding: 6px;
    border-radius: 4px;
    background: #0d0d0d;
    color: #888;
    letter-spacing: 0.5px;
}
.conn-status.ok { color: #7fd47f; }
.conn-status.err { color: #ff7a7a; }

/* History */
.history {
    background: #0d0d0d;
    border: 1px solid #242424;
    border-radius: 6px;
    padding: 10px;
    max-height: 260px;
    overflow-y: auto;
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 13px;
}
.history-empty { color: #555; font-style: italic; text-align: center; padding: 10px 0; }
.history-row {
    display: grid;
    grid-template-columns: 36px 1fr 1fr;
    gap: 8px;
    padding: 3px 4px;
    border-radius: 3px;
}
.history-row:nth-child(odd) { background: rgba(255,255,255,0.02); }
.history-row .num { color: #666; }
.history-row .m-white,
.history-row .m-black { cursor: default; padding: 1px 4px; border-radius: 2px; }
.history-row .m-white:hover,
.history-row .m-black:hover { background: #1d1d1d; }

.history::-webkit-scrollbar { width: 8px; }
.history::-webkit-scrollbar-track { background: #0a0a0a; }
.history::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 4px; }

/* Material */
.material-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 2px;
    font-size: 14px;
}
.mat-label { color: #999; }
.mat-value {
    font-family: 'Consolas', monospace;
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    color: #e0e0e0;
}

/* Actions */
.actions { display: flex; flex-direction: column; gap: 8px; }

/* Buttons */
.btn {
    border: 1px solid transparent;
    border-radius: 5px;
    padding: 10px 14px;
    font-size: 14px;
    cursor: pointer;
    transition: background .15s ease, border-color .15s ease, transform .05s ease;
    font-weight: 500;
    letter-spacing: 0.3px;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: #5b8fb9; color: #fff; border-color: #5b8fb9; }
.btn-primary:hover { background: #6ea0c9; }
.btn-secondary { background: #2a2a2a; color: #e0e0e0; border-color: #2a2a2a; }
.btn-secondary:hover { background: #333; }
.btn-ghost { background: transparent; color: #bfbfbf; border-color: #2a2a2a; }
.btn-ghost:hover { background: #1a1a1a; }
.btn-warn { background: #6e5b2a; color: #fff; border-color: #6e5b2a; }
.btn-warn:hover { background: #847038; }
.btn-danger { background: #8b2a2a; color: #fff; border-color: #8b2a2a; }
.btn-danger:hover { background: #a13535; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Legend */
.legend {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
}
.legend-col { display: flex; flex-direction: column; gap: 6px; }
.legend-title {
    font-size: 11px;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-bottom: 4px;
}
.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: #ccc;
}
.legend-img {
    width: 28px;
    height: 28px;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.4));
}

/* ============ Overlays ============ */
.overlay,
.promo-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    animation: fade-in .25s ease;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.overlay-card,
.promo-card {
    background: #181818;
    border: 1px solid #2a2a2a;
    border-radius: 10px;
    padding: 28px 32px;
    max-width: 420px;
    width: 90%;
    text-align: center;
    box-shadow: 0 12px 40px rgba(0,0,0,0.7);
    animation: pop .25s ease;
}
@keyframes pop { from { transform: scale(.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.overlay-title {
    font-size: 22px;
    font-weight: 700;
    color: #fff;
    margin-bottom: 8px;
    letter-spacing: 0.5px;
}
.overlay-sub {
    color: #aaa;
    font-size: 14px;
    margin-bottom: 20px;
}
.overlay-actions,
.promo-choices {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
}
.promo-title {
    font-size: 16px;
    color: #fff;
    margin-bottom: 14px;
}
.promo-choices { gap: 12px; }
.promo-choice {
    background: #0d0d0d;
    border: 2px solid #2a2a2a;
    border-radius: 8px;
    padding: 10px;
    cursor: pointer;
    transition: border-color .15s, transform .15s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}
.promo-choice:hover { border-color: #5b8fb9; transform: translateY(-2px); }
.promo-choice img { width: 60px; height: 60px; }
.promo-choice span { font-size: 12px; color: #bbb; }

/* Utility */
.hidden { display: none !important; }

/* Footer */
.footer {
    text-align: center;
    padding: 24px;
    color: #555;
    font-size: 12px;
    border-top: 1px solid #1a1a1a;
    margin-top: 24px;
}

/* ============ Адаптив ============ */
@media (max-width: 960px) {
    .wrap {
        grid-template-columns: 1fr;
    }
    .sidebar {
        order: 2;
    }
    .board-col { order: 1; }
}

@media (max-width: 768px) {
    .topbar {
        grid-template-columns: auto auto;
        padding: 10px 14px;
        gap: 8px;
    }
    .topbar-center { display: none; }
    .topbar-right { justify-self: end; }
    .label { font-size: 11px; letter-spacing: 2px; }
    .wrap { padding: 12px; gap: 14px; }
    .board-frame {
        width: 90vw;
        padding: 18px;
    }
    .board {
        width: 100%;
        height: auto;
    }
    .captured-panel { width: 90vw; }
    .coord { font-size: 9px; }
    .sidebar { gap: 10px; }
    .panel { padding: 10px; }
}

@media (max-width: 420px) {
    .label { display: none; }
    .topbar { grid-template-columns: 1fr; justify-items: center; }
}