/* Base layout ------------------------------------------------------------- */
* {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            min-height: 100vh;
            padding: 16px;
            font-family: Arial, Helvetica, sans-serif;
            background: #181818;
            color: #f0f0f0;
        }

        main {
            width: 100%;
            min-height: calc(100vh - 32px);
            margin: 0;
            display: grid;
            grid-template-rows: auto auto auto auto 1fr auto;
            gap: 12px;
        }

        h1 {
            margin-top: 0;
            margin-bottom: 8px;
        }

        h2 {
            margin-top: 0;
            font-size: 20px;
        }

        .description {
            margin-top: 0;
            margin-bottom: 0;
            color: #bdbdbd;
            line-height: 1.5;
        }

/* Camera controls --------------------------------------------------------- */
.controls {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 0;
        }

        button,
        select {
            min-height: 42px;
            padding: 10px 16px;
            border: 1px solid #555;
            border-radius: 6px;
            font-size: 16px;
        }

        select {
            min-width: 260px;
            background: #292929;
            color: #f0f0f0;
        }

        button {
            cursor: pointer;
            background: #333;
            color: #f0f0f0;
        }

        button:hover {
            background: #444;
        }

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

        .start-button {
            background: #256b36;
        }

        .start-button:hover {
            background: #2f8245;
        }

        .stop-button {
            background: #7b2929;
        }

        .stop-button:hover {
            background: #963232;
        }

        .status {
            margin-bottom: 0;
            padding: 12px 14px;
            border: 1px solid #444;
            border-radius: 6px;
            background: #252525;
        }

        .status.ok {
            border-color: #3d8e4d;
            color: #87e498;
        }

        .status.error {
            border-color: #a23c3c;
            color: #ff8a8a;
        }

        .status.warning {
            border-color: #a17a2b;
            color: #ffd166;
        }

/* Tabs ------------------------------------------------------------------- */
.tabs {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
            align-items: flex-start;
        }

        .tab-button {
            flex: 0 0 auto;
            width: auto;
            height: 38px;
            min-height: 38px;
            line-height: 1;
            white-space: nowrap;
            border-color: #444;
            background: #242424;
        }

        .tab-button.active {
            border-color: #4aa564;
            background: #256b36;
            color: #fff;
        }

        .tab-panel {
            display: none;
            min-height: 0;
        }

        .tab-panel.active {
            display: grid;
            gap: 12px;
            min-height: 0;
        }

/* Detection threshold controls ------------------------------------------- */
.sensitivity-panel {
            margin-bottom: 0;
            padding: 16px;
            border: 1px solid #3e3e3e;
            border-radius: 8px;
            background: #222;
        }

        .sensitivity-panel h2 {
            margin-bottom: 14px;
        }

        .sensitivity-grid {
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 16px;
        }

        .hand-settings {
            padding: 14px;
            border: 1px solid #3b3b3b;
            border-radius: 8px;
            background: #252525;
        }

        .hand-settings h3 {
            margin: 0 0 12px;
            font-size: 18px;
        }

        .setting-row {
            display: grid;
            grid-template-columns: 1fr auto;
            gap: 10px;
            align-items: center;
            margin-top: 12px;
        }

        .setting-row label {
            color: #d7d7d7;
            font-size: 14px;
        }

        .setting-value {
            min-width: 42px;
            text-align: right;
            color: #87e498;
            font-weight: bold;
        }

        input[type="range"] {
            grid-column: 1 / -1;
            width: 100%;
            accent-color: #4aa564;
        }

