/* --- LAYOUT & THEME --- */
body { 
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
    background-color: #1a1a1a; 
    color: #eee; 
    display: flex; 
    flex-direction: column; 
    align-items: center; 
    margin: 0; 
    padding: 20px;
    min-height: 100vh;
}

h1 { margin: 10px 0 20px 0; color: #e74c3c; text-transform: uppercase; letter-spacing: 2px; text-align: center; }

/* Main Container */
#main-layout { 
    display: flex; 
    gap: 20px; 
    align-items: flex-start; 
    justify-content: center; 
    flex-wrap: wrap;
    width: 100%;
    max-width: 1400px;
}

/* Layout variants */
#main-layout.sidebar-right {
    flex-direction: row-reverse;
}

/* Move list on right side */
#game-area.movelist-right {
    flex-direction: row;
    gap: 20px;
    align-items: stretch;
}

#game-area.movelist-right #board-column {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#game-area.movelist-right #movelist-column {
    display: flex;
    flex-direction: column;
    gap: 10px;
    /* height removed to allow stretch */
}

#game-area.movelist-right #log-area {
    width: 280px !important;
    height: auto !important;
    max-height: none;
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
}

#movelist-column.history-in-movelist #history-nav {
    width: 280px;
    margin-top: 0;
    box-sizing: border-box;
}

/* Move list on left side */
#game-area.movelist-left {
    flex-direction: row-reverse;
    gap: 20px;
    align-items: stretch;
}

#game-area.movelist-left #board-column {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#game-area.movelist-left #movelist-column {
    display: flex;
    flex-direction: column;
    gap: 10px;
    /* height removed to allow stretch */
}

#game-area.movelist-left #log-area {
    width: 280px !important;
    height: auto !important;
    max-height: none;
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
}

#game-area.movelist-left #movelist-column.history-in-movelist #history-nav {
    width: 280px;
    margin-top: 0;
    box-sizing: border-box;
}

/* Status bar and History nav positioning with proper stacking order */
/* Board is the main reference point */
#board {
    order: 5;
}

/* Default: Status bar above board (order 4), History nav below board (order 6) */
#status-bar-container {
    order: 4;
}

#history-nav {
    order: 6;
}

/* Status bar bottom: moves it below the board */
#board-column.statusbar-bottom #status-bar-container {
    order: 6;
}

/* History nav top: moves it above the board */
#board-column.history-top #history-nav {
    order: 4;
}

/* When BOTH at bottom: status bar first (closer to board), then history */
#board-column.statusbar-bottom:not(.history-top) #status-bar-container {
    order: 6;
}

#board-column.statusbar-bottom:not(.history-top) #history-nav {
    order: 7;
}

/* When BOTH at top: status bar first, then history */
#board-column.history-top:not(.statusbar-bottom) #status-bar-container {
    order: 3;
}

#board-column.history-top:not(.statusbar-bottom) #history-nav {
    order: 4;
}

/* --- NEW UI STYLES --- */
#sidebar {
    padding: 0 !important;
    background: #444 !important; /* Gap color */
    gap: 1px;
    display: flex;
    flex-direction: column;
    overflow: visible;
    width: 280px;
    min-width: 280px;
    max-width: 280px;
}

.accordion-item, .panel {
    background: #2c2c2c;
    overflow: visible;
}

.accordion-header, .panel-header {
    padding: 12px 15px;
    background: #252525;
    color: #eee;
    font-weight: bold;
    cursor: pointer;
    user-select: none;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 1px;
    border-left: 4px solid transparent;
    transition: background 0.2s;
}

.accordion-header:hover {
    background: #333;
}

.accordion-header.active {
    border-left: 4px solid #e74c3c;
    background: #333;
}

/* Direct child only — this is the ▼ chevron. A descendant selector would
   also catch the .drag-handle span nested in the header's inner flex div and
   flip it upside down whenever the section is open. */
.accordion-header > span {
    transition: transform 0.2s;
}

.accordion-header.active > span {
    transform: rotate(180deg);
}

.accordion-content {
    padding: 15px;
    display: none;
    background: #2c2c2c;
    overflow: visible;
}

.accordion-content.show {
    display: block;
    animation: slideDown 0.2s ease-out;
}

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

.panel-content {
    padding: 15px;
    overflow: visible;
}

