:root {
    /* Light Mode Colors */
    --bg-primary: #f5f5f7;
    --bg-secondary: #ffffff;
    --bg-gradient-start: #667eea;
    --bg-gradient-end: #764ba2;
    --text-primary: #1d1d1f;
    --text-secondary: #6e6e73;
    --text-muted: #86868b;
    --border-color: rgba(0, 0, 0, 0.1);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.12);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.16);
    --card-bg: #ffffff;
    --input-bg: #f5f5f7;
    --header-bg: rgba(255, 255, 255, 0.85);
    --accent-primary: #007aff;
    --accent-secondary: #5856d6;
    --accent-success: #34c759;
    --accent-danger: #ff3b30;
    --accent-warning: #ff9500;
    --positive-color: #34c759;
    --negative-color: #ff3b30;
    --neutral-color: #8e8e93;
    --summary-card-bg: linear-gradient(135deg, #e8f0fe 0%, #f0f4ff 100%);
    --button-secondary-bg: #6e6e73;
    --button-secondary-text: #ffffff;
}

[data-theme="dark"] {
    /* Dark Mode Colors */
    --bg-primary: #000000;
    --bg-secondary: #1c1c1e;
    --bg-gradient-start: #4a5568;
    --bg-gradient-end: #2d3748;
    --text-primary: #ffffff;
    --text-secondary: #ebebf5;
    --text-muted: #8e8e93;
    --border-color: rgba(255, 255, 255, 0.15);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.6);
    --card-bg: #1c1c1e;
    --input-bg: #2c2c2e;
    --header-bg: rgba(28, 28, 30, 0.85);
    --accent-primary: #0a84ff;
    --accent-secondary: #5e5ce6;
    --accent-success: #32d74b;
    --accent-danger: #ff453a;
    --accent-warning: #ff9f0a;
    --positive-color: #32d74b;
    --negative-color: #ff453a;
    --neutral-color: #98989d;
    --summary-card-bg: linear-gradient(135deg, #2c2c2e 0%, #3a3a3c 100%);
    --button-secondary-bg: #48484a;
    --button-secondary-text: #ffffff;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: linear-gradient(135deg, var(--bg-gradient-start) 0%, var(--bg-gradient-end) 100%);
    color: var(--text-primary);
    min-height: 100vh;
    transition: background 0.3s ease, color 0.3s ease;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 24px;
}

.card {
    background: var(--card-bg);
    border-radius: 20px;
    padding: 32px;
    box-shadow: var(--shadow-md);
    margin-bottom: 24px;
    border: 1px solid var(--border-color);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    backdrop-filter: blur(10px);
}

.card:hover {
    box-shadow: var(--shadow-lg);
    transform: translateY(-2px);
}

.card-small {
    max-width: 440px;
    margin: 60px auto;
}

h1 {
    text-align: center;
    color: var(--text-primary);
    margin-bottom: 32px;
    font-weight: 600;
    letter-spacing: -0.5px;
}

h2 {
    color: var(--text-primary);
    margin-top: 24px;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 12px;
    font-weight: 600;
    letter-spacing: -0.3px;
    font-size: 24px;
}

.form-group {
    margin-bottom: 20px;
}

label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: var(--text-secondary);
    font-size: 15px;
    letter-spacing: -0.2px;
}

input, select, textarea {
    width: 100%;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    font-size: 16px;
    background: var(--input-bg);
    color: var(--text-primary);
    transition: all 0.2s ease;
    font-family: inherit;
    -webkit-appearance: none;
    appearance: none;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 4px rgba(0, 122, 255, 0.1);
    background: var(--card-bg);
}

[data-theme="dark"] input:focus,
[data-theme="dark"] select:focus,
[data-theme="dark"] textarea:focus {
    box-shadow: 0 0 0 4px rgba(10, 132, 255, 0.2);
}

button {
    background: var(--accent-primary);
    color: #ffffff;
    padding: 13px 28px;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 500;
    margin-right: 10px;
    margin-top: 10px;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    font-family: inherit;
    letter-spacing: -0.2px;
    box-shadow: var(--shadow-sm);
}

button:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
    opacity: 0.9;
}

button:active {
    transform: translateY(0);
    box-shadow: var(--shadow-sm);
}

button:disabled {
    background: var(--neutral-color);
    color: #ffffff;
    cursor: not-allowed;
    opacity: 0.5;
    transform: none;
}

button.secondary {
    background: var(--button-secondary-bg);
    color: var(--button-secondary-text);
}

button.secondary:hover {
    opacity: 0.85;
}

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

button.danger:hover {
    opacity: 0.9;
}

button.success {
    background: var(--accent-success);
    color: #ffffff;
}

button.success:hover {
    opacity: 0.9;
}

.btn-link {
    background: none;
    color: var(--accent-primary);
    text-decoration: none;
    padding: 0;
    cursor: pointer;
    font-weight: 500;
    border: none;
    box-shadow: none;
    transition: opacity 0.2s ease;
}

.btn-link:hover {
    opacity: 0.7;
    transform: none;
}