/* Video preview and analysis output -------------------------------------- */
.view-card {
            padding: 16px;
            border: 1px solid #3e3e3e;
            border-radius: 10px;
            background: #222;
        }

        .processed-card {
            display: grid;
            grid-template-columns: minmax(0, 1fr) minmax(340px, 420px);
            grid-template-rows: auto minmax(0, 1fr);
            gap: 16px;
            align-items: stretch;
            min-height: 0;
        }

        .processed-card h2 {
            grid-column: 1 / -1;
        }

        .hands-card {
            display: grid;
            grid-template-columns: minmax(0, 1fr) minmax(340px, 420px);
            grid-template-rows: auto minmax(0, 1fr);
            gap: 16px;
            min-height: 0;
        }

        .hands-card h2 {
            grid-column: 1 / -1;
        }

        .processed-media {
            display: grid;
            grid-template-rows: minmax(0, 1fr) auto;
            min-width: 0;
            min-height: 0;
        }

        #processedImage {
            display: block;
            width: 100%;
            height: 100%;
            min-height: 0;
            background: #000;
            border-radius: 8px;
            object-fit: contain;
        }

        .processed-stream {
            display: block;
            width: 100%;
            height: 100%;
            min-height: 0;
            background: #000;
            border-radius: 8px;
            object-fit: contain;
        }

        #video {
            display: none;
        }

        .view-description {
            margin-top: 10px;
            margin-bottom: 0;
            color: #aaa;
            font-size: 14px;
            line-height: 1.4;
        }

        .analysis-panel {
            margin-top: 0;
            padding: 12px;
            border: 1px solid #383838;
            border-radius: 8px;
            background: #1d1d1d;
            min-height: 0;
            overflow: hidden;
        }

        .analysis-panel h3 {
            margin: 0 0 10px;
            font-size: 16px;
        }

        .analysis-results {
            display: grid;
            grid-template-columns: 1fr;
            gap: 10px;
            max-height: calc(100vh - 440px);
            overflow-y: auto;
            overscroll-behavior: contain;
        }

        .analysis-card {
            min-height: 222px;
            padding: 10px;
            border: 1px solid #343434;
            border-radius: 6px;
            background: #252525;
        }

        .analysis-card strong {
            display: block;
            margin-bottom: 8px;
        }

        .analysis-line {
            display: flex;
            justify-content: space-between;
            gap: 12px;
            min-height: 24px;
            padding: 3px 0;
            color: #cfcfcf;
            font-size: 14px;
        }

        .analysis-line span:last-child {
            color: #f0f0f0;
            font-weight: bold;
            text-align: right;
        }

        .state-on {
            color: #87e498 !important;
        }

        .state-off {
            color: #ffb36b !important;
        }

/* Camera tic-tac-toe ------------------------------------------------------ */
.game-card {
            display: grid;
            grid-template-columns: minmax(0, 1fr) minmax(260px, 340px);
            grid-template-rows: auto minmax(0, 1fr);
            gap: 16px;
            min-height: 0;
        }

        .game-card h2 {
            grid-column: 1 / -1;
        }

        .game-stage {
            position: relative;
            min-width: 0;
            min-height: 0;
            background: #000;
            border-radius: 8px;
            overflow: hidden;
        }

