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

html, body {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #0f1115;
    color: #e6e6e6;
    line-height: 1.5;
    overflow: hidden;
}

.layout {
    display: grid;
    grid-template-columns: 340px 1fr;
    height: 100vh;
}

/* --- Sidebar ----------------------------------------------------------- */

.sidebar {
    background: #14171d;
    border-right: 1px solid #2a2e38;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.sidebar-header h1 {
    font-size: 1.4rem;
    font-weight: 600;
    color: #f5f5f5;
    margin-bottom: 0.25rem;
}

.subtitle {
    color: #8a8f98;
    font-size: 0.85rem;
}

.controls {
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.control-group label {
    font-size: 0.75rem;
    color: #8a8f98;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

select,
input[type="number"] {
    background: #0f1115;
    color: #e6e6e6;
    border: 1px solid #2a2e38;
    border-radius: 6px;
    padding: 0.55rem 0.7rem;
    font-size: 0.9rem;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s;
}

select:focus,
input[type="number"]:focus {
    border-color: #4a90e2;
}

.btn-primary {
    background: #4a90e2;
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 0.65rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    text-decoration: none;
    text-align: center;
    display: inline-block;
}

.btn-primary:hover {
    background: #3a7bc8;
}

.btn-primary:disabled {
    background: #2a2e38;
    color: #5a5f6a;
    cursor: not-allowed;
}

.btn-secondary {
    background: transparent;
    color: #e6e6e6;
    border: 1px solid #2a2e38;
    border-radius: 6px;
    padding: 0.6rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    display: inline-block;
    transition: border-color 0.15s, color 0.15s;
}

.btn-secondary:hover {
    border-color: #4a90e2;
    color: #4a90e2;
}

/* --- Game info (sidebar) ---------------------------------------------- */

.game-info {
    background: #1a1d24;
    border: 1px solid #2a2e38;
    border-radius: 8px;
    padding: 1rem;
    flex: 1;
    min-height: 200px;
}

.game-info.empty {
    display: flex;
    align-items: center;
    justify-content: center;
}

.info-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: #f5f5f5;
    margin-bottom: 0.2rem;
    line-height: 1.3;
}

.info-title .vs {
    color: #5a5f6a;
    font-weight: 400;
    font-size: 0.85em;
    margin: 0 0.25rem;
}

.info-subtitle {
    color: #8a8f98;
    font-size: 0.85rem;
    margin-bottom: 1rem;
}

.game-detail {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.game-detail .row {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    padding-bottom: 0.4rem;
    border-bottom: 1px solid #23262e;
    font-size: 0.85rem;
}

.game-detail .row:last-child {
    border-bottom: none;
}

.game-detail .label {
    color: #8a8f98;
    flex-shrink: 0;
}

.game-detail .value {
    color: #e6e6e6;
    text-align: right;
    word-break: break-word;
}

.info-actions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #2a2e38;
}

.placeholder {
    color: #5a5f6a;
    font-style: italic;
    font-size: 0.85rem;
    text-align: center;
}

/* --- Main viewer ------------------------------------------------------- */

.main {
    padding: 1.5rem 1.5rem 2.5rem;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.viewer.empty {
    background: #14171d;
    border: 1px dashed #2a2e38;
    border-radius: 12px;
}

.placeholder-large {
    color: #5a5f6a;
    font-size: 1.1rem;
    text-align: center;
}

/* --- Analysis viewer --------------------------------------------------- */

.analysis {
    display: grid;
    grid-template-columns: auto 24px 1fr;
    gap: 1rem;
    width: 100%;
    height: 100%;
    max-height: calc(100vh - 4rem);
}

.board-wrapper {
    aspect-ratio: 1 / 1;
    height: 100%;
    max-height: calc(100vh - 4rem);
    background: #0f1115;
    border: 1px solid #2a2e38;
    border-radius: 10px;
    overflow: hidden;
    position: relative;
}

.board-wrapper .cg-wrap {
    width: 100% !important;
    height: 100% !important;
}

.eval-bar {
    width: 24px;
    height: 100%;
    max-height: calc(100vh - 4rem);
    aspect-ratio: auto;
    background: #1a1d24;
    border: 1px solid #2a2e38;
    border-radius: 4px;
    position: relative;
    overflow: hidden;
}

.eval-bar-fill {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    background: #f5f5f5;
    transition: height 0.25s ease;
    height: 50%;
}

.eval-bar-label {
    position: absolute;
    bottom: 4px;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 0.65rem;
    color: #0f1115;
    font-weight: 700;
    z-index: 2;
    pointer-events: none;
}

.eval-bar-label.top {
    bottom: auto;
    top: 4px;
    color: #e6e6e6;
}

.analysis-side {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    min-width: 0;
    overflow: hidden;
}

.engine-readout {
    background: #1a1d24;
    border: 1px solid #2a2e38;
    border-radius: 8px;
    padding: 0.75rem 1rem;
    font-size: 0.85rem;
    min-height: 9rem;
    display: flex;
    flex-direction: column;
}

.engine-header {
    margin-bottom: 0.5rem;
}

.engine-readout .eval-num {
    font-size: 1.4rem;
    font-weight: 700;
    color: #f5f5f5;
    margin-right: 0.5rem;
}

.engine-readout .eval-depth {
    color: #8a8f98;
    font-size: 0.8rem;
}

.pv-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    height: 5.4em;
}

.pv-row {
    display: flex;
    gap: 0.6rem;
    align-items: baseline;
    font-family: "Consolas", "Monaco", monospace;
    font-size: 0.8rem;
    line-height: 1.5;
    white-space: nowrap;
    overflow: hidden;
    height: 1.6em;
}

.pv-row .pv-eval {
    color: #f5f5f5;
    font-weight: 600;
    flex-shrink: 0;
    width: 3.5em;
}

.pv-row .pv-line {
    color: #c0c4cb;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.engine-readout.loading {
    color: #8a8f98;
}

/* Engine-off state: keep grid layout, just hide eval visuals so the toggle stays clickable. */
.analysis.engine-off .eval-bar {
    visibility: hidden;
}
.analysis.engine-off .engine-readout {
    display: none;
}

.move-list {
    background: #1a1d24;
    border: 1px solid #2a2e38;
    border-radius: 8px;
    padding: 0.5rem;
    flex: 1;
    overflow-y: auto;
    font-family: "Consolas", "Monaco", monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    min-height: 0;
}

.move-list .move-pair {
    display: inline;
}

.move-list .move-num {
    color: #5a5f6a;
    margin-right: 0.3rem;
}

.move-list .move {
    display: inline-block;
    padding: 0.1rem 0.35rem;
    margin: 0 0.1rem;
    border-radius: 3px;
    color: #e6e6e6;
    cursor: pointer;
    transition: background 0.1s;
}

.move-list .move:hover {
    background: #2a2e38;
}

.move-list .move.active {
    background: #4a90e2;
    color: #fff;
}

.viewer-controls {
    display: flex;
    gap: 0.4rem;
    background: #1a1d24;
    border: 1px solid #2a2e38;
    border-radius: 8px;
    padding: 0.5rem;
}

.viewer-controls button {
    flex: 1;
    background: transparent;
    color: #e6e6e6;
    border: 1px solid transparent;
    border-radius: 4px;
    padding: 0.4rem;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background 0.1s;
}

.viewer-controls button:hover:not(:disabled) {
    background: #2a2e38;
}

.viewer-controls button:disabled {
    color: #5a5f6a;
    cursor: not-allowed;
}

.viewer-controls button.active {
    background: #4a90e2;
    color: #fff;
}

.import-error {
    background: #2a1d1d;
    border: 1px solid #5a2e2e;
    color: #e6b6b6;
    padding: 1.2rem;
    border-radius: 8px;
    font-size: 0.9rem;
    line-height: 1.5;
    max-width: 500px;
}

.import-error code {
    background: #0f1115;
    padding: 0.1rem 0.4rem;
    border-radius: 3px;
    font-size: 0.85em;
    color: #e6e6e6;
}

/* --- Responsive -------------------------------------------------------- */

@media (max-width: 1000px) {
    body {
        overflow: auto;
    }
    .layout {
        grid-template-columns: 1fr;
        height: auto;
        min-height: 100vh;
    }
    .sidebar {
        border-right: none;
        border-bottom: 1px solid #2a2e38;
    }
    .main {
        padding: 1rem;
        align-items: flex-start;
    }
    .analysis {
        grid-template-columns: 1fr 24px;
        grid-template-rows: auto auto;
        max-height: none;
    }
    .board-wrapper {
        grid-column: 1;
        grid-row: 1;
        max-height: none;
    }
    .eval-bar {
        grid-column: 2;
        grid-row: 1;
        max-height: none;
    }
    .analysis-side {
        grid-column: 1 / -1;
        grid-row: 2;
    }
}
