/* --- General Styles & Variables --- */
:root {
    --primary-color: #6d5edc;
    --primary-gradient: linear-gradient(135deg, #897dec, #6d5edc);
    --secondary-color: #f0f2f5;
    --background-color: #ffffff;
    --text-color: #333;
    --text-color-light: #f8f9fa;
    --border-color: #e9ecef;
    --shadow-color: rgba(0, 0, 0, 0.1);
    --success-color: #28a745;
    --warning-color: #ffc107;
    --danger-color: #dc3545;
    --light-color: #f8f9fa;
    --font-family: 'Vazirmatn', sans-serif;
    --border-radius: 12px;
    --header-height: 120px;
    --footer-height: 70px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
}

body {
    font-family: var(--font-family);
    background-color: var(--secondary-color);
    color: var(--text-color);
    line-height: 1.6;
    direction: rtl;
    text-align: right;
    overscroll-behavior-y: contain;
}

#app-container {
    max-width: 800px;
    margin: 0 auto;
    background-color: var(--background-color);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 0 20px var(--shadow-color);
}

main {
    flex-grow: 1;
    padding: 1rem;
    padding-top: calc(var(--header-height) + 1rem);
    padding-bottom: calc(var(--footer-height) + 2rem);
    transition: padding-top 0.3s ease-in-out;
}

/* --- Header --- */
.app-header {
    background: var(--primary-gradient);
    color: var(--text-color-light);
    padding: 1.5rem 1rem;
    text-align: center;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom-left-radius: var(--border-radius);
    border-bottom-right-radius: var(--border-radius);
    max-width: 800px;
    margin: 0 auto;
    transition: all 0.3s ease-in-out;
}

.header-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: all 0.3s ease-in-out;
}

.app-header h1 {
    font-size: 1.8rem;
    font-weight: 700;
    margin: 0;
}

.app-header p {
    font-size: 1rem;
    opacity: 0.9;
    transition: all 0.3s ease-in-out;
}

/* Scrolled state for header */
.app-header.scrolled {
    height: 60px;
    padding: 0.5rem 1rem;
    border-radius: 0;
    border-bottom: 1px solid rgba(0,0,0,0.1);
}
.app-header.scrolled p {
    opacity: 0;
    visibility: hidden;
    height: 0;
    margin: 0;
}
main.header-scrolled {
    padding-top: 76px; /* 60px header + 16px padding */
}


.back-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 1rem;
    background: none;
    border: none;
    color: var(--text-color-light);
    cursor: pointer;
    padding: 0.5rem;
}
.back-btn.hidden { display: none; }

/* --- Footer --- */
.footer-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: 800px;
    margin: 0 auto;
    height: var(--footer-height);
    background-color: var(--background-color);
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-shadow: 0 -2px 10px var(--shadow-color);
    z-index: 100;
    border-top-left-radius: var(--border-radius);
    border-top-right-radius: var(--border-radius);
}

.nav-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: #555;
    font-family: var(--font-family);
    font-size: 0.75rem;
    cursor: pointer;
    flex-grow: 1;
    height: 100%;
    transition: color 0.2s;
}
.nav-btn:hover {
    color: var(--primary-color);
}
.nav-btn svg {
    width: 24px;
    height: 24px;
    margin-bottom: 4px;
}

/* --- Pages --- */
.page {
    display: none;
}
.page.active {
    display: block;
    animation: fadeIn 0.3s ease-in-out;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* --- Buttons --- */
.btn {
    padding: 0.75rem 1.5rem;
    border-radius: var(--border-radius);
    border: none;
    font-family: var(--font-family);
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.btn-primary {
    background: var(--primary-gradient);
    color: var(--text-color-light);
}
.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(109, 94, 220, 0.4);
}

.btn-secondary {
    background-color: var(--light-color);
    color: var(--text-color);
    border: 1px solid var(--border-color);
}
.btn-secondary:hover {
    background-color: #e9ecef;
}

.btn-danger {
    background-color: var(--danger-color);
    color: white;
}

.btn-full {
    width: 100%;
    margin-bottom: 1.5rem;
}

/* --- Placeholder --- */
.placeholder {
    text-align: center;
    padding: 3rem 1rem;
    color: #aaa;
}
.placeholder-icon {
    color: #ddd;
    margin-bottom: 1rem;
}
.placeholder p {
    font-size: 1.2rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
}

/* --- Game Card --- */
.game-card {
    background-color: var(--background-color);
    border-radius: var(--border-radius);
    margin-bottom: 1rem;
    box-shadow: 0 4px 12px var(--shadow-color);
    cursor: pointer;
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
}
.game-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
}