.control-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}
.control-row label { margin-bottom: 0; font-weight: normal; color: #aaa; }
.control-row select, .control-row input[type=range], .control-row input[type=color] { 
    width: 55%; 
    margin-bottom: 0; 
}

.action-area {
    padding: 15px;
    background: #222;
    border-top: 1px solid #444;
}




/* Compact Player Setup */
.p-setup-row {
    background: #333;
    padding: 5px 8px;
    border-radius: 4px;
    margin-bottom: 6px;
}
.p-setup-row select {
    width: 65%;
    margin: 0;
    border: none;
    background: transparent;
    color: white;
    text-align: left;
    cursor: pointer;
}
.p-setup-row select:focus { outline: none; background: #444; }

/* Material List Compact */
#material-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px;
    max-height: 300px;
    overflow-y: auto;
    padding-right: 5px;
}
#material-list div {
    background: #333;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    text-align: left;
    overflow: hidden;
    min-width: 0;
}

/* Board Area */
#game-area { display: flex; flex-direction: column; align-items: center; gap: 10px; }

#board-column { 
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
}
#movelist-column { display: contents; }

#board { 
    display: grid; 
    grid-template-columns: repeat(14, 40px); 
    grid-template-rows: repeat(14, 40px); 
    border: 10px solid #333; 
    background: #222;
    user-select: none;
    box-shadow: 0 20px 50px rgba(0,0,0,0.6);
    /* Fixed dimensions to prevent layout shifts */
    width: 560px; 
    height: 560px; 
}

.square { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; font-size: 28px; cursor: pointer; position: relative; }
/* Keep filtered pieces in their square's stacking context. On a rotated
   board Chromium otherwise changes their rasterization when nearby move
   markers appear, making a stationary piece alternate between sharp/soft. */
#board .square { isolation: isolate; }
.white-sq { background-color: #bdc3c7; }
.black-sq { background-color: #7f8c8d; }
.invalid-sq { background-color: transparent; border: none; cursor: default; }

/* Square a dragged piece would land on. On touch the dragged piece floats
   above the fingertip, so this ring is what says where it is actually going. */
.square.drop-target::before {
    content: '';
    position: absolute;
    inset: 0;
    box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.85);
    pointer-events: none;
    z-index: 4;
}

/* Shown only inside the mobile shell; see the media query at the bottom. */
.mobile-only { display: none !important; }
#mobile-tabbar { display: none; }

/* Status & Log */
#status-box { width: 100%; max-width: 580px; background: #333; padding: 10px; border-radius: 4px; text-align: center; font-weight: bold; font-size: 18px; margin-bottom: 5px; border: 2px solid #444; }
#log-area { 
    width: 580px; 
    height: 160px; 
    background: #2a2a2a; 
    border: 2px solid #444; 
    border-radius: 6px; 
    padding: 8px; 
    box-sizing: border-box;
    overflow-y: auto; 
    overflow-x: hidden;
    font-family: 'Segoe UI', Tahoma, sans-serif;
}

/* Custom Scrollbar for Move List */
#log-area::-webkit-scrollbar {
    width: 8px;
}

#log-area::-webkit-scrollbar-track {
    background: #222;
    border-radius: 4px;
}

#log-area::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 4px;
}

#log-area::-webkit-scrollbar-thumb:hover {
    background: #555;
}

#move-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.move-row {
    display: grid;
    grid-template-columns: 30px 1fr 1fr; /* Default for 2-player, overridden by JS */
    gap: 3px;
    padding: 3px;
    background: #222;
    border-radius: 4px;
    transition: background 0.15s;
    align-items: center;
}

.move-row:hover {
    background: #2d2d2d;
}

.move-number {
    font-weight: bold;
    color: #888;
    font-size: 12px;
    text-align: center;
}

.move-item {
    padding: 5px 6px;
    border-radius: 3px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.15s;
    background: #333;
    color: #ddd;
    text-align: center;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-width: 0;
    overflow: hidden;
}

.move-piece-icon {
    width: 18px;
    height: 18px;
    object-fit: contain;
    filter: drop-shadow(0 1px 1px rgba(0,0,0,0.3));
    flex-shrink: 0;
}

.move-item:hover {
    background: #3d3d3d;
    color: #fff;
    transform: translateY(-1px);
}

.move-item.active {
    background: #4a4a4a; /* Neutral lighter gray for selection */
    color: #fff;
    font-weight: bold;
    box-shadow: 0 0 6px rgba(0,0,0,0.6);
}

.move-item.before-first {
    background: #4a4a4a; /* Neutral lighter gray for selection */
    color: #fff;
    font-weight: bold;
    box-shadow: 0 0 6px rgba(0,0,0,0.6);
}

.move-item.empty {
    background: transparent;
    cursor: default;
}

.move-item.empty:hover {
    background: transparent;
    transform: none;
}

