:root {
    /* Core Colors */
    --bg-color: #0F1115;
    --bg-card: #16191E;
    --bg-input: #000000;

    --text-primary: #FFFFFF;
    --text-secondary: #9CA3AF;

    --accent-color: #F39C12;
    /* Default Orange */
    --accent-red: #DA292A;
    --success-color: #2ECC71;

    --nav-height: 60px;
    /* Safe area added dynamically */
    --header-height: 56px;

    --card-radius: 12px;
    --input-radius: 8px;

    --border-color: rgba(255, 255, 255, 0.08);

    /* Dynamic Backgrounds */
    --accent-fade: rgba(243, 156, 18, 0.15);
}

html.theme-red {
    --accent-color: #DA292A;
    --accent-fade: rgba(218, 41, 42, 0.15);
}

html.light-mode {
    --bg-color: #F0F2F5;
    --bg-card: #FFFFFF;
    --bg-input: #F9FAFB;
    --text-primary: #111827;
    --text-secondary: #6B7280;
    --border-color: rgba(0, 0, 0, 0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
    /* Remove blue highlight */
}

body {
    font-family: 'Chakra Petch', sans-serif;
    background: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    /* Prevent body scroll */
    position: fixed;
    width: 100%;
    height: 100%;
    user-select: none;
}

/* === HEADER === */
.app-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    /* justify-content: space-between; REMOVED */
    padding-left: 8px;
    /* Reduced padding */
    padding-right: 8px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border-color);
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 1000;
    /* Safe Area Top */
    padding-top: env(safe-area-inset-top);
    height: calc(var(--header-height) + env(safe-area-inset-top));
}

.filters-scroll {
    display: flex;
    width: 100%;
    height: 100%;
    align-items: center;
    gap: 6px;
    /* No scrolling, force fit */
    overflow: hidden;
}

.app-header::-webkit-scrollbar {
    display: none;
}

.header-actions {
    display: none;
    /* Removed from header */
}

/* History Actions Bar */


.icon-btn {
    background: transparent;
    border: none;
    font-size: 1.2rem;
    color: var(--text-primary);
    padding: 8px;
    border-radius: 50%;
}

/* === APP CONTAINER & VIEWS === */
#app-container {
    position: absolute;
    top: calc(var(--header-height) + env(safe-area-inset-top));
    bottom: calc(var(--nav-height) + env(safe-area-inset-bottom));
    width: 100%;
    overflow: hidden;
}

.view {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
    transition: opacity 0.2s ease, transform 0.2s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 20px;
    background: var(--bg-color);
    /* Prevent overlap visibility */
}

.view.active-view {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
    z-index: 10;
}

.content-padding {
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* === COMPONENTS === */

/* CARDS */
.stats-card,
.input-card,
.bonus-card {
    background: var(--bg-card);
    border-radius: var(--card-radius);
    padding: 16px;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.stats-card {
    display: flex;
    align-items: center;
    gap: 16px;
}

#rankLogo {
    width: 70px;
    height: 70px;
}

#currentStats {
    display: flex;
    flex-direction: column;
}

#statRank {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
}

#statRP {
    font-size: 1rem;
    color: var(--accent-color);
    font-weight: 600;
}

#statAvg {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.next-rank-info {
    margin-left: auto;
    /* Push to right */
    text-align: right;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.next-rank-label {
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-weight: 600;
}

.next-rank-value {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--text-primary);
}

.next-rank-games {
    font-size: 0.8rem;
    margin-top: 2px;
}

.trend-up {
    color: var(--success-color);
}

.trend-down {
    color: var(--accent-red);
}

/* INPUTS */
.input-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 12px;
}

.input-group label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

input[type="number"],
input[type="text"],
input[type="datetime-local"],
input[type="date"],
input[type="time"],
select {
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 12px 14px;
    border-radius: var(--input-radius);
    font-family: inherit;
    font-size: 1rem;
    outline: none;
    width: 100%;

    /* Ensure native pickers are dark */
    color-scheme: dark;
}

input:focus,
select:focus {
    border-color: var(--accent-color);
}

/* BUTTONS */
.primary-btn {
    background: var(--accent-color);
    color: #fff;
    border: none;
    padding: 14px;
    border-radius: var(--input-radius);
    font-size: 1rem;
    font-weight: 700;
    text-transform: uppercase;
    width: 100%;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(243, 156, 18, 0.2);
}

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

.secondary-btn {
    background: transparent;
    border: 1px solid var(--accent-color);
    color: var(--accent-color);
    padding: 12px;
    border-radius: var(--input-radius);
    font-weight: 600;
    width: 100%;
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.2s;
}

