* {
    padding: 0 !important;
    box-sizing: border-box !important;
}

*:not(#controls):not(#mode-buttons) {
    margin: 0 !important;
}

html {
    width: 100%;
    height: 100%;
    overflow-x: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
    background: #1a1a1a;
    color: #fff;
    overflow: hidden;
    touch-action: manipulation;
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
}

#container {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
}

#board-container {
    width: 100%;
    height: min(100vw, 60vh);  /* Match the board size exactly */
    display: flex;
    align-items: flex-start;
    justify-content: center;
    background: #2a2a2a;
    position: relative;
    overflow: hidden;
}

#board {
    width: min(100vw, 60vh);
    height: min(100vw, 60vh);
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    background: #f0d9b5;
    aspect-ratio: 1 / 1;
}

.square {
    position: relative;
    cursor: pointer;
    user-select: none;
    aspect-ratio: 1 / 1;
}

.square.light {
    background: #f0d9b5;
}

.square.dark {
    background: #b58863;
}

.square.drag-over {
    background: #829769 !important;
    box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.4) !important;
    transition: background 0.1s ease, box-shadow 0.1s ease;
}

.square.legal-move::after {
    content: '';
    position: absolute;
    width: 30%;
    height: 30%;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.2);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.square.selected {
    box-shadow: inset 0 0 0 3px rgba(255, 255, 0, 0.8);
}

/* Custom square markings from PGN tokens */
.square.custom-mark {
    background-color: var(--mark-color) !important;
    opacity: 0.7;
}

.piece {
    width: 95%;
    height: 95%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: auto;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
}

.piece svg {
    width: 100%;
    height: 100%;
    display: block;
    pointer-events: none; 
}

.piece.dragging {
    opacity: 0.5;
    cursor: grabbing;
}

/* Piece color and shadow styling */
.piece.white {
    color: #fff;
    text-shadow: 0 0 3px #000, 0 0 2px #000;
}

.piece.black {
    color: #000;
    text-shadow: 0 0 2px #fff, 0 0 1px #fff;
}

/* Touch drag state */
.piece.dragging-touch {
    opacity: 0.3;
}

@media (min-aspect-ratio: 1/1) {
    .piece {
        font-size: 9vh;
    }
}

#controls {
    height: 5vh;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    width: min(100vw, 60vh);
    margin-left: auto;
    margin-right: auto;
    background: #2a2a2a;
    overflow: hidden;
}

.arrow-btn {
    width: 40px;
    min-width: 40px;
    height: 40px;
    margin: 4px 0;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    background: #444;
    border: none;
    color: #fff;
    cursor: pointer;
    border-radius: 5px;
    transition: background 0.2s ease, opacity 0.2s ease;
}

.arrow-btn:active {
    background: #555;
}

.arrow-btn:disabled {
    background: #333;
    color: #666;
    cursor: not-allowed;
    opacity: 0.5;
}

.arrow-btn.active {
    background: #555;
    color: #fff;
}

#new-btn {
    font-size: 20px;
    font-weight: 900;
    line-height: 1;
}

#new-btn svg {
    width: 20px;
    height: 20px;
    display: block;
    color: #fff;
}

#messages {
    height: 20vh;
    padding: 2vh 3vw;
    background: #1a1a1a;
    overflow-y: auto;
    font-size: 3.5vw;
    line-height: 1.4;
}

#engine-lines {
    height: 10vh;
    padding: 2vh 3vw;
    background: #0a0a0a;
    overflow-y: auto;
    font-size: 3.5vw;
    line-height: 1.6;
    font-family: 'Courier New', monospace;
}

.engine-line {
    margin-bottom: 1vh;
}

#mode-buttons {
    height: 5vh;
    display: flex;
    background: #2a2a2a;
    width: min(100vw, 60vh);
    margin-left: auto;
    margin-right: auto;
}

.mode-btn {
    flex: 1;
    height: 40px;
    margin: 4px 0;
    background: #333;
    border: none;
    color: #aaa;
    font-size: 10px;
    cursor: pointer;
    border-radius: 5px;
    min-width: 0;
    padding: 4px;
    white-space: nowrap;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.mode-btn svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

.mode-btn span {
    font-size: 12px;
}

.mode-btn:last-child {
    border-right: none;
}

.mode-btn.active {
    background: #555;
    color: #fff;
}

.mode-btn:active {
    background: #444;
}

.square.last-move {
    background-color: rgba(255, 255, 0, 0.41) !important;
}

.square.last-move.light {
    background-color: rgba(255, 255, 0, 0.41) !important;
}

.square.last-move.dark {
    background-color: rgba(0, 30, 0, 0.50) !important;
}

#engine-toggle, #speech-toggle {
    display: inline-block;
    margin-right: 20px;
    margin-top: 10px;
    vertical-align: top; 
}

#engine-toggle.hidden, #speech-toggle.hidden {
    display: none;
}

#toggles-container {
    background: #1a1a1a;
    padding: 10px 3vw;
}

#toggles-container label {
    color: #ccc;
    cursor: pointer;
    user-select: none;
    display: block;
    margin-bottom: 5px;
}

#toggles-container input[type="checkbox"] {
    margin-right: 5px;
}

/* Touch ghost piece during drag */
.touch-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 1000;
    opacity: 0.8;
}

/* Touch preview indicator */
.touch-preview {
    position: fixed;
    pointer-events: none;
    z-index: 1001;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(0, 0, 0, 0.85);
    border: 2px solid #fff;
    border-radius: 12px;
    padding: 8px 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    animation: preview-appear 0.15s ease-out;
}

/* Promotion dialog overlay and components */
.promotion-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.promotion-dialog {
    background-color: #2b2b2b;
    padding: 20px;
    border-radius: 8px;
    display: flex;
    gap: 10px;
}

.promotion-button {
    width: 60px;
    height: 60px;
    cursor: pointer;
    padding: 5px;
    border-radius: 4px;
    transition: background-color 0.2s;
    background-color: transparent;
    border: none;
}

.promotion-button:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* Clickable moves in message window */
.clickable-move {
    cursor: pointer;
    text-decoration: underline;
    color: #fff;
    transition: color 0.2s ease, background-color 0.2s ease;
    padding: 2px 4px;
    border-radius: 3px;
}

/* Variation moves (non-mainline) should be less prominent */
.clickable-move.variation {
    color: #aaa;  /* Same color as metadata header */
}

/* Try variations - thematically significant alternatives */
.clickable-move.try-variation {
    color: #aaa;  /* Same as regular variations for now - structure retained for future experimentation */
}

.clickable-move:hover {
    color: #ffeb3b;
    background-color: rgba(255, 255, 255, 0.1);
}

.clickable-move:active {
    background-color: rgba(255, 255, 255, 0.2);
}

/* Current position indicator */
.clickable-move.current-position {
    background-color: rgba(255, 235, 59, 0.4);
    font-weight: bold;
    border: 1px solid #ffeb3b;
}

.clickable-move.current-position:hover {
    background-color: rgba(255, 235, 59, 0.6);
    color: #fff;
}

@keyframes preview-appear {
    from {
        opacity: 0;
        transform: translate(-50%, 10px);
    }
    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}

.preview-piece {
    width: 50px;
    height: 50px;
    margin-bottom: 4px;
}

.preview-piece svg {
    width: 100%;
    height: 100%;
}

.preview-square {
    font-size: 20px;
    font-weight: bold;
    color: #fff;
    font-family: monospace;
    letter-spacing: 1px;
}