.alert {
    padding: 16px 20px;
    border-radius: 12px;
    margin-bottom: 20px;
    font-weight: 500;
    border: 1px solid;
    backdrop-filter: blur(10px);
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.alert-error {
    background: rgba(255, 59, 48, 0.1);
    color: var(--accent-danger);
    border-color: var(--accent-danger);
}

[data-theme="dark"] .alert-error {
    background: rgba(255, 69, 58, 0.15);
}

.alert-success {
    background: rgba(52, 199, 89, 0.1);
    color: var(--accent-success);
    border-color: var(--accent-success);
}

[data-theme="dark"] .alert-success {
    background: rgba(50, 215, 75, 0.15);
}

.alert-info {
    background: rgba(0, 122, 255, 0.1);
    color: var(--accent-primary);
    border-color: var(--accent-primary);
}

[data-theme="dark"] .alert-info {
    background: rgba(10, 132, 255, 0.15);
}

.text-center {
    text-align: center;
}

.text-muted {
    color: var(--text-muted);
}

.loading {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid var(--border-color);
    border-top: 3px solid var(--accent-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-right: 10px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.header {
    background: var(--header-bg);
    padding: 20px 32px;
    margin-bottom: 32px;
    border-radius: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-primary);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-md);
}

.header h2 {
    color: var(--text-primary);
    border: none;
    margin: 0;
}

.header .user-info {
    display: flex;
    align-items: center;
    gap: 16px;
}

table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin-top: 20px;
    border-radius: 12px;
    overflow: hidden;
}

th, td {
    padding: 16px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

th {
    background: var(--accent-primary);
    color: white;
    font-weight: 600;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

tr {
    transition: background 0.2s ease;
}

tr:hover {
    background: var(--input-bg);
}

tbody tr:last-child td {
    border-bottom: none;
}

.positive {
    color: var(--positive-color);
    font-weight: 600;
}

.negative {
    color: var(--negative-color);
    font-weight: 600;
}

.zero {
    color: var(--neutral-color);
}

.summary-cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    margin: 0 0 32px 0;
    width: 100%;
    max-width: 100%;
}

#gameSection .summary-cards,
#hostStats,
#playerStats {
    width: 100%;
    max-width: none;
}

/* Responsive: stack on mobile */
@media (max-width: 768px) {
    .summary-cards {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 480px) {
    .summary-cards {
        grid-template-columns: 1fr;
    }
}

.summary-card {
    background: var(--summary-card-bg);
    color: var(--text-primary);
    padding: 28px 24px;
    border-radius: 16px;
    text-align: center;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
    min-height: 130px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.summary-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
}

.summary-card h3 {
    font-size: 13px;
    margin-bottom: 12px;
    opacity: 0.7;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.summary-card .amount {
    font-size: 32px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.badge {
    display: inline-block;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border: 1px solid;
}

.badge-host {
    background: rgba(255, 149, 0, 0.15);
    color: var(--accent-warning);
    border-color: var(--accent-warning);
}

.badge-player {
    background: rgba(0, 122, 255, 0.15);
    color: var(--accent-primary);
    border-color: var(--accent-primary);
}

[data-theme="dark"] .badge-host {
    background: rgba(255, 159, 10, 0.2);
}

[data-theme="dark"] .badge-player {
    background: rgba(10, 132, 255, 0.2);
}

.hidden {
    display: none;
}

/* ===== Modal Styles ===== */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 9999;
    animation: fadeIn 0.2s ease;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.modal-overlay.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background: var(--card-bg);
    border-radius: 20px;
    padding: 32px;
    max-width: 600px;
    width: 90%;
    max-height: 85vh;
    max-height: 85dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
    animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 16px;
}

.modal-header h2 {
    margin: 0;
    border: none;
    padding: 0;
    color: var(--text-primary);
}

.modal-close {
    background: none;
    border: none;
    font-size: 28px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 44px;
    height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    transition: all 0.2s ease;
    margin: 0;
    flex-shrink: 0;
}

.modal-close:hover {
    background: var(--input-bg);
    transform: none;
}

.modal-body {
    color: var(--text-primary);
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
    flex-wrap: wrap;
}

.modal-footer button {
    width: auto !important;
    margin: 0 !important;
    flex: 1;
    min-width: 100px;
}

.member-item {
    background: var(--input-bg);
    padding: 16px;
    border-radius: 12px;
    margin-bottom: 12px;
    border-left: 4px solid var(--accent-primary);
    transition: all 0.2s ease;
}

.member-item:hover {
    transform: translateX(4px);
    box-shadow: var(--shadow-sm);
}

.member-item h4 {
    margin: 0 0 4px 0;
    color: var(--text-primary);
    font-size: 16px;
}

.member-item p {
    margin: 0;
    color: var(--text-secondary);
    font-size: 14px;
}

/* ===== Live Session Banner ===== */
.live-session-banner {
    background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 24px;
    box-shadow: 0 4px 20px rgba(40, 167, 69, 0.3);
    animation: pulseGlow 2s ease-in-out infinite;
}

[data-theme="dark"] .live-session-banner {
    background: linear-gradient(135deg, #32d74b 0%, #20c997 100%);
    box-shadow: 0 4px 20px rgba(50, 215, 75, 0.4);
}

@keyframes pulseGlow {
    0%, 100% {
        box-shadow: 0 4px 20px rgba(40, 167, 69, 0.3);
    }
    50% {
        box-shadow: 0 4px 30px rgba(40, 167, 69, 0.6);
    }
}

[data-theme="dark"] .live-session-banner {
    animation-name: pulseGlowDark;
}

@keyframes pulseGlowDark {
    0%, 100% {
        box-shadow: 0 4px 20px rgba(50, 215, 75, 0.4);
    }
    50% {
        box-shadow: 0 4px 30px rgba(50, 215, 75, 0.7);
    }
}

.live-session-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
}

.live-indicator {
    display: flex;
    align-items: center;
    gap: 10px;
}

.live-dot {
    width: 12px;
    height: 12px;
    background: #ffffff;
    border-radius: 50%;
    animation: blink 1.5s ease-in-out infinite;
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.8);
}

@keyframes blink {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.3;
    }
}

