/* CSS Variables */
:root {
    --color-primary: #6366F1;
    --color-primary-dark: #4F46E5;
    --color-secondary: #64748B;
    --color-background: #0F172A;
    --color-surface: #1E293B;
    --color-surface-light: #334155;
    --color-text: #F8FAFC;
    --color-text-muted: #94A3B8;
    --color-success: #22C55E;
    --color-warning: #F59E0B;
    --color-error: #EF4444;

    --color-dice: #FFFEF0;
    --color-dice-dots: #1A1A1A;
    --color-kept: #22C55E;

    --color-col-down: #3B82F6;
    --color-col-up: #EC4899;
    --color-col-free: #22C55E;
    --color-col-announced: #F59E0B;

    --color-available: rgba(34, 197, 94, 0.3);
    --color-preview: rgba(99, 102, 241, 0.5);

    --transition-fast: 0.15s ease;
    --transition-normal: 0.3s ease;

    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.3);
}

/* Reset */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Base */
html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--color-background);
    color: var(--color-text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

/* Screen System */
.screen {
    display: none;
    height: 100%;
    height: 100dvh;
    overflow-y: auto;
}

.screen.active {
    display: flex;
    flex-direction: column;
}

/* Container */
.container {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 24px 16px;
    max-width: 480px;
    margin: 0 auto;
    width: 100%;
}

/* Header */
.header {
    text-align: center;
    margin-bottom: 24px;
}

.header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 4px;
}

.header h2 {
    font-size: 1.5rem;
    font-weight: 600;
}

.subtitle {
    color: var(--color-text-muted);
    font-size: 1rem;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 14px 24px;
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--transition-fast);
    touch-action: manipulation;
    min-height: 48px;
    width: 100%;
}

.btn:active {
    transform: scale(0.98);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.btn-primary {
    background: var(--color-primary);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background: var(--color-primary-dark);
}

.btn-secondary {
    background: var(--color-surface-light);
    color: var(--color-text);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--color-secondary);
}

.btn-success {
    background: var(--color-success);
    color: white;
}

.btn-link {
    background: none;
    border: none;
    color: var(--color-primary);
    cursor: pointer;
    font-size: 0.9rem;
    padding: 8px;
    text-decoration: underline;
}

/* Form Elements */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 0.9rem;
    color: var(--color-text-muted);
}

input[type="text"],
select {
    width: 100%;
    padding: 12px 16px;
    font-size: 1rem;
    border: 2px solid var(--color-surface-light);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    transition: border-color var(--transition-fast);
}

input[type="text"]:focus,
select:focus {
    outline: none;
    border-color: var(--color-primary);
}

input[type="text"]::placeholder {
    color: var(--color-text-muted);
}

/* Divider */
.divider {
    display: flex;
    align-items: center;
    margin: 24px 0;
    color: var(--color-text-muted);
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--color-surface-light);
}

.divider span {
    padding: 0 12px;
    font-size: 0.9rem;
}

/* Join Section */
.join-section {
    display: flex;
    gap: 12px;
}

.join-section input {
    flex: 1;
    text-transform: uppercase;
    text-align: center;
    font-weight: 600;
    letter-spacing: 2px;
}

.join-section .btn {
    width: auto;
    padding: 12px 24px;
}

/* Room Code Display */
.room-code-display {
    text-align: center;
    margin-bottom: 24px;
}

.room-code-display p {
    color: var(--color-text-muted);
    margin-bottom: 8px;
}

.code {
    font-size: 3rem;
    font-weight: 700;
    letter-spacing: 8px;
    color: var(--color-primary);
    background: var(--color-surface);
    padding: 16px 24px;
    border-radius: var(--radius-lg);
}

/* Players List */
.players-list {
    background: var(--color-surface);
    border-radius: var(--radius-md);
    padding: 16px;
    margin-bottom: 24px;
}

.player-item {
    display: flex;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid var(--color-surface-light);
}

.player-item:last-child {
    border-bottom: none;
}

.player-item .player-number {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--color-primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 600;
    margin-right: 12px;
}

.player-item.empty .player-number {
    background: var(--color-surface-light);
}

.player-item .player-name {
    flex: 1;
}