.card-header {
    background: var(--primary-gradient);
    color: var(--text-color-light);
    padding: 0.75rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.game-card.finished .card-header {
    background: linear-gradient(135deg, #a8b0b8, #89929a);
}

.game-title {
    font-weight: 700;
    font-size: 1.1rem;
}

.game-status {
    background-color: var(--success-color);
    color: white;
    padding: 0.2rem 0.6rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
}

.delete-game-btn {
    background: none;
    border: none;
    color: var(--text-color-light);
    cursor: pointer;
    opacity: 0.8;
    transition: opacity 0.2s;
}
.delete-game-btn:hover {
    opacity: 1;
}

.card-body {
    padding: 1rem;
}

.card-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.85rem;
    color: #777;
    margin-bottom: 1rem;
}
.meta-item {
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.team-score-card {
    background-color: var(--light-color);
    border-radius: var(--border-radius);
    padding: 1rem;
    margin-bottom: 0.75rem;
}
.team-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.25rem;
}
.team-names {
    font-weight: 500;
}
.team-score {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--primary-color);
}
.team-score.negative {
    color: var(--danger-color);
}
.score-progress {
    width: 100%;
    height: 6px;
    background-color: #e9ecef;
    border-radius: 3px;
    margin-top: 0.5rem;
    overflow: hidden;
}
.progress-bar {
    height: 100%;
    background: var(--primary-gradient);
    border-radius: 3px;
    transition: width 0.5s ease-in-out;
}

.card-footer {
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    color: #777;
}

/* --- Game Page --- */
#game-page {
    padding-bottom: 1rem;
}
.game-header-info {
    background-color: var(--light-color);
    padding: 1rem;
    border-radius: var(--border-radius);
    margin-bottom: 1rem;
}
.game-header-info .meta-item {
    margin-bottom: 0.5rem;
}
.details-toggle {
    color: var(--primary-color);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
}
.extra-details {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}
.extra-details.hidden {
    display: none;
}
.game-description {
    font-style: italic;
    color: #555;
}

.current-hand-box {
    padding: 1.5rem;
    border-radius: var(--border-radius);
    margin-bottom: 1rem;
    text-align: center;
    background: linear-gradient(135deg, #fff3cd, #ffeeba);
    color: #856404;
    border: 1px solid #ffeeba;
}
.current-hand-box h3 {
    margin-bottom: 0.5rem;
    font-size: 1rem;
    font-weight: normal;
}
.current-hand-box p {
    margin-bottom: 1.5rem;
    line-height: 1.8;
    font-size: 1.2rem;
    font-weight: 500;
}
.current-hand-box .btn-group {
    display: flex;
    gap: 1rem;
    justify-content: center;
}
.score-positive {
    color: var(--success-color);
    font-weight: bold;
}
.score-negative {
    color: var(--danger-color);
    font-weight: bold;
}

.game-scores-display {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.75rem;
    margin-bottom: 1rem;
}
@media (min-width: 600px) {
    .game-scores-display {
        grid-template-columns: 1fr 1fr;
        gap: 1rem;
    }
}


#history-btn {
    background: linear-gradient(135deg, #ff8a8a, #ff5252);
    color: white;
    width: 100%;
    margin-bottom: 1rem;
}

.chart-wrapper {
    border: 1px solid var(--border-color);
    padding: 1rem;
    border-radius: var(--border-radius);
    background-color: #fff;
    height: 300px;
}

.stats-section h3 {
    margin-top: 2rem;
    margin-bottom: 1rem;
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 0.5rem;
}
.stats-section h4 {
    margin-top: 1.5rem;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.stat-card {
    background-color: var(--light-color);
    padding: 1rem;
    border-radius: var(--border-radius);
    margin-bottom: 0.75rem;
}

.stat-card p {
    font-size: 0.9rem;
    color: #555;
    line-height: 1.8;
}
.stat-card .success { color: var(--success-color); font-weight: bold; }
.stat-card .fail { color: var(--danger-color); font-weight: bold; }
.stat-card .special-event { font-weight: bold; }

.stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
    margin-bottom: 2rem;
}

@media (min-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr 1fr;
    }
    .stats-grid .full-width-chart {
        grid-column: 1 / -1;
    }
}

