/* CSS Reset & Base */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --success-color: #22c55e;
    --error-color: #ef4444;
    --warning-color: #f59e0b;
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-color: #1e293b;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 4px 6px rgba(0, 0, 0, 0.1);
    --radius: 8px;
    --radius-sm: 4px;
    --potential-bg: #f0fdf4;
    --die-bg: #ffffff;
    --highlight-bg: #dbeafe;
    --winner-bg: #fef3c7;
    --winner-text: #92400e;
    --gameover-gradient: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    --gameover-border: #f59e0b;
}

@media (prefers-color-scheme: dark) {
    :root {
        --primary-color: #3b82f6;
        --primary-hover: #60a5fa;
        --success-color: #4ade80;
        --error-color: #f87171;
        --warning-color: #fbbf24;
        --bg-color: #0f172a;
        --card-bg: #1e293b;
        --text-color: #f1f5f9;
        --text-muted: #94a3b8;
        --border-color: #334155;
        --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 4px 6px rgba(0, 0, 0, 0.3);
        --potential-bg: #14532d;
        --die-bg: #334155;
        --highlight-bg: #1e3a5f;
        --winner-bg: #1e3a5f;
        --winner-text: #93c5fd;
        --gameover-gradient: linear-gradient(135deg, #422006 0%, #78350f 100%);
        --gameover-border: #d97706;
    }
}

html {
    font-size: 16px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.5;
    min-height: 100vh;
}

/* App Container */
#app {
    max-width: 800px;
    margin: 0 auto;
    padding: 1rem;
}

/* Screens */
.screen {
    display: none;
}

.screen.active {
    display: block;
}

/* Header */
header {
    text-align: center;
    padding: 1.25rem 0;
}

header h1 {
    font-size: 2rem;
    font-weight: 700;
}

header h1 a {
    color: var(--primary-color);
    text-decoration: none;
}

header h1 a:hover {
    text-decoration: underline;
}

header .subtitle {
    color: var(--text-muted);
    font-size: 1rem;
}

header .game-code {
    font-family: monospace;
    font-size: 1.25rem;
    background: var(--card-bg);
    padding: 0.25rem 0.75rem;
    border-radius: var(--radius-sm);
    display: inline-block;
}

header .game-info {
    display: flex;
    justify-content: center;
    align-items: stretch;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-top: 0.75rem;
}

header .game-info > span {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    min-width: 80px;
    box-shadow: var(--shadow);
}

header .game-info .info-label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 0.125rem;
}

header .game-info .info-value {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text-color);
}

header .game-info .game-code .info-value {
    font-family: monospace;
    letter-spacing: 0.1em;
}

header .game-info .current-turn.my-turn {
    background: var(--primary-color);
}

header .game-info .current-turn.my-turn .info-label,
header .game-info .current-turn.my-turn .info-value {
    color: white;
}

/* Cards */
.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1rem;
    box-shadow: var(--shadow);
}

.card h2 {
    font-size: 1.125rem;
    margin-bottom: 1rem;
    color: var(--text-color);
}

/* Forms */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-weight: 500;
    margin-bottom: 0.25rem;
    font-size: 0.875rem;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group select {
    width: 100%;
    padding: 0.625rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    transition: border-color 0.15s;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* Radio Groups */
.radio-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.radio-label {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: border-color 0.15s;
}

.radio-label:hover {
    border-color: var(--primary-color);
}

.radio-label input[type="radio"] {
    margin-top: 0.25rem;
}

.radio-label span {
    font-weight: 500;
}

.radio-label small {
    display: block;
    color: var(--text-muted);
    font-size: 0.75rem;
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.625rem 1.25rem;
    font-size: 1rem;
    font-weight: 500;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 0.15s;
    text-align: center;
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background-color: var(--primary-hover);
}

.btn-small {
    padding: 0.375rem 0.75rem;
    font-size: 0.875rem;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-full {
    width: 100%;
}

/* Divider */
.divider {
    text-align: center;
    color: var(--text-muted);
    margin: 1.5rem 0;
    position: relative;
}

.divider::before,
.divider::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 40%;
    height: 1px;
    background: var(--border-color);
}

.divider::before {
    left: 0;
}

.divider::after {
    right: 0;
}

/* Player List */
.player-list {
    list-style: none;
    margin-bottom: 1rem;
}

.player-list:empty {
    display: none;
}

.player-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.875rem 1rem;
    background: var(--bg-color);
    border-radius: var(--radius-sm);
    margin-bottom: 0.5rem;
}