.player-item.empty .player-name {
    color: var(--color-text-muted);
    font-style: italic;
}

.player-item .host-badge {
    font-size: 0.75rem;
    background: var(--color-warning);
    color: black;
    padding: 2px 8px;
    border-radius: 12px;
}

/* Waiting Actions */
.waiting-actions {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* ==================== PLAYING SCREEN ==================== */

/* Game Header */
.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-surface-light);
}

.turn-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

#current-player-name {
    font-weight: 600;
}

.turn-badge {
    font-size: 0.75rem;
    padding: 4px 8px;
    border-radius: 12px;
    background: var(--color-success);
    color: white;
}

.turn-badge.waiting {
    background: var(--color-surface-light);
}

.throws-indicator {
    display: flex;
    gap: 6px;
}

.throw-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--color-surface-light);
    transition: background var(--transition-fast);
}

.throw-dot.active {
    background: var(--color-primary);
}

.throw-dot.used {
    background: var(--color-text-muted);
}

/* Dice Area */
.dice-area {
    padding: 16px;
    text-align: center;
}

.dice-container {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-bottom: 8px;
}

.die {
    width: 52px;
    height: 52px;
    background: var(--color-dice);
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all var(--transition-fast);
    border: 3px solid transparent;
    box-shadow: var(--shadow-md);
    user-select: none;
}

.die:active {
    transform: scale(0.95);
}

.die.kept {
    border-color: var(--color-kept);
    box-shadow: 0 0 12px rgba(34, 197, 94, 0.5);
}

.die.rolling {
    animation: roll 0.5s ease-out;
}

.die.disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

@keyframes roll {
    0% { transform: rotateX(0deg) rotateY(0deg); }
    25% { transform: rotateX(90deg) rotateY(45deg) scale(1.1); }
    50% { transform: rotateX(180deg) rotateY(90deg); }
    75% { transform: rotateX(270deg) rotateY(135deg) scale(1.1); }
    100% { transform: rotateX(360deg) rotateY(180deg); }
}

.die-value {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--color-dice-dots);
}

.dice-hint {
    font-size: 0.8rem;
    color: var(--color-text-muted);
}

/* Game Actions */
.game-actions {
    display: flex;
    gap: 12px;
    padding: 0 16px 12px;
}

.btn-throw {
    flex: 1;
    background: var(--color-primary);
    color: white;
    font-size: 1.1rem;
}

.btn-throw:disabled {
    background: var(--color-surface-light);
}

.btn-announce {
    background: var(--color-col-announced);
    color: black;
    padding: 14px 16px;
}

/* Score Section */
.score-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--color-surface);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    overflow: hidden;
}

.score-tabs {
    display: flex;
    padding: 8px 8px 0;
    gap: 4px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.score-tab {
    padding: 8px 16px;
    border: none;
    background: var(--color-surface-light);
    color: var(--color-text-muted);
    border-radius: var(--radius-md) var(--radius-md) 0 0;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: all var(--transition-fast);
}

.score-tab.active {
    background: var(--color-background);
    color: var(--color-text);
}

.score-tab.current-turn {
    color: var(--color-success);
}

/* Score Table */
.score-table-wrapper {
    flex: 1;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    background: var(--color-background);
}

.score-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.score-table th,
.score-table td {
    padding: 8px 4px;
    text-align: center;
    border-bottom: 1px solid var(--color-surface-light);
}

.score-table thead {
    position: sticky;
    top: 0;
    background: var(--color-surface);
    z-index: 1;
}

.cat-header {
    width: 70px;
    text-align: left;
    padding-left: 8px !important;
}

.col-header {
    width: 50px;
    font-weight: 700;
}

.col-down { color: var(--color-col-down); }
.col-up { color: var(--color-col-up); }
.col-free { color: var(--color-col-free); }
.col-announced { color: var(--color-col-announced); }

.score-table tbody tr:hover {
    background: var(--color-surface);
}

.category-name {
    text-align: left;
    padding-left: 8px !important;
    font-weight: 500;
}

.score-cell {
    position: relative;
    cursor: default;
    min-width: 44px;
    transition: background var(--transition-fast);
}

.score-cell.filled {
    color: var(--color-text);
}

.score-cell.available {
    background: var(--color-available);
    cursor: pointer;
    font-weight: 600;
}

.score-cell.available:hover {
    background: var(--color-preview);
}

.score-cell.preview {
    color: var(--color-primary);
    font-style: italic;
}

.score-cell.locked {
    color: var(--color-text-muted);
    opacity: 0.5;
}

/* Section Rows */
.section-row td {
    background: var(--color-surface);
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--color-text-muted);
}