.individual-stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
    margin-top: 1rem;
}

@media (min-width: 768px) {
    .individual-stats-grid {
        grid-template-columns: 1fr 1fr;
    }
}


/* --- History Page --- */
#history-page h2 {
    text-align: center;
    margin-bottom: 1.5rem;
}
.hand-card {
    background-color: white;
    padding: 1rem;
    border-radius: var(--border-radius);
    margin-bottom: 1rem;
    box-shadow: 0 2px 8px var(--shadow-color);
    border: 2px solid transparent;
}
.hand-card.hakem-success { border-color: var(--success-color); }
.hand-card.hakem-failed { border-color: var(--danger-color); }
.hand-card.shelem { background-color: #e9f7ef; }
.hand-card.yasa { background-color: #fbe9e7; }

.hand-card-header {
    font-weight: bold;
    margin-bottom: 0.75rem;
    color: #555;
}
.hand-card-header span {
    font-weight: 300;
}
.hand-card-body {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    font-size: 1.1rem;
    align-items: center;
    margin-bottom: 1rem;
}
.hand-card-footer {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    justify-content: space-between;
}
.score-box {
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    font-weight: bold;
    color: white;
    font-size: 1rem;
    flex-grow: 1;
    text-align: center;
}
.score-box-hakem.success { background-color: var(--success-color); }
.score-box-hakem.fail { background-color: var(--danger-color); }
.score-box-opponent { background-color: #4a90e2; }

.hand-card .edit-btn {
    background-color: var(--primary-color);
    color: white;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
}

/* --- Modal Styles --- */
.modal-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: flex-end; /* Align to bottom for mobile */
    justify-content: center;
    z-index: 1000;
}
.modal-wrapper.hidden {
    display: none;
}
.modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    animation: fadeIn 0.3s;
}
.modal-content {
    background-color: var(--background-color);
    width: 100%;
    max-width: 800px;
    max-height: 90vh;
    overflow-y: auto;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    padding: 1.5rem;
    position: relative;
    animation: slideUp 0.3s ease-out;
}
@media (min-width: 600px) {
    .modal-wrapper {
        align-items: center; /* Center for desktop */
    }
    .modal-content {
        width: 90%;
        max-width: 500px;
        border-radius: var(--border-radius);
        animation: zoomIn 0.3s ease-out;
    }
}
@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}
@keyframes zoomIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}
.modal-header h2 {
    font-size: 1.3rem;
    margin: 0;
}
.modal-close-btn, .modal-back-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.5rem;
    color: #888;
}

.form-group {
    margin-bottom: 1.5rem;
}
.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
}
.form-control {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    font-family: var(--font-family);
    font-size: 1rem;
}
.form-control:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(109, 94, 220, 0.2);
}
/* Hide number input spinners */
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;
}


.button-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 0.75rem;
}
.button-grid.full-width-4 {
    grid-template-columns: repeat(4, 1fr);
}
.button-grid.full-width-3 {
    grid-template-columns: repeat(3, 1fr);
}
.button-grid.commitment-grid {
    grid-template-columns: repeat(4, 1fr);
}
.commitment-grid .grid-btn.span-2 {
    grid-column: span 2;
}