.game-board {
            position: absolute;
            top: 8%;
            right: 28%;
            bottom: 8%;
            left: 6%;
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-template-rows: repeat(3, 1fr);
            border: 3px solid rgba(255, 255, 255, 0.85);
            pointer-events: none;
        }

        .game-cell {
            display: grid;
            place-items: center;
            border: 2px solid rgba(255, 255, 255, 0.75);
            color: #fff;
            font-size: clamp(36px, 6vw, 92px);
            font-weight: bold;
            text-shadow: 0 2px 6px #000;
            user-select: none;
        }

        .game-cell.target,
        .game-cell.drop-target {
            background: rgba(74, 165, 100, 0.35);
        }

        .game-tray-zone {
            position: absolute;
            top: 8%;
            right: 6%;
            bottom: 8%;
            width: 16%;
            display: grid;
            place-items: start center;
            padding-top: 12px;
            border: 2px dashed rgba(255, 209, 102, 0.72);
            border-radius: 8px;
            background: rgba(20, 20, 20, 0.42);
            color: #ffd166;
            font-size: 13px;
            font-weight: bold;
            pointer-events: none;
            text-shadow: 0 2px 6px #000;
        }

        .game-camera-piece {
            position: absolute;
            left: 86%;
            top: 50%;
            display: grid;
            place-items: center;
            width: clamp(44px, 7vw, 82px);
            aspect-ratio: 1;
            border: 3px solid #ffd166;
            border-radius: 8px;
            background: rgba(48, 48, 48, 0.92);
            color: #fff;
            font-size: clamp(30px, 5vw, 62px);
            font-weight: bold;
            line-height: 1;
            pointer-events: none;
            text-shadow: 0 2px 6px #000;
            transform: translate(-50%, -50%);
            transition: border-color 120ms ease, background 120ms ease;
        }

        .game-camera-piece.pickup-target {
            border-color: #87e498;
            background: rgba(37, 107, 54, 0.94);
        }

        .game-pointer {
            position: absolute;
            width: 18px;
            height: 18px;
            border: 3px solid #ffd166;
            border-radius: 50%;
            transform: translate(-50%, -50%);
            opacity: 0;
            pointer-events: none;
            box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.35);
        }

        .game-pointer.visible {
            opacity: 1;
        }

        .game-panel {
            display: grid;
            align-content: start;
            gap: 12px;
            padding: 12px;
            border: 1px solid #383838;
            border-radius: 8px;
            background: #1d1d1d;
        }

        .game-piece-tray {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
            padding: 10px;
            border: 1px solid #343434;
            border-radius: 6px;
            background: #252525;
        }

        .game-piece-tray span {
            color: #aaa;
            font-size: 14px;
        }

        .game-panel-piece {
            display: grid;
            place-items: center;
            width: 72px;
            height: 72px;
            border: 2px solid #ffd166;
            border-radius: 8px;
            background: #303030;
            color: #fff;
            font-size: 44px;
            font-weight: bold;
            line-height: 1;
            text-shadow: 0 2px 6px #000;
            user-select: none;
        }

        .game-status {
            display: flex;
            justify-content: space-between;
            gap: 12px;
            padding: 10px;
            border: 1px solid #343434;
            border-radius: 6px;
            background: #252525;
        }

        .game-panel button {
            justify-self: start;
            width: auto;
        }

/* Transfer counter -------------------------------------------------------- */
.transfer-panel {
            display: grid;
            grid-template-columns: minmax(160px, 220px) 1fr auto;
            gap: 14px;
            align-items: stretch;
            margin-top: 0;
            padding: 14px;
            border: 1px solid #3e3e3e;
            border-radius: 8px;
            background: #222;
        }

        .transfer-count-box {
            padding: 12px;
            border: 1px solid #343434;
            border-radius: 6px;
            background: #252525;
        }

        .transfer-count-box span {
            display: block;
            color: #aaa;
            font-size: 13px;
        }

        .transfer-count-box strong {
            display: block;
            margin-top: 4px;
            color: #87e498;
            font-size: 34px;
            line-height: 1;
        }

        .transfer-events {
            min-height: 58px;
            max-height: 96px;
            overflow-y: auto;
            overscroll-behavior: contain;
            padding: 10px 12px;
            border: 1px solid #343434;
            border-radius: 6px;
            background: #1d1d1d;
            color: #d0d0d0;
            font-size: 14px;
            line-height: 1.45;
        }

        .transfer-events div + div {
            margin-top: 4px;
            color: #aaa;
        }

        .reset-button {
            min-width: 120px;
            align-self: center;
        }

/* Connection statistics --------------------------------------------------- */
.stats {
            display: flex;
            gap: 16px;
            flex-wrap: wrap;
            margin-top: 0;
            padding: 14px;
            border-radius: 8px;
            background: #252525;
        }

        .stat {
            min-width: 160px;
        }

        .stat-label {
            display: block;
            margin-bottom: 4px;
            color: #aaa;
            font-size: 13px;
        }

        .stat-value {
            font-size: 17px;
            font-weight: bold;
        }

        canvas {
            display: none;
        }

        @media (max-width: 850px) {
            .processed-card {
                grid-template-columns: 1fr;
                grid-template-rows: auto auto auto;
            }

            .hands-card,
            .game-card {
                grid-template-columns: 1fr;
                grid-template-rows: auto auto auto;
            }

            .processed-card h2,
            .hands-card h2,
            .game-card h2 {
                grid-column: auto;
            }

            select {
                width: 100%;
            }

            button {
                flex: 1;
            }

            .tab-button {
                flex: 0 0 auto;
            }

            .sensitivity-grid,
            .analysis-results,
            .transfer-panel {
                grid-template-columns: 1fr;
            }

            .reset-button {
                width: 100%;
            }
        }
