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

:root {
    --bg-primary: #0a0a0f;
    --bg-card: #16161f;
    --bg-input: #1e1e2a;
    --accent: #6366f1;
    --accent-light: #818cf8;
    --accent-glow: rgba(99, 102, 241, 0.4);
    --gradient: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #a855f7 100%);
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border: rgba(255, 255, 255, 0.08);
    --orange: #f59e0b;
    --red: #ef4444;
    --green: #22c55e;
    --radius: 16px;
    --radius-sm: 10px;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Cairo', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    direction: rtl;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 480px;
    margin: 0 auto;
    padding: 0 16px;
}

/* Header */
.header {
    text-align: center;
    padding: 12px 0 8px;
    flex-shrink: 0;
}

.header-top {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.site-link {
    position: absolute;
    right: 0;
    display: flex;
    align-items: center;
    gap: 4px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.65rem;
    font-weight: 600;
    transition: color 0.2s;
}

.site-link:active {
    color: var(--accent-light);
}

.site-link svg {
    width: 14px;
    height: 14px;
}

.logo {
    font-size: 1.3rem;
    font-weight: 800;
    background: var(--gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-subtitle {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin: 2px 0 6px;
}

.store-links {
    display: flex;
    justify-content: center;
    gap: 8px;
}

.store-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-primary);
    transition: all 0.2s;
}

.store-badge:active {
    transform: scale(0.96);
}

.store-badge svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

.store-badge .store-info {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    line-height: 1.2;
}

.store-badge .store-info small {
    font-size: 0.5rem;
    color: var(--text-muted);
}

.store-badge .store-info span {
    font-weight: 600;
    font-size: 0.7rem;
}

/* Scoreboard */
.scoreboard {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 16px;
    background: var(--bg-card);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    flex-shrink: 0;
}

.team {
    flex: 1;
    text-align: center;
}

.team-name {
    font-family: 'Cairo', sans-serif;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    color: var(--text-secondary);
    font-size: 0.95rem;
    font-weight: 600;
    text-align: center;
    width: 100%;
    padding: 2px 4px;
    direction: rtl;
    outline: none;
    transition: border-color 0.2s;
}

.team-name:focus {
    border-color: var(--accent);
    color: var(--text-primary);
}

.team-total {
    font-size: 3rem;
    font-weight: 800;
    line-height: 1.1;
    background: var(--gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.stage-wins {
    font-size: 0.8rem;
    color: var(--text-muted);
    min-height: 1.2em;
}

.stage-wins .win-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--accent);
    margin: 0 2px;
}

.stage-wins .win-dot.empty {
    background: var(--bg-input);
    border: 1px solid var(--text-muted);
}

.divider {
    flex-shrink: 0;
    text-align: center;
}

.vs {
    font-size: 2rem;
    font-weight: 800;
    color: var(--text-muted);
}

.stage-label {
    font-size: 0.65rem;
    color: var(--text-muted);
    min-height: 1em;
}

/* Score Input */
.score-input {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 0;
    flex-shrink: 0;
}

.score-input input {
    flex: 1;
    font-family: 'Cairo', sans-serif;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 1.3rem;
    font-weight: 700;
    text-align: center;
    padding: 10px 8px;
    outline: none;
    direction: ltr;
    min-width: 0;
    transition: border-color 0.2s;
}

.score-input input:focus {
    border-color: var(--accent);
}

.btn-add {
    font-family: 'Cairo', sans-serif;
    background: var(--gradient);
    border: none;
    border-radius: var(--radius-sm);
    color: white;
    font-size: 0.9rem;
    font-weight: 700;
    padding: 12px 16px;
    cursor: pointer;
    white-space: nowrap;
    transition: transform 0.15s, opacity 0.15s;
    flex-shrink: 0;
}

.btn-add:active {
    transform: scale(0.96);
    opacity: 0.9;
}

/* History */
.history-container {
    flex: 1;
    overflow-y: auto;
    border-radius: var(--radius);
    background: var(--bg-card);
    border: 1px solid var(--border);
    min-height: 0;
    position: relative;
}

.history-table {
    width: 100%;
    border-collapse: collapse;
}

.history-table tr {
    border-bottom: 1px solid var(--border);
}

.history-table td {
    padding: 8px 12px;
    text-align: center;
    font-size: 0.95rem;
    font-weight: 600;
}

.history-table .round-row td {
    color: var(--text-primary);
}

.history-table .total-row td {
    color: var(--accent-light);
    font-weight: 700;
    font-size: 0.85rem;
    background: rgba(99, 102, 241, 0.06);
}

.history-table .round-label {
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 400;
    width: 60px;
}

.history-table .total-label {
    color: var(--accent);
    font-size: 0.75rem;
    font-weight: 600;
    width: 60px;
}

.empty-state {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-muted);
    font-size: 0.9rem;
}

