/**
 * Penguin chess viewer – single stylesheet (theme via class overrides).
 * Default palette inspired by devonchess.org game viewer (green/cream),
 * refreshed with a modern, contemporary surface, soft shadows and rounded
 * controls. User-configurable colours are still injected through the
 * --penguin-* custom properties set inline by the renderer.
 */

.penguin-viewer {
    --penguin-light: #f0f5e8;
    --penguin-dark: #6b8e4e;
    --penguin-border: #e3e8ee;
    --penguin-text: #1f2933;
    --penguin-muted: #647084;
    --penguin-accent: #2f6f4f;
    --penguin-panel-bg: #ffffff;
    --penguin-surface: #ffffff;
    --penguin-surface-soft: #f6f8f5;
    --penguin-white-piece: #ffffff;
    --penguin-black-piece: #1a1a1a;
    --penguin-control-bg: #ffffff;
    --penguin-control-active: #6b8e4e;
    --penguin-ring: rgba(47, 111, 79, 0.35);
    --penguin-radius: 16px;
    --penguin-radius-sm: 10px;
    --penguin-shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 8px 24px rgba(16, 24, 40, 0.07);
    --penguin-shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.08);
    --penguin-board-width: 75%;
    --penguin-annotation-height: 400px;
    margin: 1.5rem 0;
    padding: 1.25rem;
    font-family: inherit;
    color: var(--penguin-text);
    background: var(--penguin-surface);
    border: 1px solid var(--penguin-border);
    border-radius: var(--penguin-radius);
    box-shadow: var(--penguin-shadow);
    max-width: 100%;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
}

.penguin-viewer *,
.penguin-viewer *::before,
.penguin-viewer *::after {
    box-sizing: border-box;
}

/* Top bar: action buttons on the first row; in multi-game mode the game selector
   sits on a second row beneath them. Logo stays top-right. */
.penguin-topbar {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.penguin-topbar-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
}

/* Logo right-aligned within the top bar. Sized by height so it stands taller
   than the row of action buttons (~2rem each, so 3 buttons ≈ 6rem); width scales
   with the image's aspect ratio and is capped so it never dominates the row. */
.penguin-logo-wrap {
    flex: 0 0 auto;
    max-width: 40%;
    line-height: 0;
}

.penguin-logo {
    height: 7rem;
    width: auto;
    max-width: 100%;
    object-fit: contain;
    display: block;
}

/* Shared pill-style control used by the top-bar toggles. */
.penguin-help-toggle,
.penguin-details-toggle,
.penguin-flip-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.5rem 0.9rem;
    font-size: 0.9rem;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    color: var(--penguin-text);
    background: var(--penguin-control-bg);
    border: 1px solid var(--penguin-border);
    border-radius: 999px;
    box-shadow: var(--penguin-shadow-sm);
    transition: background-color 0.15s ease, color 0.15s ease,
        border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}

.penguin-help-toggle:hover,
.penguin-details-toggle:hover,
.penguin-flip-toggle:hover {
    border-color: var(--penguin-control-active);
    transform: translateY(-1px);
    box-shadow: var(--penguin-shadow);
}

.penguin-help-toggle:focus-visible,
.penguin-details-toggle:focus-visible,
.penguin-flip-toggle:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--penguin-ring);
}

.penguin-help-toggle.is-active,
.penguin-flip-toggle.is-active {
    background: var(--penguin-control-active);
    color: #fff;
    border-color: var(--penguin-control-active);
}

/* Icon shown to the right of each top-bar button label. Sized in em so it
   tracks the button text and inherits its colour through currentColor. */
.penguin-btn-icon {
    flex: 0 0 auto;
    width: 1.15em;
    height: 1.15em;
    display: block;
}

.penguin-help {
    margin-top: 0.75rem;
    padding: 1rem 1.25rem;
    background: var(--penguin-surface-soft);
    border: 1px solid var(--penguin-border);
    border-radius: var(--penguin-radius-sm);
    font-size: 0.9rem;
    line-height: 1.6;
}

.penguin-help.penguin-help-hidden {
    display: none;
}

.penguin-help-heading {
    margin: 1rem 0 0.35rem;
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--penguin-accent);
}

.penguin-help-heading:first-child {
    margin-top: 0;
}

.penguin-help p {
    margin: 0.35rem 0;
}

.penguin-help-list {
    margin: 0.35rem 0;
    padding-left: 1.25rem;
}

.penguin-help-list li {
    margin: 0.2rem 0;
}

.penguin-help code {
    padding: 0.1rem 0.35rem;
    background: var(--penguin-light);
    border-radius: 6px;
    font-size: 0.85em;
}

.penguin-help-credits {
    margin-top: 0.75rem;
    font-size: 0.85rem;
    color: var(--penguin-muted);
}

.penguin-game-select-wrap {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    flex-basis: 100%;
    width: fit-content;
    max-width: 100%;
}