/* Color-coded moves by player */
.move-item[data-color="red"] { border-left: 3px solid #ff3333; }
.move-item[data-color="blue"] { border-left: 3px solid #3399ff; }
.move-item[data-color="yellow"] { border-left: 3px solid #ffcc00; }
.move-item[data-color="green"] { border-left: 3px solid #00cc66; }
.move-item[data-color="white"] { border-left: 3px solid #ffffff; }
.move-item[data-color="black"] { border-left: 3px solid #555555; }

/* Event messages (non-move logs) */
.event-message {
    padding: 6px 10px;
    margin: 2px 0;
    background: #1a1a1a;
    border-left: 3px solid #e74c3c;
    border-radius: 3px;
    font-size: 12px;
    color: #ccc;
    font-style: italic;
}

/* --- VISUALS --- */
#board .square.selected::before {
    content: ''; position: absolute; inset: 0; background: rgba(245, 218, 80, .35); pointer-events: none;
}
.last-move { background-color: rgba(255, 255, 0, 0.3) !important; }
#board .valid-move::after, #board .capture-move::after {
    content: ''; position: absolute; inset: 0; margin: auto;
    border-radius: 50%; box-sizing: border-box; pointer-events: none; z-index: 3;
}
#board .valid-move::after { width: 25%; height: 25%; background: rgba(20, 20, 20, .25); }
#board .capture-move::after { width: 94%; height: 94%; border: clamp(4px, .7vw, 7px) solid rgba(20, 20, 20, .28); }

/* Drag and Drop Styles */
/* The grip is drawn with a dot-grid gradient rather than written as text.
   It used to be two U+22EE "vertical ellipsis" glyphs, but that is a maths
   symbol that few UI fonts carry, so each browser fell back to a different
   font and spaced the dots differently. A gradient renders identically
   everywhere and needs no glyph at all. */
.drag-handle {
    cursor: move;
    margin-right: 10px;
    display: inline-block;
    flex-shrink: 0;
    width: 8px;
    height: 12px;
    background-image: radial-gradient(#666 40%, transparent 45%);
    background-size: 4px 4px;
    background-position: center;
}
.drag-handle:hover {
    background-image: radial-gradient(#aaa 40%, transparent 45%);
}

.accordion-item.dragging, .panel.dragging {
    opacity: 0.5;
    border: 2px dashed #666;
}

.in-check { background-image: radial-gradient(ellipse at center, rgba(230, 35, 35, .8) 0%, rgba(230, 35, 35, .35) 48%, transparent 75%); }
.premove-highlight { background-image: linear-gradient(rgba(110, 140, 220, .3), rgba(110, 140, 220, .3)); }

.piece { 
    z-index: 2; 
    font-family: 'Segoe UI Symbol', 'Arial Unicode MS', serif; 
    line-height: 1;
    filter: drop-shadow(0px 2px 2px rgba(0,0,0,0.6));
    /* Transition for smooth movement */
    transition: transform 0.1s;
}
@media (hover: hover) {
    .piece:hover { transform: scale(1.15); }
}

.p-red { color: #ff3333; text-shadow: 0 0 1px #500; }
.p-blue { color: #3399ff; text-shadow: 0 0 1px #005; }
.p-yellow { color: #ffcc00; text-shadow: 0 0 1px #550; }
.p-green { color: #00cc66; text-shadow: 0 0 1px #050; }
.p-white { color: #ffffff; text-shadow: 0 0 2px #000; }
.p-black { color: #000000; text-shadow: 0 0 1px #fff; }
.p-gray { color: #555; text-shadow: none; opacity: 1; }

.p-setup-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.p-label { width: 60px; font-weight: bold; font-size: 14px; }

/* --- UPDATED UI & ALIGNMENT --- */
.control-row select {
    text-align: left; /* Fix right centered text */
    text-align-last: left;
}

/* Custom Range Input Styling to fix "white part".
   The track and thumb have to be declared once per engine — a rule listing
   both ::-webkit- and ::-moz- selectors together is dropped entirely by both
   browsers, since neither understands the other's pseudo-element. Firefox
   also needs an explicit `border: none` on the thumb, which it draws by
   default and Chromium does not. */
input[type=range] {
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
}
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: #e74c3c;
    cursor: pointer;
    margin-top: -6px;
    box-shadow: 0 0 5px rgba(0,0,0,0.5);
}
input[type=range]::-webkit-slider-runnable-track {
    width: 100%;
    height: 4px;
    cursor: pointer;
    background: #555;
    border-radius: 2px;
}
input[type=range]::-moz-range-thumb {
    height: 16px;
    width: 16px;
    border: none;
    border-radius: 50%;
    background: #e74c3c;
    cursor: pointer;
    box-shadow: 0 0 5px rgba(0,0,0,0.5);
}
input[type=range]::-moz-range-track {
    width: 100%;
    height: 4px;
    cursor: pointer;
    background: #555;
    border-radius: 2px;
}

/* Status Bar & Action Buttons */
#status-bar-container {
    width: 580px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #333;
    border: 2px solid #444;
    border-radius: 4px;
    padding: 5px 10px;
    box-sizing: border-box;
    margin-bottom: 5px;
}

#status-text {
    font-weight: bold;
    font-size: 16px;
    color: #eee;
}

.status-actions {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: transparent;
    border: none;
    color: #ccc;
    font-size: 20px;
    cursor: pointer;
    padding: 5px;
    border-radius: 4px;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275); /* Bouncy */
    position: relative;
    width: auto;
    margin: 0;
}

/* Hover-only, because a touch browser leaves :hover stuck on the last thing
   tapped — the button would stay enlarged and captioned after you let go. */
@media (hover: hover) {
    .icon-btn:hover {
        background: rgba(255,255,255,0.1);
        transform: scale(1.2);
        color: white;
    }
}

.icon-btn:active {
    transform: scale(0.95);
}

.icon-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    transform: none !important;
}

/* Tooltip */
.icon-btn::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #000;
    color: #fff;
    padding: 4px 8px;
    font-size: 10px;
    border-radius: 4px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    margin-bottom: 5px;
}
@media (hover: hover) {
    .icon-btn:hover::after {
        opacity: 1;
    }
}

/* Captured Pieces Display */
.captured-row {
    display: flex;
    flex-wrap: wrap;
    gap: 1px;
    margin-top: 2px;
    min-height: 15px;
    max-width: 100%;
    overflow: hidden;
}
.captured-piece {
    font-size: 11px;
    line-height: 1;
    opacity: 0.8;
}

/* General Button Animation Update */
button {
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
button:active {
    transform: scale(0.98);
}

/* --- GENERAL INPUTS --- */
label { display: block; font-size: 12px; font-weight: bold; margin-bottom: 5px; color: #bbb; }
select { width: 100%; background: #333; color: white; border: 1px solid #555; padding: 6px; border-radius: 4px; font-size: 12px; margin-bottom: 5px; }
/* Ranges are styled entirely by the custom track/thumb rules further up: the
   border and padding of the generic input styling would draw a box around
   them, and `background` here would override their transparent background. */
input[type=range] { width: 100%; margin-bottom: 5px; }

input[type=text], input[type=password] { 
    width: 100%; 
    background: #333; 
    color: white; 
    border: 1px solid #555; 
    padding: 8px; 
    border-radius: 4px; 
    font-size: 13px; 
    margin-bottom: 5px; 
    box-sizing: border-box; 
}

/* Coordinates inside squares */
.coord-rank { 
    position: absolute; 
    top: 2px; 
    left: 4px; 
    font-size: 11px; 
    color: rgba(255, 255, 255, 0.7); 
    text-shadow: 0 0 2px #000;
    pointer-events: none; 
    font-weight: bold;
}
.coord-file { 
    position: absolute; 
    bottom: 2px; 
    right: 4px; 
    font-size: 11px; 
    color: rgba(255, 255, 255, 0.7); 
    text-shadow: 0 0 2px #000;
    pointer-events: none; 
    font-weight: bold;
}

/* History Navigation */
#history-nav {
    width: 580px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    background: #2a2a2a;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 6px 10px;
    box-sizing: border-box;
    margin-top: 5px;
}

#history-nav .history-info {
    font-size: 12px;
    color: #aaa;
    min-width: 80px;
    text-align: center;
}

#history-nav .icon-btn {
    font-size: 16px;
    padding: 3px 8px;
}

/* Drag Ghost */
#drag-ghost {
    font-size: 32px;
    filter: drop-shadow(0 4px 8px rgba(0,0,0,0.5));
    cursor: grabbing !important;
}

/* Piece being dragged */
.piece-dragging {
    opacity: 0.3 !important;
}

/* Annotation Circles (via CSS for squares) */
.annotated-circle {
    box-shadow: inset 0 0 0 4px rgba(255, 170, 0, 0.9) !important;
}

/* Cursor styles during drag */
.square {
    cursor: pointer;
}

.square .piece {
    cursor: grab;
}

.square:active .piece {
    cursor: grabbing;
}

/* Board needs position relative for SVG overlay */
#board {
    position: relative;
}

/* SVG Annotations */
#annotation-svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

/* Visual indicator when viewing history */
#status-text.viewing-history {
    color: #888 !important;
}