.total-row td {
    background: var(--color-surface);
    font-weight: 700;
    border-top: 2px solid var(--color-primary);
}

.bonus-row td {
    background: var(--color-surface);
    color: var(--color-warning);
    font-size: 0.8rem;
}

/* Game Footer */
.game-footer {
    padding: 12px 16px;
    background: var(--color-surface);
    border-top: 1px solid var(--color-surface-light);
}

.scores-summary {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 8px;
}

.score-summary-item {
    font-size: 0.85rem;
    padding: 4px 8px;
    background: var(--color-surface-light);
    border-radius: var(--radius-sm);
}

.score-summary-item.you {
    background: var(--color-primary);
    color: white;
}

/* ==================== MODALS ==================== */

.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    padding: 24px;
    max-width: 400px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-content h3 {
    margin-bottom: 12px;
    text-align: center;
}

.modal-content p {
    color: var(--color-text-muted);
    margin-bottom: 16px;
    text-align: center;
}

/* Announce Modal */
.announce-categories {
    margin-bottom: 16px;
}

.announce-group {
    margin-bottom: 12px;
}

.announce-group h4 {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    margin-bottom: 8px;
}

.announce-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.announce-btn {
    padding: 10px 16px;
    border: 2px solid var(--color-surface-light);
    background: var(--color-surface-light);
    color: var(--color-text);
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.announce-btn:hover {
    border-color: var(--color-col-announced);
    background: rgba(245, 158, 11, 0.2);
}

.announce-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* Help Modal */
.modal-help-content {
    max-height: 85vh;
}

.help-section {
    margin-bottom: 16px;
}

.help-section h4 {
    color: var(--color-primary);
    margin-bottom: 4px;
}

.help-section p {
    text-align: left;
    margin-bottom: 0;
}

.help-section ul {
    margin-left: 20px;
    color: var(--color-text-muted);
}

.help-section li {
    margin-bottom: 4px;
}

/* ==================== FINISHED SCREEN ==================== */

.result-icon {
    font-size: 4rem;
    margin-bottom: 8px;
}

.final-scores {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    padding: 16px;
    margin-bottom: 24px;
}

.final-score-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid var(--color-surface-light);
}

.final-score-item:last-child {
    border-bottom: none;
}

.final-score-item .position {
    font-size: 1.5rem;
    margin-right: 12px;
}

.final-score-item .name {
    flex: 1;
    font-weight: 500;
}

.final-score-item .score {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--color-primary);
}

.final-score-item.winner {
    background: rgba(34, 197, 94, 0.1);
    margin: -16px -16px 0;
    padding: 16px;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.final-score-item.winner .score {
    color: var(--color-success);
}

.finished-actions {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* ==================== ERROR SCREEN ==================== */

.error-icon {
    font-size: 4rem;
    margin-bottom: 16px;
}

.error-text {
    color: var(--color-text-muted);
    text-align: center;
    margin-bottom: 24px;
}

/* ==================== LOADING ==================== */

.loading-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.9);
    z-index: 200;
    align-items: center;
    justify-content: center;
}

.loading-overlay.active {
    display: flex;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--color-surface-light);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* ==================== RESPONSIVE ==================== */

@media (min-width: 480px) {
    .die {
        width: 60px;
        height: 60px;
    }

    .die-value {
        font-size: 2rem;
    }

    .score-table {
        font-size: 0.9rem;
    }

    .score-cell {
        min-width: 52px;
    }
}

@media (min-width: 768px) {
    .container {
        max-width: 600px;
    }

    .die {
        width: 70px;
        height: 70px;
    }

    .die-value {
        font-size: 2.25rem;
    }

    .score-table {
        font-size: 1rem;
    }
}