.penguin-game-select-label {
    font-size: 0.9rem;
    font-weight: 600;
}

/* Hidden sizer text sets the dropdown width to the longest option label. */
.penguin-game-select-field {
    display: inline-grid;
    vertical-align: top;
}

.penguin-game-select-field > .penguin-game-select,
.penguin-game-select-field > .penguin-game-select-sizer {
    grid-area: 1 / 1;
    width: 100%;
    min-width: 0;
}

.penguin-game-select-sizer {
    visibility: hidden;
    white-space: nowrap;
    padding: 0.55rem 2rem 0.55rem 0.75rem;
    font: inherit;
    font-size: 0.9rem;
    border: 1px solid transparent;
}

.penguin-game-select {
    padding: 0.55rem 2rem 0.55rem 0.75rem;
    font: inherit;
    font-size: 0.9rem;
    color: var(--penguin-text);
    background-color: var(--penguin-control-bg);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23647084' d='M6 8 0 0h12z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    border: 1px solid var(--penguin-border);
    border-radius: var(--penguin-radius-sm);
    box-shadow: var(--penguin-shadow-sm);
    cursor: pointer;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.penguin-game-select:hover {
    border-color: var(--penguin-control-active);
}

.penguin-game-select:focus-visible {
    outline: none;
    border-color: var(--penguin-control-active);
    box-shadow: 0 0 0 3px var(--penguin-ring);
}

.penguin-game-hidden {
    display: none;
}

.penguin-details {
    margin-top: 0.5rem;
    margin-bottom: 0.9rem;
    padding: 1rem 1.25rem;
    background: var(--penguin-surface-soft);
    border: 1px solid var(--penguin-border);
    border-radius: var(--penguin-radius-sm);
}

.penguin-details.penguin-details-hidden,
.penguin-details-games.penguin-details-hidden {
    display: none;
}

.penguin-details-list {
    display: grid;
    grid-template-columns: minmax(auto, max-content) minmax(0, 1fr);
    gap: 0.4rem 1.25rem;
    margin: 0;
}

.penguin-details-list dt {
    font-weight: 600;
    color: var(--penguin-muted);
    margin: 0;
    min-width: 0;
    overflow-wrap: break-word;
    word-break: break-word;
}

.penguin-details-list dd {
    margin: 0;
    min-width: 0;
    overflow-wrap: break-word;
    word-break: break-word;
}

.penguin-board-wrap {
    display: flex;
    justify-content: center;
    margin: 0.9rem 0;
    width: 100%;
}

.penguin-board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: var(--penguin-board-width, 75%);
    max-width: 100%;
    aspect-ratio: 1 / 1;
    min-width: 200px;
    min-height: 200px;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 6px 20px rgba(16, 24, 40, 0.16), 0 0 0 1px rgba(16, 24, 40, 0.06);
    user-select: none;
}

@supports not (aspect-ratio: 1 / 1) {
    .penguin-board-wrap {
        position: relative;
        width: var(--penguin-board-width, 75%);
        max-width: 100%;
        margin-left: auto;
        margin-right: auto;
    }

    .penguin-board-wrap::before {
        content: '';
        display: block;
        padding-top: 100%;
    }

    .penguin-board {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        min-width: 0;
        min-height: 0;
    }
}

.penguin-square {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
}

.penguin-square-light {
    background-color: var(--penguin-light);
}

.penguin-square-dark {
    background-color: var(--penguin-dark);
}

.penguin-piece {
    display: block;
    width: 85%;
    height: 85%;
    flex-shrink: 0;
    pointer-events: none;
}

.penguin-piece-svg {
    width: 100%;
    height: 100%;
    display: block;
    filter: drop-shadow(0 1px 1.5px rgba(0, 0, 0, 0.35));
}

/* Theme: blue board (override on .penguin-viewer.penguin-theme-blue) */
.penguin-viewer.penguin-theme-blue {
    --penguin-light: #dee3e6;
    --penguin-dark: #8ca2ad;
}

/* Theme: brown wood */
.penguin-viewer.penguin-theme-brown {
    --penguin-light: #f0d9b5;
    --penguin-dark: #b58863;
}

.penguin-eval-wrap {
    display: flex;
    justify-content: center;
    width: 100%;
    margin: 0.6rem 0;
}

/* Sliding evaluation bar: white (left) vs black (right). Width matches the
   board; height is half the control buttons' min-height (2.75rem). */
.penguin-eval-bar {
    position: relative;
    width: var(--penguin-board-width, 75%);
    max-width: 100%;
    height: 1.375rem;
    border-radius: 999px;
    background: #1f2933;
    overflow: hidden;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.25);
    box-sizing: border-box;
}

.penguin-eval-white {
    height: 100%;
    width: 50%;
    background: #f5f7fa;
    box-sizing: border-box;
    transition: width 0.3s ease;
}

/* Evaluation value, centred over the bar. Two layers are stacked so the
   number stays readable on any background: the light layer shows over the
   black (right) region, the dark layer is clipped to the white (left)
   region and painted on top of it. */