.grid-btn {
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    background-color: white;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font-family);
}
.grid-btn:hover {
    background-color: var(--light-color);
    border-color: var(--primary-color);
}
.grid-btn.selected {
    background-color: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.player-setup-grid {
    display: grid;
    grid-template: "top top top" 40px
                   "left mid right" 60px
                   "bottom bottom bottom" 40px / 1fr auto 1fr;
    align-items: center;
    justify-items: center;
    gap: 1rem;
    margin: 2rem auto;
    width: 100%;
    max-width: 300px;
}
.player-btn {
    width: 100px;
    height: 40px;
    border: 2px dashed #ccc;
    border-radius: var(--border-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background-color: var(--light-color);
    font-weight: 500;
    font-family: var(--font-family);
    text-align: center;
    padding: 0 0.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.player-btn.filled {
    border-style: solid;
    border-color: var(--primary-color);
    background-color: #e8e5ff;
}
.player-btn[data-player="p1"] { grid-area: top; }
.player-btn[data-player="p2"] { grid-area: right; }
.player-btn[data-player="p3"] { grid-area: bottom; }
.player-btn[data-player="p4"] { grid-area: left; }

.table-center {
    grid-area: mid;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background-color: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
}

.recent-players-list {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0.5rem;
    margin-top: 1rem;
}
#confirm-player-name {
    margin-top: 1.5rem;
}

.error-message {
    color: var(--danger-color);
    font-size: 0.9rem;
    margin-top: 0.5rem;
    text-align: center;
}

.suit-selection .grid-btn {
    font-size: 2rem;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* --- Rules Page --- */
.rules-content {
    padding: 1rem;
    background: white;
    border-radius: var(--border-radius);
}
.rules-content h2, .rules-content h3, .rules-content h4 {
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
    color: var(--primary-color);
}
.rules-content p, .rules-content li {
    margin-bottom: 0.75rem;
    color: #555;
}
.rules-content ul {
    padding-right: 20px;
}

/* --- Winner Modal --- */
.winner-modal-content {
    text-align: center;
}
.winner-modal-content h2 {
    color: var(--success-color);
    font-size: 2rem;
}
.winner-modal-content p {
    font-size: 1.2rem;
    margin: 1rem 0;
}
.winner-modal-content .team-names {
    font-weight: bold;
    font-size: 1.5rem;
    color: var(--primary-color);
}

/* --- Confirmation Summary --- */
.confirmation-summary {
    background-color: var(--light-color);
    border-radius: var(--border-radius);
    padding: 1.5rem;
}
.summary-item {
    display: grid;
    grid-template-columns: 100px 1fr;
    gap: 1rem;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-color);
}
.summary-item:last-child {
    border-bottom: none;
}
.summary-item.full-width {
    grid-template-columns: 1fr;
    text-align: center;
}
.summary-item.full-width strong {
    font-size: 1.2rem;
    margin-bottom: 0.5rem;
}
.summary-item strong {
    color: var(--primary-color);
    font-weight: 500;
}
.summary-item span {
    font-weight: bold;
}
.summary-buttons {
    display: flex;
    gap: 1rem;
    margin-top: 1.5rem;
}

/* --- Import Modals --- */
.import-options .btn {
    flex-direction: column;
    height: auto;
    padding: 1rem;
    text-align: right;
    align-items: flex-start;
}
.import-options .btn strong {
    font-size: 1rem;
    margin-bottom: 0.5rem;
}
.import-options .btn span {
    font-size: 0.85rem;
    white-space: normal;
    line-height: 1.5;
}


/* --- Utility Classes --- */
.score-value {
    direction: ltr;
    display: inline-block;
}

.app-credit-footer {
    text-align: center;
    margin-top: 2rem;
    font-size: 0.8rem;
    color: #999;
}

.points-to-target {
    font-size: 0.8rem;
    color: #888;
    text-align: left;
}

.team-names-badges {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.badges-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
}

.badge-chip {
    font-size: 0.75rem;
    padding: 0.2rem 0.6rem;
    border-radius: 12px;
    cursor: pointer;
    color: white;
    background-color: #6f42c1;
    font-weight: 500;
}

.badge-chip.badge-team { background-color: #007bff; }
.badge-chip.badge-hakem { background-color: #6f42c1; }
.badge-chip.badge-dealer { background-color: #fd7e14; }

#badge-changes-modal ul {
    list-style-type: none;
    padding: 0;
}

#badge-changes-modal li {
    padding: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}
#badge-changes-modal li:last-child {
    border-bottom: none;
}

.badge-changes-history {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px dashed var(--border-color);
    font-size: 0.9rem;
}
.badge-changes-history ul {
    padding-right: 1.2rem;
    margin-top: 0.5rem;
    color: #555;
}