.live-text {
    color: #ffffff;
    font-weight: 700;
    font-size: 18px;
    letter-spacing: 1px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.join-live-button {
    background: #ffffff;
    color: #28a745;
    padding: 12px 24px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    border: none;
    cursor: pointer;
    transition: all 0.2s ease;
    margin: 0;
}

[data-theme="dark"] .join-live-button {
    color: #32d74b;
}

.join-live-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.join-live-button:active {
    transform: translateY(0);
}

/* Mobile responsive adjustments for live banner */
@media (max-width: 480px) {
    .live-session-banner {
        padding: 16px;
        margin-bottom: 16px;
    }
    
    .live-session-content {
        flex-direction: column;
        gap: 12px;
    }
    
    .live-text {
        font-size: 16px;
    }
    
    .join-live-button {
        width: 100%;
        padding: 14px 20px;
        font-size: 16px;
    }
}

/* ===== Dark Mode Toggle ===== */
.theme-toggle {
    position: fixed;
    bottom: calc(24px + env(safe-area-inset-bottom));
    right: calc(24px + env(safe-area-inset-right));
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 1000;
    padding: 0;
    margin: 0;
}

.theme-toggle:hover {
    transform: translateY(-4px) scale(1.05);
    box-shadow: var(--shadow-lg);
}

.theme-toggle:active {
    transform: scale(0.95);
}

/* ===== Mobile Responsive Styles ===== */

/* Tablets and below */
@media (max-width: 768px) {
    .container {
        padding: 10px;
    }
    
    .card {
        padding: 20px;
        margin-bottom: 15px;
    }
    
    .card-small {
        margin: 20px auto;
    }
    
    h1 {
        font-size: 24px;
        margin-bottom: 20px;
    }
    
    h2 {
        font-size: 20px;
    }
    
    .header {
        flex-direction: column;
        gap: 10px;
        padding: 15px;
        text-align: center;
    }
    
    .header .user-info {
        flex-direction: column;
        gap: 10px;
    }
    
    /* Buttons full-width on mobile, but NOT inside modals or flex rows */
    button:not(.modal-close):not(.modal-footer button):not(.join-live-button):not(.theme-toggle) {
        width: 100%;
        margin-right: 0;
        margin-bottom: 10px;
    }
    
    /* Make tables scrollable */
    table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }
    
    th, td {
        padding: 8px;
        font-size: 14px;
    }
    
    .summary-card .amount {
        font-size: 28px;
    }

    /* Modal adjustments for tablet/mobile */
    .modal-overlay.active {
        align-items: flex-start;
        padding-top: 40px;
        padding-bottom: 20px;
        overflow-y: auto;
    }

    .modal-content {
        padding: 20px;
        width: 95%;
        max-height: none;
        margin-bottom: 20px;
    }
}

/* Mobile phones */
@media (max-width: 480px) {
    body {
        font-size: 14px;
    }
    
    .container {
        padding: 8px;
    }
    
    .card {
        padding: 16px;
        border-radius: 12px;
    }
    
    h1 {
        font-size: 20px;
    }
    
    h2 {
        font-size: 18px;
    }
    
    input, select, textarea {
        padding: 12px;
        font-size: 16px; /* Prevents zoom on iOS - must stay 16px */
    }
    
    button {
        padding: 14px 16px;
        font-size: 16px;
        min-height: 44px; /* Apple minimum touch target */
    }
    
    th, td {
        padding: 6px;
        font-size: 12px;
    }
    
    .summary-card {
        padding: 20px 15px;
        min-height: 100px;
    }
    
    .summary-card h3 {
        font-size: 14px;
        margin-bottom: 10px;
    }
    
    .summary-card .amount {
        font-size: 24px;
    }
    
    .header {
        padding: 10px;
    }
    
    .header h2 {
        font-size: 16px;
        letter-spacing: 1px;
    }

    .modal-content {
        padding: 16px;
        border-radius: 16px;
    }
}

/* Landscape mobile phones */
@media (max-width: 768px) and (orientation: landscape) {
    .summary-cards {
        grid-template-columns: repeat(2, 1fr);
    }
}