.penguin-eval-score {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    margin: 0;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    pointer-events: none;
    user-select: none;
}

.penguin-eval-score-light {
    color: #f5f7fa;
}

.penguin-eval-score-dark {
    color: #1f2933;
    -webkit-clip-path: inset(0 50% 0 0);
    clip-path: inset(0 50% 0 0);
}

.penguin-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    margin: 0.75rem 0 1rem;
}

.penguin-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.75rem;
    min-height: 2.75rem;
    padding: 0.35rem 0.65rem;
    font-size: 1.1rem;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    color: var(--penguin-text);
    background: var(--penguin-control-bg);
    border: 1px solid var(--penguin-border);
    border-radius: var(--penguin-radius-sm);
    box-shadow: var(--penguin-shadow-sm);
    transition: background-color 0.15s ease, color 0.15s ease,
        border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}

.penguin-btn:hover {
    border-color: var(--penguin-control-active);
    transform: translateY(-1px);
    box-shadow: var(--penguin-shadow);
}

.penguin-btn:active {
    transform: translateY(0);
}

.penguin-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--penguin-ring);
}

.penguin-btn-autoplay.is-playing {
    background: var(--penguin-control-active);
    color: #fff;
    border-color: var(--penguin-control-active);
}

.penguin-annotation-wrap {
    height: var(--penguin-annotation-height, 400px);
    overflow: hidden;
    border: 1px solid var(--penguin-border);
    border-radius: var(--penguin-radius-sm);
    background: var(--penguin-panel-bg);
}

.penguin-annotation-games {
    height: 100%;
}

.penguin-annotation {
    height: 100%;
    overflow-y: auto;
    padding: 1rem 1.25rem;
    font-size: clamp(0.85rem, 2.5vw, 1rem);
    line-height: 1.7;
    scrollbar-width: thin;
    scrollbar-color: var(--penguin-border) transparent;
}

.penguin-annotation::-webkit-scrollbar {
    width: 10px;
}

.penguin-annotation::-webkit-scrollbar-thumb {
    background: var(--penguin-border);
    border: 3px solid transparent;
    border-radius: 999px;
    background-clip: padding-box;
}

.penguin-move-num {
    color: var(--penguin-muted);
    font-weight: 600;
}

.penguin-move {
    cursor: default;
    padding: 0.05em 0.2em;
    border-radius: 6px;
    transition: background-color 0.12s ease;
}

.penguin-move-current {
    font-weight: 700;
    color: #fff;
    background: var(--penguin-control-active);
}

.penguin-comment {
    color: var(--penguin-muted);
    font-style: italic;
}

.penguin-nag {
    color: var(--penguin-accent);
    font-weight: 700;
}

.penguin-variation {
    display: inline;
    margin: 0 0.15em;
    padding: 0.1em 0.45em;
    font: inherit;
    font-size: 0.95em;
    color: var(--penguin-accent);
    background: transparent;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    text-align: left;
    transition: background-color 0.12s ease;
}

.penguin-variation:hover {
    background: var(--penguin-surface-soft);
}

.penguin-variation:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--penguin-ring);
}

.penguin-error {
    color: #b32d2e;
    padding: 0.75rem 1rem;
    background: rgba(179, 45, 46, 0.06);
    border-left: 4px solid #b32d2e;
    border-radius: var(--penguin-radius-sm);
}

@media (max-width: 480px) {
    .penguin-viewer {
        padding: 1rem;
    }

    .penguin-board {
        width: 100%;
    }

    .penguin-eval-bar {
        width: 100%;
        height: 1.25rem;
    }

    .penguin-btn {
        min-width: 2.5rem;
        min-height: 2.5rem;
        font-size: 1rem;
    }

    .penguin-details-list {
        grid-template-columns: minmax(0, 1fr);
        gap: 0 0;
    }

    .penguin-details-list dt {
        margin-top: 0.5rem;
    }

    .penguin-details-list dt:first-child {
        margin-top: 0;
    }

    .penguin-details-list dd {
        margin-bottom: 0.15rem;
    }

    /* Keep Help / Details / Flip on the left of the logo (same row as desktop). */
    .penguin-topbar-actions {
        flex: 1 1 auto;
        min-width: 0;
    }

    .penguin-logo-wrap {
        max-width: 38%;
    }

    .penguin-logo {
        height: 5rem;
    }

    .penguin-game-select-wrap {
        width: 100%;
    }

    .penguin-game-select-field {
        flex: 1 1 auto;
        min-width: 0;
        display: block;
    }

    .penguin-game-select-sizer {
        display: none;
    }

    .penguin-game-select-field > .penguin-game-select {
        width: 100%;
    }

    /* Height is tuned by JS to the remaining viewport; keep a sensible floor. */
    .penguin-annotation-wrap {
        min-height: 100px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .penguin-viewer * {
        transition: none !important;
    }
}