.empty-state.hidden {
    display: none;
}

/* Bottom Actions */
.actions {
    display: flex;
    gap: 8px;
    padding: 12px 0;
    flex-shrink: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.btn-action {
    flex: 1;
    font-family: 'Cairo', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 600;
    padding: 10px 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-action svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.btn-action:disabled {
    opacity: 0.35;
    cursor: default;
}

.btn-action:not(:disabled):active {
    transform: scale(0.96);
}

.btn-undo:not(:disabled) {
    color: var(--orange);
    border-color: rgba(245, 158, 11, 0.2);
}

.btn-reset:not(:disabled) {
    color: var(--red);
    border-color: rgba(239, 68, 68, 0.2);
}

.btn-settings {
    color: var(--text-secondary);
}

/* Modals */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
    backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    width: 90%;
    max-width: 360px;
    padding: 20px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.modal-header h2 {
    font-size: 1.1rem;
    font-weight: 700;
}

.btn-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

.setting-item {
    margin-bottom: 18px;
}

.setting-item label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.setting-item input[type="number"] {
    font-family: 'Cairo', sans-serif;
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 1.1rem;
    font-weight: 700;
    text-align: center;
    padding: 10px;
    outline: none;
    direction: ltr;
}

.setting-item input[type="number"]:focus {
    border-color: var(--accent);
}

.match-type-group {
    display: flex;
    gap: 8px;
}

.match-type-btn {
    flex: 1;
    font-family: 'Cairo', sans-serif;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 600;
    padding: 8px 4px;
    cursor: pointer;
    transition: all 0.2s;
}

.match-type-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
}

/* Winner Overlay */
.winner-card {
    background: var(--bg-card);
    border: 1px solid var(--accent);
    border-radius: var(--radius);
    padding: 32px 24px;
    text-align: center;
    width: 90%;
    max-width: 340px;
    box-shadow: 0 0 40px var(--accent-glow);
}

.trophy {
    font-size: 3.5rem;
    margin-bottom: 8px;
}

.winner-title {
    font-size: 1.3rem;
    font-weight: 800;
    margin-bottom: 16px;
    background: var(--gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.winner-scores {
    display: flex;
    justify-content: center;
    gap: 24px;
    margin-bottom: 12px;
}

.winner-scores .ws-team {
    text-align: center;
}

.winner-scores .ws-name {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.winner-scores .ws-score {
    font-size: 1.8rem;
    font-weight: 800;
}

.winner-scores .ws-score.winner {
    color: var(--accent-light);
}

.winner-scores .ws-score.loser {
    color: var(--text-muted);
}

.winner-stage-info {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

.btn-winner-action {
    font-family: 'Cairo', sans-serif;
    background: var(--gradient);
    border: none;
    border-radius: var(--radius-sm);
    color: white;
    font-size: 1rem;
    font-weight: 700;
    padding: 12px 32px;
    cursor: pointer;
    transition: transform 0.15s;
}

.btn-winner-action:active {
    transform: scale(0.96);
}

/* Confirm Dialog */
.confirm-modal {
    text-align: center;
    padding: 24px;
}

.confirm-modal p {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 20px;
}

.confirm-buttons {
    display: flex;
    gap: 10px;
}

.confirm-buttons button {
    flex: 1;
    font-family: 'Cairo', sans-serif;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 700;
    padding: 10px;
    cursor: pointer;
    transition: transform 0.15s;
}

.confirm-buttons button:active {
    transform: scale(0.96);
}

.btn-confirm-yes {
    background: var(--red);
    color: white;
}

.btn-confirm-no {
    background: var(--bg-input);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

/* Scrollbar */
.history-container::-webkit-scrollbar {
    width: 4px;
}

.history-container::-webkit-scrollbar-track {
    background: transparent;
}

.history-container::-webkit-scrollbar-thumb {
    background: var(--text-muted);
    border-radius: 2px;
}

/* Remove number input arrows */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

input[type="number"] {
    -moz-appearance: textfield;
}