.player-list li:last-child {
    margin-bottom: 0;
}

.player-list .player-info {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
}

.player-list .player-name {
    font-weight: 600;
    font-size: 1.125rem;
}

.player-list .player-status {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.player-list .player-status.connected {
    color: var(--success-color);
}

.player-list .btn-remove {
    background: none;
    border: none;
    color: var(--error-color);
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
}

.player-list .btn-remove:hover {
    text-decoration: underline;
}

/* Add Player Row */
.add-player-row {
    display: flex;
    gap: 0.75rem;
    margin-top: 1rem;
}

.add-player-row input {
    flex: 1;
    padding: 0.625rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 1rem;
}

.add-player-row input:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* Empty State Message */
.empty-message {
    color: var(--text-muted);
    text-align: center;
    padding: 1.5rem;
    font-style: italic;
}

/* Waiting Card */
.waiting-card {
    text-align: center;
    background: var(--potential-bg);
    border: 1px solid var(--success-color);
}

.waiting-card p {
    color: var(--text-muted);
    margin: 0;
}

/* Share Links */
.share-links {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.share-link label {
    font-weight: 500;
    font-size: 0.875rem;
    display: block;
    margin-bottom: 0.25rem;
}

.link-copy {
    display: flex;
    gap: 0.5rem;
}

.link-copy input {
    flex: 1;
    padding: 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    background: var(--bg-color);
    color: var(--text-color);
}

/* Dice */
.dice-container {
    display: flex;
    justify-content: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.die {
    width: 56px;
    height: 56px;
    background: var(--die-bg);
    border: 2px solid var(--border-color);
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 4.5rem;
    line-height: 1;
    padding-bottom: 0.6rem;
    cursor: pointer;
    transition: all 0.15s;
    user-select: none;
}

@media (hover: hover) {
    .die:hover {
        border-color: var(--primary-color);
    }
}

.die.held {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.die.rolling {
    animation: roll 0.15s linear infinite;
}

.die.pending {
    animation: pulse 0.3s ease-out;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(0.9); }
    100% { transform: scale(1); }
}

@keyframes roll {
    0% { transform: rotate(0deg); }
    25% { transform: rotate(90deg); }
    50% { transform: rotate(180deg); }
    75% { transform: rotate(270deg); }
    100% { transform: rotate(360deg); }
}

.dice-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
}

#rolls-remaining {
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Scorecard */
.scorecard-container {
    overflow-x: auto;
    margin-bottom: 1.25rem;
}

.scorecard {
    width: 100%;
    border-collapse: collapse;
    background: var(--card-bg);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    font-size: 0.875rem;
}

.scorecard th,
.scorecard td {
    padding: 0.5rem;
    text-align: center;
    border-bottom: 1px solid var(--border-color);
}

.scorecard th {
    background: var(--bg-color);
    font-weight: 600;
}

.scorecard th.category-header {
    text-align: left;
    padding-left: 0.75rem;
}

.scorecard td.category-name {
    text-align: left;
    font-weight: 500;
    padding-left: 0.75rem;
}

.scorecard tr.section-header td {
    background: var(--primary-color);
    color: white;
    font-weight: 600;
    text-align: left;
    padding-left: 0.75rem;
}

.scorecard tr.subtotal td,
.scorecard tr.total td {
    background: var(--bg-color);
    font-weight: 600;
}

.scorecard tr.total td {
    background: var(--primary-color);
    color: white;
}

.scorecard td.current-turn {
    background: var(--highlight-bg);
}

.scorecard td.available {
    cursor: pointer;
}

.scorecard td.available:hover {
    background: var(--highlight-bg);
}

/* Inline score input */
.inline-score-input {
    width: 100%;
    max-width: 50px;
    padding: 0.25rem;
    border: 2px solid var(--primary-color);
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    text-align: center;
    -moz-appearance: textfield;
}

.inline-score-input::-webkit-outer-spin-button,
.inline-score-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.scorecard .score-value {
    min-width: 40px;
}

.scorecard .score-value.scored {
    font-weight: 600;
}

.scorecard .score-zero {
    color: var(--text-muted);
    font-weight: 400;
}

.scorecard .potential {
    color: var(--primary-color);
    font-style: italic;
    opacity: 0.8;
}

/* Toast Notifications */
#toast-container {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.toast {
    padding: 0.75rem 1rem;
    border-radius: var(--radius-sm);
    color: white;
    font-size: 0.875rem;
    box-shadow: var(--shadow-lg);
    animation: slideIn 0.3s ease-out;
}

.toast.error {
    background: var(--error-color);
}

.toast.success {
    background: var(--success-color);
}

.toast.info {
    background: var(--primary-color);
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* Game Over Summary (inline on game screen) */
.game-over-summary {
    text-align: center;
}

.game-over-summary h2 {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

.game-over-summary .btn {
    margin-top: 1.5rem;
}

/* Final Scores */
#final-scores-inline {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}

.final-score-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.875rem 1rem;
    background: var(--bg-color);
    border-radius: var(--radius);
}

.final-score-row.winner {
    background: var(--winner-bg);
}

.final-score-row.winner .name,
.final-score-row.winner .score {
    color: var(--winner-text);
}

.final-score-row .rank {
    width: 1.5rem;
    color: var(--text-muted);
    font-size: 0.875rem;
}

.final-score-row.winner .rank {
    color: var(--winner-text);
    opacity: 0.7;
}

.final-score-row .name {
    flex: 1;
    text-align: left;
    font-weight: 500;
}

.final-score-row .score {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* History list */
.history-list {
    max-height: 200px;
    overflow-y: auto;
    font-size: 0.875rem;
}

.history-list:empty::after {
    content: 'No moves yet';
    color: var(--text-muted);
    font-style: italic;
}

.history-entry {
    padding: 0.5rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background-color 0.15s;
}

.history-entry:last-child {
    border-bottom: none;
}

.history-entry:hover {
    background-color: var(--bg-color);
}

.history-entry .player-name {
    font-weight: 600;
}

.history-entry .category {
    font-weight: 500;
}

.history-entry .score {
    font-weight: 600;
    color: var(--primary-color);
}

.history-entry {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
}

.history-entry .history-text {
    flex: 1;
}

.history-entry .history-time {
    color: var(--text-muted);
    font-size: 0.75rem;
    font-family: monospace;
    white-space: nowrap;
}

.history-entry.history-system {
    font-style: italic;
    color: var(--text-muted);
}

/* Scorecard cell highlight */
.scorecard td.highlight {
    background-color: var(--winner-bg) !important;
    transition: background-color 0.15s;
}


/* Responsive */
@media (min-width: 600px) {
    .radio-group {
        flex-direction: row;
    }

    .radio-label {
        flex: 1;
    }

    .die {
        width: 64px;
        height: 64px;
        font-size: 5rem;
        line-height: 1;
        padding-bottom: 0.6rem;
    }
}

/* Connection Status Indicator */
.connection-status {
    position: fixed;
    top: 0.5rem;
    right: 0.5rem;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    font-weight: 500;
}

.connection-status.connected {
    background: var(--success-color);
    color: white;
}

.connection-status.disconnected {
    background: var(--error-color);
    color: white;
}

.connection-status.connecting {
    background: var(--warning-color);
    color: white;
}