.secondary-btn:active {
    background: rgba(243, 156, 18, 0.1);
    transform: scale(0.98);
}

/* PARTY SCROLLER */
.party-scroller {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 4px;
}

.party-btn {
    background: var(--bg-input);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    padding: 10px 4px;
    /* Reduced side padding slightly */
    min-width: 0;
    /* Important for flex shrinking */
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 600;

    display: flex;
    align-items: center;
    /* justify-content: center; REMOVED */

    transition: all 0.2s;
    height: 48px;
    /* Fixed height for consistency */
}

.party-btn span {
    display: block;
    margin: 0 auto;
    /* Hybrid Alignment: Center if fits, Left if long */
    max-width: 100%;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.party-btn.active {
    background: var(--accent-color);
    color: #fff !important;
    border: 1px solid var(--accent-color);
    /* Maintain border width to prevent shift */
}

/* HISTORY LIST */
.sticky-filters {
    position: sticky;
    top: 0;
    background: var(--bg-card);
    z-index: 5;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    gap: 12px;
}

/* Consolidated Filter Styles */
.filters-scroll {
    /* Already defined in header, but ensuring no conflict here if class used elsewhere */
    display: flex;
    width: 100%;
    align-items: center;
    gap: 6px;
    overflow: hidden;
}

.filter-chip {
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);

    /* Layout for fitting */
    flex: 1;
    min-width: 0;
    padding: 0 4px;
    display: flex;
    /* justify-content: center; REMOVED to allow margin:auto to handle safe centering */
    align-items: center;

    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;

    overflow: hidden;
    /* text-overflow removal from parent, handled in span */

    transition: all 0.2s;
    height: 32px;
}

.filter-chip span {
    display: block;
    margin: 0 auto;
    /* Center if space available, Left align if overflow */
    max-width: 100%;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.filter-chip.active {
    background: var(--accent-color);
    color: #fff;
    border-color: var(--accent-color);
    box-shadow: 0 4px 10px rgba(243, 156, 18, 0.2);
    font-weight: 700;
}

.filter-chip:active {
    transform: scale(0.96);
    opacity: 0.8;
}




.icon-btn-danger {
    background: rgba(218, 41, 42, 0.1);
    color: var(--accent-red);
    border: none;
    border-radius: 8px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
}

.matches-list {
    display: flex;
    flex-direction: column;
}

.list-header,
.list-row {
    display: grid;
    /* Use minmax(0, 1fr) to prevent overflow from long strings blowing out column width */
    grid-template-columns: 40px 80px minmax(0, 1fr) minmax(0, 1fr) 40px;
    align-items: center;
    padding: 12px 8px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.85rem;
}

.list-header {
    font-weight: 700;
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-transform: uppercase;
}

.party-list-badge {
    background: rgba(255, 255, 255, 0.05);
    padding: 4px 8px;
    /* Slightly more vertical padding */
    border-radius: 4px;
    color: var(--text-secondary);
    display: flex;
    /* Flexbox for vertical stacking */
    flex-direction: column;
    justify-content: center;
    max-width: 100%;
    /* overflow: hidden; Removed from parent to allow children to handle width */
    vertical-align: middle;
}

.party-member-row {
    display: block;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.3;
}

.rp-val {
    font-weight: 700;
}

.rp-pos {
    color: var(--success-color);
}

.rp-neg {
    color: var(--accent-red);
}

/* STATS TABLE */
.table-container {
    background: var(--bg-card);
    border-radius: var(--card-radius);
    overflow-x: auto;
    /* Enable horizontal scroll */
    border: 1px solid var(--border-color);
}

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

#summaryTable th,
#summaryTable td {
    padding: 6px 4px;
    /* Reduced padding for mobile */
    border: 1px solid var(--border-color);
    text-align: center;
    font-size: 0.8rem;
    /* Slightly smaller data font */
}

#summaryTable th {
    background: rgba(255, 255, 255, 0.03);
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.65rem;
    /* Smaller headers */
}

.rank-cell {
    font-weight: 700;
    color: #fff;
    text-align: left !important;
    padding-left: 8px !important;
    font-size: 0.75rem !important;
    /* Smaller rank text */

    /* Sticky Column Logic */
    position: sticky;
    left: 0;
    z-index: 2;
    background-color: var(--bg-card);
    /* Ensure opacity over scrolling content */
    box-shadow: 2px 0 5px rgba(0, 0, 0, 0.5);
    /* Shadow to indicate float */
}

