:root {
    --wood-bg: #c89f65;
    --wood-border: #4a2511;
    --dark-point: #5c2f0a;
    --light-point: #f2d2a2;
    --p1-piece: #f9f9f9;
    --p2-piece: #222222;
    --bg-dark: #2c3e50;
    --panel-bg: #1a252f;
    --accent: #e74c3c;
    --accent2: #3498db;
    --green: #2ecc71;
}

* { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Helvetica Neue', Arial, sans-serif; user-select: none; }

body {
    background-color: var(--bg-dark);
    color: #fff;
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ============ LOBBY ============ */
.lobby-screen {
    position: fixed; inset: 0; z-index: 200;
    background: var(--bg-dark);
    display: flex; justify-content: center; align-items: flex-start;
    padding: max(3.5rem, env(safe-area-inset-top)) 1rem 2rem;
    overflow-y: auto;
}
.lobby-home {
    position: fixed; top: max(0.75rem, env(safe-area-inset-top)); left: 1rem; z-index: 210;
}
.lobby-card {
    background: var(--panel-bg);
    border-radius: 16px;
    padding: 1.5rem;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.6);
    display: flex; flex-direction: column; gap: 1rem;
}
.lobby-logo { font-size: 3rem; text-align: center; }
.lobby-card h1 { text-align: center; font-size: 1.8rem; color: var(--accent); }
.lobby-subtitle { text-align: center; opacity: 0.7; font-size: 0.9rem; }
.lobby-section label { display: block; font-size: 0.82rem; opacity: 0.7; margin-bottom: 0.35rem; font-weight: bold; text-transform: uppercase; letter-spacing: 0.05em; }
.lobby-section input {
    width: 100%; padding: 11px 13px; border-radius: 8px;
    background: rgba(255,255,255,0.08); border: 1px solid rgba(255,255,255,0.15);
    color: #fff; font-size: 1rem; outline: none;
    transition: border-color 0.2s;
    box-sizing: border-box;
}
.lobby-section input:focus { border-color: var(--accent2); }
.lobby-divider { display: flex; flex-direction: column; gap: 0.7rem; }
.lobby-btn {
    padding: 13px; border: none; border-radius: 8px;
    font-size: 1rem; font-weight: bold; cursor: pointer;
    transition: 0.2s; width: 100%; box-sizing: border-box;
}
.lobby-btn.primary { background: var(--accent); color: #fff; }
.lobby-btn.primary:active { background: #c0392b; }
.lobby-or { text-align: center; opacity: 0.4; font-size: 0.85rem; }
.join-row { display: flex; gap: 0.6rem; align-items: stretch; }
.join-row input {
    flex: 1; min-width: 0; padding: 13px 10px; border-radius: 8px;
    background: rgba(255,255,255,0.08); border: 1px solid rgba(255,255,255,0.15);
    color: #fff; font-size: 1.1rem; outline: none; text-transform: uppercase;
    letter-spacing: 0.15em; font-weight: bold; box-sizing: border-box;
}
.join-row input:focus { border-color: var(--accent2); }
.join-row .lobby-btn { width: auto !important; padding: 13px 18px; flex-shrink: 0; background: var(--accent2); color: #fff; white-space: nowrap; }
.lobby-status { text-align: center; color: var(--accent); font-weight: bold; min-height: 1.1rem; font-size: 0.9rem; }
.lobby-code-display {
    background: rgba(46,204,113,0.1); border: 1px solid var(--green);
    border-radius: 10px; padding: 12px; text-align: center;
    font-size: 1rem; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap;
}
.lobby-code-display strong { font-size: 1.8rem; letter-spacing: 0.2em; color: var(--green); }
.copy-btn {
    background: var(--green); color: #fff; border: none; border-radius: 6px;
    padding: 6px 12px; cursor: pointer; font-weight: bold; font-size: 0.85rem;
}
.waiting-msg {
    display: flex; align-items: center; justify-content: center; gap: 10px;
    opacity: 0.8; font-size: 0.9rem;
}
.spinner {
    width: 18px; height: 18px; border: 3px solid rgba(255,255,255,0.2);
    border-top-color: #fff; border-radius: 50%;
    animation: spin 0.8s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.mode-selectors { display: flex; gap: 8px; }
.mode-selectors button {
    flex: 1; padding: 10px; border: none; border-radius: 6px;
    background: #34495e; color: #fff; cursor: pointer; font-weight: bold;
    transition: 0.2s; font-size: 0.9rem; min-width: 0;
}
.mode-selectors button.active { background: var(--accent); }

/* ============ GAME ============ */
.app-container {
    display: flex; width: 100%; height: 100%;
    max-width: 1400px; padding: 1rem; gap: 1rem;
    flex-direction: row; align-items: stretch;
}
.panel { background-color: var(--panel-bg); border-radius: 10px; padding: 1rem; display: flex; flex-direction: column; gap: 1rem; width: 250px; box-shadow: 0 4px 10px rgba(0,0,0,0.5); }
.panel h2 { font-size: 1.2rem; text-align: center; border-bottom: 2px solid #fff; padding-bottom: 0.5rem; }

button { padding: 10px; border: none; border-radius: 5px; cursor: pointer; font-weight: bold; transition: 0.2s; font-size: 1rem; }
.action-btn { background: #3498db; color: white; }
.action-btn:hover:not(:disabled) { background: #2980b9; }
.action-btn:disabled { background: #555; cursor: not-allowed; opacity: 0.7; }
.action-btn.highlight { background: #2ecc71; animation: pulse 1.5s infinite; }
.danger-btn { background: #c0392b; color: white; margin-top: auto; }
.danger-btn:hover { background: #a53125; }
.mode-display { background: rgba(255,255,255,0.08); padding: 10px; border-radius: 6px; text-align: center; font-size: 0.9rem; }
.conn-badge { font-size: 0.8rem; padding: 5px 10px; border-radius: 20px; text-align: center; font-weight: bold; }
.conn-ok { background: rgba(46,204,113,0.2); color: var(--green); }
.conn-warn { background: rgba(231,76,60,0.2); color: var(--accent); }

@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.05); } }

.history-log { flex: 1; overflow-y: auto; background: #111; padding: 10px; border-radius: 5px; font-family: monospace; font-size: 0.9rem; line-height: 1.4; min-height: 100px; }
#scoreBoard { background: #222; padding: 10px; border-radius: 5px; text-align: center; }

.board-container { flex: 3; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.status-bar { display: flex; justify-content: space-between; align-items: center; background: var(--panel-bg); padding: 10px 20px; border-radius: 10px; font-size: 1.2rem; font-weight: bold; }
.dice-display { display: flex; gap: 10px; min-width: 80px; justify-content: center; font-size: 2rem; }

.tavla-board { min-height: 60vh; flex: 1; background-color: var(--wood-bg); border: 15px solid var(--wood-border); border-radius: 10px; display: flex; position: relative; box-shadow: inset 0 0 20px rgba(0,0,0,0.5); overflow: hidden; }
.board-half { flex: 1; display: flex; flex-direction: column; position: relative; z-index: 2; }
.board-bar { width: 40px; background-color: var(--wood-border); border-left: 2px solid #2a1106; border-right: 2px solid #2a1106; display: flex; flex-direction: column; position: relative; z-index: 2; }
.bear-off-zone { width: 60px; background-color: #8c6a43; border-left: 5px solid var(--wood-border); display: flex; flex-direction: column; position: relative; z-index: 2; }
.row { flex: 1; display: flex; position: relative; }
.top-row { flex-direction: row; }
.bottom-row { flex-direction: row; }
.bar-top, .bar-bottom, .bear-off-top, .bear-off-bottom { flex: 1; display: flex; flex-direction: column; align-items: center; padding: 5px 0; overflow: hidden; position: relative; }
.bar-top, .bear-off-top { justify-content: flex-start; }
.bar-bottom, .bear-off-bottom { justify-content: flex-end; }
.bear-off-count { font-size: 10px; font-weight: bold; color: rgba(255,255,255,0.85); background: rgba(0,0,0,0.45); border-radius: 4px; padding: 1px 3px; margin: 2px 0; text-align: center; z-index: 5; flex-shrink: 0; line-height: 1.2; max-width: 100%; }

.point { flex: 1; position: relative; display: flex; flex-direction: column; align-items: center; width: 100%; height: 100%; cursor: pointer; }
.point::before { content: ''; position: absolute; left: 0; right: 0; width: 100%; height: 80%; z-index: 1; clip-path: polygon(50% 100%, 0 0, 100% 0); }
.bottom-row .point::before { bottom: 0; clip-path: polygon(50% 0, 0 100%, 100% 100%); }
.top-row .point::before { top: 0; }
.point.dark::before { background-color: var(--dark-point); }
.point.light::before { background-color: var(--light-point); }
.point.highlight::after { content: ''; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(46,204,113,0.4); z-index: 2; pointer-events: none; }

.checker-container { position: absolute; top: 0; bottom: 0; left: 0; right: 0; display: flex; flex-direction: column; align-items: center; z-index: 3; padding: 5px 0; pointer-events: none; }
.bottom-row .checker-container { justify-content: flex-end; }
.top-row .checker-container { justify-content: flex-start; }
.checker { width: 80%; aspect-ratio: 1; border-radius: 50%; box-shadow: 0 3px 5px rgba(0,0,0,0.5), inset 0 0 5px rgba(255,255,255,0.2); border: 2px solid rgba(0,0,0,0.2); position: relative; pointer-events: auto; cursor: grab; margin: -5% 0; transition: transform 0.2s; }
.checker:active { cursor: grabbing; }
.checker.p1 { background-color: var(--p1-piece); }
.checker.p2 { background-color: var(--p2-piece); }
.checker.selected { transform: scale(1.1); box-shadow: 0 0 15px #f1c40f; border-color: #f1c40f; z-index: 10; }
.bear-off-top .checker, .bear-off-bottom .checker { margin: -18% 0; width: 75%; }

#winModal .modal-content { text-align: center; }
#winModal h2 { font-size: 2rem; color: #f1c40f; margin-bottom: 1rem; }
#winModal p { font-size: 1.1rem; margin-bottom: 1.5rem; }
#winModal .btn-next { background: #2ecc71; color: #fff; padding: 14px 30px; font-size: 1.2rem; font-weight: bold; border: none; border-radius: 8px; cursor: pointer; width: 100%; }

.die { width: 40px; height: 40px; background: #fff; border-radius: 8px; display: grid; grid-template: repeat(3,1fr)/repeat(3,1fr); padding: 4px; box-shadow: 0 2px 5px rgba(0,0,0,0.3); }
.die.used { opacity: 0.35; }
.die span { display: block; width: 8px; height: 8px; background: #000; border-radius: 50%; justify-self: center; align-self: center; }
.rolling { animation: roll 0.5s ease-in-out; }
@keyframes roll { 0% { transform: rotate(0deg) scale(1); } 50% { transform: rotate(180deg) scale(1.2); } 100% { transform: rotate(360deg) scale(1); } }

.modal { display: none; position: fixed; z-index: 100; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.7); justify-content: center; align-items: center; }
.modal-content { background-color: var(--panel-bg); padding: 20px; border-radius: 10px; width: 80%; max-width: 600px; position: relative; color: #fff; }
.close-btn { position: absolute; top: 10px; right: 15px; color: #aaa; font-size: 28px; font-weight: bold; cursor: pointer; }
.rules-btn { position: absolute; bottom: 1rem; left: 1rem; width: 40px; height: 40px; background: #e67e22; border-radius: 50%; display: flex; justify-content: center; align-items: center; font-size: 1.5rem; font-weight: bold; cursor: pointer; color: #fff; }
.direction-arrows { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 1; }
.point-number { position: absolute; font-size: 11px; font-weight: bold; z-index: 2; pointer-events: none; }
.top-row .point-number { top: 3px; }
.bottom-row .point-number { bottom: 3px; }
.point.dark .point-number { color: rgba(255,255,255,0.6); }
.point.light .point-number { color: rgba(0,0,0,0.5); }

/* ===== RESPONSIVE ===== */
.mobile-flex-row { display: none; }
.details-content { display: flex; flex-direction: column; gap: 10px; height: 100%; }
.history-flex { flex: 1; }
details.mobile-details summary { display: none; }

@media (max-width: 1024px) {
    .app-container { flex-direction: column; }
    .left-panel { order: 1; }
    .board-container { order: 2; min-height: 50vh; }
    .right-panel { order: 3; }
    .panel { width: 100%; height: auto; }
    .tavla-board { max-height: 70vh; }
}

@media (max-width: 768px) {
    .app-container { padding: 0.5rem; gap: 0.5rem; }
    .tavla-board { border-width: 8px; min-height: 45vh; }
    .board-bar { width: 25px; }
    .bear-off-zone { width: 40px; }
    .checker { margin: -10% 0; }
    .status-bar { font-size: 1rem; padding: 5px 10px; }
    .dice-display { transform: scale(0.8); }
    .point-number { font-size: 9px; }
    .panel { flex-direction: column; padding: 10px; }
    .mobile-flex-row { display: flex; width: 100%; gap: 10px; margin-bottom: 10px; }
    .mobile-flex-row button { flex: 1; margin: 0; padding: 12px 5px; }
    details.mobile-details { width: 100%; }
    details.mobile-details summary { display: block; background: #34495e; padding: 12px; font-weight: bold; text-align: center; border-radius: 5px; cursor: pointer; outline: none; }
    details.mobile-details[open] summary { margin-bottom: 10px; background: #2c3e50; border: 1px solid #34495e; }
    .panel h2 { display: none; }
    .rules-btn { position: fixed; bottom: 15px; right: 15px; z-index: 50; box-shadow: 0 4px 10px rgba(0,0,0,0.5); }
}

.global-hub-btn { display: flex; justify-content: center; align-items: center; background: rgba(255,255,255,0.1); border: 1px solid rgba(255,255,255,0.2); text-decoration: none; font-size: 1.5rem; min-width: 44px; height: 44px; border-radius: 8px; transition: transform 0.2s, background 0.2s; flex-shrink: 0; }
.global-hub-btn:active { transform: scale(0.9); background: rgba(255,255,255,0.3); }
.mobile-flex-row { display: flex; width: 100%; gap: 10px; margin-bottom: 10px; align-items: stretch; }
.mobile-flex-row button { flex: 1; margin: 0; padding: 12px 5px; }

.landscape-warning { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: var(--bg-dark); z-index: 9999; justify-content: center; align-items: center; text-align: center; padding: 20px; }
.landscape-warning h2 { color: #e74c3c; margin-bottom: 15px; font-size: 2rem; }
.landscape-warning p { font-size: 1.2rem; }

@media (max-width: 950px) and (max-height: 500px) and (orientation: landscape) {
    .landscape-warning { display: flex; }
    .app-container { display: none; }
    .lobby-screen { display: none; }
}

/* Opponent dice indicator */
.opp-dice-label { font-size: 0.75rem; opacity: 0.6; text-align: center; margin-top: 2px; }
