:root {
            --bg: #f4f1ea;
            --panel: #fffdf8;
            --ink: #25221d;
            --muted: #746b5f;
            --line: #5a4532;
            --board: #d8a968;
            --board-dark: #a56d35;
            --accent: #2563eb;
            --accent-dark: #1d4ed8;
            --good: #16865a;
            --warn: #b56a00;
            --danger: #bd2f2f;
            --shadow: 0 16px 40px rgba(32, 26, 18, 0.16);
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            min-height: 100vh;
            color: var(--ink);
            font-family: "Microsoft JhengHei", "PingFang TC", Arial, sans-serif;
            background:
                linear-gradient(135deg, rgba(37, 99, 235, 0.08), transparent 32%),
                linear-gradient(315deg, rgba(22, 134, 90, 0.1), transparent 34%),
                var(--bg);
            user-select: none;
            -webkit-user-select: none;
        }

        button,
        input,
        select {
            font: inherit;
        }

        button {
            border: 0;
            cursor: pointer;
        }

        button:disabled {
            cursor: not-allowed;
            opacity: 0.62;
        }

        #home-overlay,
        #single-setup-overlay,
        #match-overlay {
            position: fixed;
            inset: 0;
            z-index: 20;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 18px;
            background: rgba(244, 241, 234, 0.86);
            backdrop-filter: blur(8px);
        }

        #single-setup-overlay,
        #match-overlay {
            display: none;
        }

        .home-panel,
        .setup-panel,
        .match-box {
            width: min(420px, 100%);
            background: var(--panel);
            border: 1px solid rgba(90, 69, 50, 0.14);
            border-radius: 8px;
            box-shadow: var(--shadow);
            padding: 24px;
        }

        .home-panel h1,
        .setup-panel h2 {
            margin: 0 0 18px;
            text-align: center;
            font-size: 2rem;
            letter-spacing: 0;
        }

        .mode-button,
        .primary-button,
        .secondary-button,
        .danger-button,
        .chip-button {
            min-height: 44px;
            border-radius: 8px;
            font-weight: 700;
            transition: transform 0.16s ease, background 0.16s ease, box-shadow 0.16s ease;
        }

        .mode-button,
        .primary-button {
            width: 100%;
            color: #fff;
            background: var(--accent);
        }

        .mode-button:hover,
        .primary-button:hover {
            background: var(--accent-dark);
            transform: translateY(-1px);
        }

        .mode-stack {
            display: grid;
            gap: 10px;
        }

        .join-row {
            display: grid;
            grid-template-columns: minmax(0, 1fr) 96px;
            gap: 8px;
        }

        .join-row input,
        .setup-select {
            width: 100%;
            min-height: 44px;
            border: 1px solid rgba(90, 69, 50, 0.25);
            border-radius: 8px;
            padding: 0 12px;
            background: #fff;
            color: var(--ink);
        }

        .setup-grid {
            display: grid;
            gap: 16px;
        }

        .field-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 700;
            color: var(--muted);
        }

        .segmented {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
        }

        .chip-button {
            background: #ede6dc;
            color: var(--ink);
        }

        .chip-button.active {
            color: #fff;
            background: var(--line);
            box-shadow: 0 6px 14px rgba(90, 69, 50, 0.24);
        }

        .setup-actions,
        .top-actions {
            display: flex;
            gap: 10px;
        }

        .secondary-button {
            flex: 1;
            background: #e9e1d6;
            color: var(--ink);
        }

        .danger-button {
            background: var(--danger);
            color: #fff;
        }

        .page {
            width: min(1120px, 100%);
            margin: 0 auto;
            padding: 18px;
            display: grid;
            gap: 14px;
        }

        .topbar {
            display: grid;
            grid-template-columns: auto minmax(0, 1fr) auto;
            gap: 14px;
            align-items: center;
        }

        .title-block h1 {
            margin: 0;
            font-size: 1.55rem;
        }

        .title-block p {
            margin: 4px 0 0;
            color: var(--muted);
            font-size: 0.95rem;
        }

        .status-card {
            min-height: 48px;
            display: flex;
            align-items: center;
            padding: 10px 14px;
            border-radius: 8px;
            background: var(--panel);
            border-left: 4px solid var(--accent);
            box-shadow: 0 8px 24px rgba(32, 26, 18, 0.08);
            font-weight: 700;
        }

        .game-layout {
            display: grid;
            grid-template-columns: minmax(300px, 1fr) 320px;
            gap: 18px;
            align-items: start;
        }

        .board-shell {
            width: min(100%, 640px);
            justify-self: center;
            padding: clamp(10px, 2vw, 18px);
            border-radius: 8px;
            background: linear-gradient(145deg, var(--board-dark), #d2a268 48%, #b77b3e);
            box-shadow: var(--shadow), inset 0 0 18px rgba(58, 36, 16, 0.28);
        }

        .canvas-wrap {
            position: relative;
            aspect-ratio: 1 / 1;
            width: 100%;
            background: var(--board);
            border: 2px solid rgba(70, 45, 24, 0.78);
            border-radius: 4px;
            overflow: hidden;
        }

        #chess-board {
            width: 100%;
            height: 100%;
            display: block;
            cursor: pointer;
        }

        .side-panel {
            display: grid;
            gap: 12px;
        }

        .info-panel,
        .history-panel {
            background: var(--panel);
            border: 1px solid rgba(90, 69, 50, 0.12);
            border-radius: 8px;
            padding: 14px;
            box-shadow: 0 8px 24px rgba(32, 26, 18, 0.08);
        }

        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
        }

        .badge {
            min-height: 42px;
            padding: 8px 10px;
            border-radius: 8px;
            background: #efe8dd;
            color: var(--muted);
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
            font-weight: 700;
        }

        .badge.good {
            color: #0f6844;
            background: #dcf2e8;
        }

        .badge.warn {
            color: #8a5200;
            background: #fff0d7;
        }

        .panel-actions {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
            margin-top: 12px;
        }

        .end-actions {
            display: grid;
            grid-template-columns: 1fr;
            gap: 8px;
            margin-top: 12px;
            padding-top: 12px;
            border-top: 1px solid rgba(90, 69, 50, 0.14);
        }

        .end-actions[hidden] {
            display: none;
        }

        .history-panel h2 {
            margin: 0 0 10px;
            font-size: 1rem;
        }

        #history-list {
            height: 280px;
            overflow-y: auto;
            display: grid;
            align-content: start;
            gap: 6px;
            color: var(--muted);
            font-size: 0.92rem;
        }

        .history-item {
            display: flex;
            justify-content: space-between;
            gap: 8px;
            padding: 6px 8px;
            border-radius: 6px;
            background: #f6f0e8;
            color: var(--ink);
        }

        .empty-history {
            height: 100%;
            display: grid;
            place-items: center;
            color: #968c7e;
            text-align: center;
        }

        .spinner {
            width: 34px;
            height: 34px;
            margin: 16px auto 0;
            border: 4px solid #d7dcec;
            border-top-color: var(--accent);
            border-radius: 50%;
            animation: spin 0.9s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        @media (max-width: 820px) {
            .page {
                padding: 10px;
            }

            .topbar {
                grid-template-columns: 1fr;
            }

            .top-actions {
                order: -1;
            }

            .game-layout {
                grid-template-columns: 1fr;
            }

            .side-panel {
                grid-template-columns: 1fr;
            }

            #history-list {
                height: 180px;
            }
        }

        @media (max-width: 520px) {
            .home-panel,
            .setup-panel,
            .match-box {
                padding: 18px;
            }

            .join-row,
            .panel-actions,
            .info-grid {
                grid-template-columns: 1fr;
            }

            .setup-actions,
            .top-actions {
                flex-direction: column;
            }
        }