th {
    background: rgba(0, 0, 0, 0.2);
    /* Default th was left, now overriding with #summaryTable above */
    font-size: 0.7rem;
    color: var(--text-secondary);
    padding: 10px;
    text-transform: uppercase;
}

td {
    padding: 12px 10px;
    font-size: 0.85rem;
    border-bottom: 1px solid var(--border-color);
}

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

.cell-bad {
    background: rgba(218, 41, 42, 0.1);
    color: var(--accent-red);
}

.cell-good {
    background: rgba(46, 204, 113, 0.1);
    color: var(--success-color);
}

/* SETTINGS */
.icon-toggle {
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    cursor: pointer;
    transition: transform 0.2s;
}

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

/* Bonus Toggle */
.toggle-header {
    background: transparent;
    border: none;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.9rem;
    padding: 8px 0;
    /* Vertical only, align left */
    cursor: pointer;
}

.settings-group {
    background: var(--bg-card);
    border-radius: var(--card-radius);
    padding: 16px;
    margin-bottom: 16px;
}

.settings-group h3 {
    margin-bottom: 12px;
    font-size: 0.9rem;
    color: var(--accent-color);
    text-transform: uppercase;
}

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

.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
}

/* BOTTOM NAV */
.bottom-nav {
    position: fixed;
    bottom: 0;
    width: 100%;
    background: var(--bg-card);
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: calc(var(--nav-height) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 1000;
}

.nav-item {
    background: transparent;
    border: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    color: var(--text-secondary);
    padding: 8px;
    flex: 1;
}

.nav-item ion-icon {
    font-size: 1.5rem;
    transition: transform 0.2s;
}

.nav-item span {
    font-size: 0.7rem;
    font-weight: 500;
}

.nav-item.active {
    color: var(--accent-color);
}

.nav-item.active ion-icon {
    transform: translateY(-2px);
}

/* Hypo Stats */
.hypo-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.hypo-item {
    background: var(--bg-card);
    padding: 10px 14px;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    border-left: 3px solid transparent;
}

/* Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 28px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #374151;
    transition: .4s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--accent-color);
}

input:checked+.slider:before {
    transform: translateX(22px);
}

/* Helper classes */
.hidden {
    display: none !important;
}

.active {
    display: flex;
}



/* ANIMATIONS */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.animate-slide-in {
    opacity: 0;
    /* Start invisible */
    animation: slideIn 0.4s ease-out forwards;
}

/* Smart List Animations */
#recentMatches {
    height: 115px;
    /* STRICTLY FIXED HEIGHT to prevent ANY layout shift */
    overflow: hidden;
    position: relative;
}

.match-item {
    transition: all 0.5s ease;
    overflow: hidden;
    /* Important for height anim */
}

/* New Item Entering (Slide Down) */
.slide-enter {
    animation: slideEnter 0.5s ease-out forwards;
}

@keyframes slideEnter {
    0% {
        opacity: 0;
        transform: translateY(-20px);
        max-height: 0;
        margin-top: -8px;
        /* Counteract padding/gap */
        padding: 0;
    }

    100% {
        opacity: 1;
        transform: translateY(0);
        max-height: 100px;
        /* Arbitrary large enough height */
        margin-top: 0;
        padding: 8px 0;
    }
}

/* Old Item Leaving (Slide Up/Fade Out) */
.slide-leave {
    /* REMOVED absolute pos to restore natural flow/push */
    animation: slideLeave 0.5s ease-in forwards;
}

@keyframes slideLeave {
    0% {
        opacity: 1;
        transform: scale(1);
        max-height: 100px;
        margin-bottom: 0;
        padding: 8px 0;
    }

    100% {
        opacity: 0;
        transform: scale(0.9);
        max-height: 0;
        margin-bottom: -8px;
        padding: 0;
    }
}

/* Light Mode Visibility Fixes */
.recent-party-txt {
    text-align: right;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* Warnings (Conditional Formatting) */
.bg-warn {
    background-color: rgba(128, 128, 128, 0.15);
    /* Reverted to Gray per user request */
    color: var(--accent-color);
    font-weight: 700;
}

.bg-void {
    background-color: rgba(0, 0, 0, 0.4);
    color: #fff;
    font-weight: 700;
}

/* Light Mode Overrides */
html.light-mode .bg-warn {
    /* Keep dynamic background but make text dark/readable */
    color: var(--text-primary) !important;
    /* Increase opacity slightly for visibility on white? 
       Actually user wants readable text. text-primary is best. */
}

html.light-mode .bg-void {
    background-color: #374151;
    /* Dark Gray */
    color: #fff;
}