/* =========================================
   APEX ARCHITECTURE: CSS LAYERS & LOGICAL PROPS (FINAL)
   ========================================= */
@layer theme, base, layout, components, utilities;

@layer theme {
    :root {
        --bg-body: #050505; 
        --bg-card: #1c1c1e;
        --bg-card-highlight: #2c2c2e;
        --bg-inset: #222222; 
        --primary: #0a84ff; 
        --primary-dim: rgba(10, 132, 255, 0.15);
        --success: #30d158; 
        --success-dim: rgba(48, 209, 88, 0.15);
        --danger: #ff453a; 
        --danger-dim: rgba(255, 69, 58, 0.15);
        --warning: #ff9f0a;
        --warning-dim: rgba(255, 159, 10, 0.15);
        --text-main: #ffffff; 
        --text-muted: #98989d;
        --border: #38383a;
        --glass: rgba(28, 28, 30, 0.85);
        --safe-bottom: env(safe-area-inset-bottom, 20px);
        --safe-top: max(12px, env(safe-area-inset-top));
    }
}

@layer base {
    html { 
        touch-action: manipulation; 
        -webkit-font-smoothing: antialiased; 
        min-block-size: 100dvh; 
    }
    * { 
        box-sizing: border-box; 
        margin: 0; 
        padding: 0; 
        user-select: none; 
        -webkit-tap-highlight-color: transparent; 
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; 
    }

    body { 
        background: var(--bg-body); 
        color: var(--text-main); 
        min-block-size: 100dvh;
        padding-bottom: calc(160px + var(--safe-bottom)); 
    }
}

@layer layout {
    /* --- HEADER & NAV --- */
    header { 
        position: sticky; 
        inset-block-start: 0; 
        z-index: 50; 
        background: var(--glass); 
        backdrop-filter: blur(20px); 
        -webkit-backdrop-filter: blur(20px); 
        border-bottom: 1px solid rgba(255,255,255,0.1); 
        padding: 12px 16px; 
        padding-top: var(--safe-top); 
    }
    
    .header-top { 
        display: flex; 
        justify-content: space-between; 
        align-items: center; 
    }

    .nav-bar { 
        position: fixed; 
        bottom: 20px; /* Physical bottom for viewport alignment */
        inset-inline-start: 50%; 
        transform: translateX(-50%); 
        inline-size: 90%; 
        max-inline-size: 400px; 
        background: var(--glass); 
        backdrop-filter: blur(20px); 
        -webkit-backdrop-filter: blur(20px); 
        border: 1px solid rgba(255,255,255,0.1); 
        border-radius: 25px; 
        display: flex; 
        padding: 5px; 
        box-shadow: 0 10px 40px rgba(0,0,0,0.5); 
        z-index: 100; 
        margin-bottom: var(--safe-bottom); 
    }

    .container { 
        max-inline-size: 600px; 
        margin-inline: auto; 
        padding: 16px; 
        animation: fadeIn 0.3s ease; 
        container-type: inline-size;
    }
}

@layer components {
    /* --- BUTTONS & INPUTS --- */
    button { cursor: pointer; border: none; font-weight: 600; transition: transform 0.1s, opacity 0.2s; font-family: inherit; }
    button:active { transform: scale(0.97); opacity: 0.8; }
    
    .btn-primary { 
        background: var(--primary); 
        color: white; 
        padding: 14px; 
        border-radius: 12px; 
        inline-size: 100%; 
        font-size: 1rem; 
        box-shadow: 0 4px 12px rgba(10, 132, 255, 0.3); 
    }
    
    .btn-danger { background: var(--danger); color: white; padding: 6px 12px; border-radius: 8px; }
    .btn-ghost { background: transparent; color: var(--primary); padding: 8px; font-size: 0.9rem; }
    .btn-sm { padding: 8px 14px; font-size: 0.85rem; border-radius: 8px; font-weight: 600; }
    
    .nav-btn { 
        flex: 1; 
        background: transparent; 
        border: none; 
        color: var(--text-muted); 
        padding-block: 12px; 
        font-size: 0.7rem; 
        font-weight: 700; 
        letter-spacing: 0.5px; 
        border-radius: 20px; 
        transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1); 
        cursor: pointer; 
    }
    .nav-btn.active { background: var(--bg-card-highlight); color: var(--primary); box-shadow: 0 2px 10px rgba(0,0,0,0.2); }

    input, select { 
        background: #111; 
        border: 1px solid var(--border); 
        color: white; 
        padding: 14px; 
        border-radius: 12px; 
        inline-size: 100%; 
        font-size: 1rem; 
        margin-bottom: 10px; 
        appearance: none; 
        -webkit-appearance: none; 
        transition: border-color 0.2s; 
    }
    input:focus, select:focus { outline: none; border-color: var(--primary); }

    /* --- CARDS & WIDGETS --- */
    .card { 
        background: var(--bg-card); 
        border-radius: 16px; 
        padding: 18px; 
        margin-bottom: 16px; 
        border: 1px solid var(--border); 
        box-shadow: 0 4px 20px rgba(0,0,0,0.2); 
    }

    #team-select-btn { 
        background: rgba(255,255,255,0.1); 
        color: white; 
        border: none; 
        padding: 8px 12px; 
        border-radius: 20px; 
        font-weight: 600; 
        font-size: 0.9rem; 
        display: flex; 
        align-items: center; 
        gap: 8px; 
        cursor: pointer; 
        transition: background 0.2s; 
    }
    #team-select-btn:active { background: rgba(255,255,255,0.2); }

    .format-badge { font-size: 0.7rem; background: var(--warning); color: black; padding: 2px 6px; border-radius: 6px; font-weight: 800; letter-spacing: 0.5px; }

    /* --- LIST ITEMS --- */
    .swipe-wrapper { position: relative; overflow: hidden; border-radius: 12px; margin-bottom: 10px; background: var(--danger); }
    
    .swipe-action-delete { 
        position: absolute; 
        inset-block: 0; 
        inset-inline-end: 0; 
        inline-size: 80px; 
        display: flex; 
        align-items: center; 
        justify-content: center; 
        color: white; 
        font-weight: 800; 
        font-size: 0.75rem; 
        z-index: 1; 
        letter-spacing: 1px; 
    }

    .list-item { 
        position: relative; z-index: 2;
        display: flex; justify-content: space-between; align-items: center; 
        background: var(--bg-card-highlight); 
        padding: 12px; 
        border-inline-start: 4px solid var(--border); 
        inline-size: 100%; 
        transition: transform 0.25s cubic-bezier(0.1, 0.9, 0.2, 1);
        touch-action: pan-y; 
        block-size: 56px; 
    }

    .list-item.active { border-inline-start-color: var(--success); background: linear-gradient(90deg, rgba(48,209,88,0.1) 0%, var(--bg-card-highlight) 100%); }
    .list-item.opponent { border-inline-start-color: var(--danger); }
    .list-item.opponent.active { background: linear-gradient(90deg, rgba(255,69,58,0.1) 0%, var(--bg-card-highlight) 100%); }
    .list-item.absent { opacity: 0.5; border-inline-start-color: #555 !important; background: #252525; }

    .list-item.active, .list-item.active .p-name-text, .list-item.active .status-badge,
    .list-item.opponent.active, .list-item.opponent.active .p-name-text, .list-item.opponent.active .status-badge {
        color: #cccccc !important;
    }

    /* --- SCORER UI --- */
    #scorer-interface {
        position: fixed; inset: 0; 
        background: var(--bg-body); z-index: 2000;
        display: flex; flex-direction: column;
        padding: var(--safe-top) 12px max(12px, var(--safe-bottom)) 12px;
        overflow: hidden; 
        block-size: 100dvh; 
        transition: transform 0.3s ease-in-out;
        container-type: size; 
    }
    #scorer-interface[hidden] { display: none !important; }

    .scorer-header-row { 
        block-size: 5dvh; min-block-size: 35px;
        display: flex; justify-content: space-between; align-items: center; 
        margin-bottom: 0; flex-shrink: 0; 
    }

    .player-card-grid { 
        display: grid; grid-template-columns: 1fr 1fr; gap: 10px; 
        block-size: 38dvh; min-block-size: 250px;
        margin-bottom: 5px; flex-shrink: 0; 
    }

    .p-card-wrapper { display: flex; flex-direction: column; block-size: 100%; overflow: hidden; }
    .p-card { 
        background: var(--bg-card); 
        border-radius: 12px; 
        padding: 0; 
        text-align: center; 
        position: relative; 
        border: 2px solid transparent; 
        transition: all 0.3s ease; 
        display: flex; flex-direction: column; 
        block-size: 100%; 
        overflow: hidden; 
    }

    .p-name-zone { flex: 0 0 20%; display: flex; align-items: center; justify-content: center; padding-inline: 4px; border-bottom: 1px solid var(--border); }
    
    .p-data-zone { 
        flex: 0 0 80%; 
        background: var(--bg-inset); 
        display: flex; flex-direction: column; 
        padding: 8px; justify-content: space-between; 
        position: static; 
    }
    
    .p-meta { 
        display: flex; align-items: flex-start; justify-content: center; 
        font-size: 0.7rem; color: var(--text-muted); font-weight: 600; 
        white-space: nowrap; margin-bottom: 5px;
    }
    
    /* NEW: Container for Score and Label */
    .score-wrapper {
        position: relative; 
        flex-grow: 1; 
        display: flex; 
        align-items: center; 
        justify-content: center; 
        /* FORCE WIDTH TO 100% TO PUSH LABEL TO EDGE */
        width: 100%;
    }

    .p-score { 
        font-size: 3.8rem; font-weight: 800; line-height: 1; 
        margin: 0;
    }

    /* RESTORED: Layout for Timeout Buttons */
    .timeout-container { 
        display: flex; align-items: center; justify-content: center; 
        gap: 8px; margin-block: 5px; min-block-size: 30px;
    }

    /* RESTORED: Layout for Score Controls */
    .p-score-controls { 
        block-size: 30%; min-block-size: 50px;
        display: flex; gap: 8px; inline-size: 100%; align-items: center; 
    }

    .hill-badge { 
        position: absolute; 
        inset-block-start: 0; 
        inset-inline-start: 50%; 
        transform: translateX(-50%); 
        font-size: 0.65rem; font-weight: 900; 
        padding: 2px 10px; 
        border-bottom-left-radius: 8px; border-bottom-right-radius: 8px; 
        text-transform: uppercase; 
        display: none; 
        box-shadow: 0 2px 5px rgba(0,0,0,0.5); 
        z-index: 10; 
    }

    /* ON TOP LABEL (Absolute inside .score-wrapper) */
    .ontop-inner { 
        position: absolute; 
        
        /* PIN TO RIGHT EDGE */
        right: 0; 
        
        /* EXACT CENTER of the wrapper */
        top: 50%; 
        
        transform: translateY(-50%) rotate(180deg); 
        writing-mode: vertical-rl; text-orientation: mixed; 
        background: var(--primary); color: white; 
        font-size: 0.6rem; font-weight: 800; 
        padding: 8px 2px; 
        border-top-right-radius: 6px; border-bottom-right-radius: 6px; 
        display: none; z-index: 5; 
    }
    .p-card.is-on-top .ontop-inner { display: block; }

    .stats-grid {
        display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
        block-size: 18dvh; flex-shrink: 0; margin-bottom: 5px;
    }

    .stat-card {
        background: var(--bg-inset); border: 1px solid var(--border); border-radius: 12px;
        display: flex; flex-direction: column; 
        justify-content: space-between; 
        padding: 8px;
    }

    .info-row { 
        block-size: 8dvh; 
        display: flex; gap: 10px; 
        flex-shrink: 0; margin-bottom: 5px; 
    }
    
    .actions-row { 
        block-size: 7dvh; min-block-size: 40px;
        display: flex; gap: 10px; flex-shrink: 0; margin-bottom: 5px; 
        padding-inline: 5px; 
    }
    .actions-row button { 
        block-size: 100%; font-size: 1rem; font-weight: 700; border-radius: 10px;
        max-block-size: 55px;
    }

    .rack-hist-card { 
        block-size: 16dvh; 
        background: var(--bg-card); border-radius: 12px; overflow: hidden; border: 1px solid var(--border); 
        display: flex; flex-direction: column; flex-shrink: 0; 
        margin-bottom: 5px; 
    }

    /* --- HISTORY & SESSION --- */
    .hist-card { background: #252525; border-radius: 12px; margin-bottom: 12px; overflow: hidden; border: 1px solid #333; }
    .hist-summary { width: 100%; text-align: left; color: inherit; padding: 16px; display: flex; justify-content: space-between; align-items: center; background: transparent; border: none; }
    
    .sess-row { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; padding: 14px; cursor: pointer; }
    .sess-score { font-weight: 800; font-size: 1rem; padding: 4px 12px; border-radius: 6px; text-align: center; white-space: nowrap; min-inline-size: 60px; }
    
    /* --- MODALS (DIALOGS) --- */
    dialog { border: none; margin: 0; padding: 0; background: transparent; color: white; max-inline-size: 100%; max-block-size: 100%; display: none; }
    dialog::backdrop { background: rgba(0,0,0,0.6); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px); }
    
    dialog.modal-sheet[open] { display: flex; position: fixed; inset: 0; justify-content: center; align-items: flex-end; }
    dialog.modal-sheet .modal-content-wrapper { 
        background: #1c1c1e; 
        inline-size: 100%; max-inline-size: 500px; 
        margin-inline: auto; 
        border-top-left-radius: 24px; border-top-right-radius: 24px; 
        padding: 24px; padding-bottom: max(24px, var(--safe-bottom)); 
        border-top: 1px solid rgba(255,255,255,0.1); 
        transform: translateY(100%); transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1); 
    }
    dialog.modal-sheet[open] .modal-content-wrapper { transform: translateY(0); }
    
    dialog.modal-center[open] { display: flex; position: fixed; inset: 0; justify-content: center; align-items: center; }
    dialog.modal-center .modal-content-wrapper { 
        background: #2c2c2e; border: 1px solid #444; border-radius: 16px; 
        inline-size: 90%; max-inline-size: 350px; 
        padding: 24px; 
        opacity: 0; transform: scale(0.9); transition: all 0.2s ease; 
    }
    dialog.modal-center[open] .modal-content-wrapper { opacity: 1; transform: scale(1); }
}

@layer utilities {
    .flex { display: flex; align-items: center; gap: 10px; }
    .between { justify-content: space-between; }
    .center { justify-content: center; }
    .column { flex-direction: column; }
    
    .list-item .flex { overflow: hidden; white-space: nowrap; max-inline-size: 65%; }
    .p-name-text { font-weight: 500 !important; overflow: hidden; text-overflow: ellipsis; display: block; }
    .p-name { font-size: 0.9rem; font-weight: 700; line-height: 1.1; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

    .btn-control { block-size: 100%; font-size: 2rem; border-radius: 8px; display: flex; align-items: center; justify-content: center; border: 1px solid rgba(255,255,255,0.1); background: rgba(255,255,255,0.05); transition: background 0.1s; }
    .btn-minus { flex: 1; color: var(--danger); }
    .btn-plus { flex: 3; color: var(--success); }
    .btn-minus:active { background: var(--danger); color: white; border-color: transparent; }
    .btn-plus:active { background: var(--success); color: white; border-color: transparent; }

    /* Stats & Info */
    .stat-label { text-align: center; font-size: 0.65rem; color: #aaa; font-weight: bold; margin-bottom: 2px; }
    .stat-value { flex-grow: 1; display: flex; align-items: center; justify-content: center; font-size: 2.2rem; font-weight: 800; line-height: 1; }
    .stat-controls { display: flex; gap: 8px; inline-size: 100%; block-size: 45%; max-block-size: 50px; }
    .stat-controls .btn-control { font-size: 1.5rem; }

    .info-box { flex: 1; text-align: center; background: var(--bg-inset); padding: 2px; border-radius: 12px; border: 1px solid var(--border); display: flex; flex-direction: column; justify-content: center; }
    .info-box-label { font-size: 0.65rem; color:#aaa; font-weight:bold; margin-bottom:0; }
    .info-box-val { font-size: 1.1rem; font-weight:800; font-family: monospace; }
    
    .status-badge { font-size: 0.7rem; font-weight: 700; color: var(--text-muted); display: flex; align-items: center; gap: 8px; flex-shrink: 0; z-index: 10; }
    .owed-badge { background: var(--danger); color: white; border-radius: 50%; inline-size: 20px; block-size: 20px; display: flex; align-items: center; justify-content: center; font-size: 0.7rem; font-weight: 800; }
    .btn-absent-toggle { background: #333; color: #888; border: 1px solid #444; padding: 4px 8px; border-radius: 6px; font-size: 0.65rem; font-weight: 800; cursor: pointer; }
    
    .p-card.hill-orange { border-color: var(--warning); background: var(--warning-dim); }
    .p-card.hill-orange .hill-badge { display: block; background: var(--warning); color: black; }
    .p-card.winner-green { border-color: var(--success); background: var(--success-dim); }
    .p-card.winner-green .hill-badge { display: block; background: var(--success); color: white; }

    .to-btn { background: #333; border: 1px solid #444; color: #888; padding-inline: 12px; block-size: 28px; border-radius: 6px; font-size: 0.7rem; font-weight: 800; letter-spacing: 1px; cursor: pointer; transition: all 0.2s; display: flex; align-items: center; justify-content: center;}
    .to-btn.used { background: var(--warning); color: black; border-color: var(--warning); box-shadow: 0 0 10px var(--warning-dim); }

    /* Rack History */
    .rack-hist-scroll-area { overflow-y: auto; flex-grow: 1; scrollbar-width: none; -ms-overflow-style: none; }
    .rack-hist-scroll-area::-webkit-scrollbar { display: none; }
    .rack-hist-header { display: grid; grid-template-columns: 0.8fr 1fr 1fr 1fr 1.5fr; background: #252525; padding: 4px 10px; font-size: 0.65rem; font-weight: 800; color: #aaa; text-align: center; border-bottom: 1px solid #333; flex-shrink: 0; }
    .rack-hist-row { display: grid; grid-template-columns: 0.8fr 1fr 1fr 1fr 1.5fr; padding: 4px 10px; font-size: 0.8rem; color: white; text-align: center; border-bottom: 1px solid #222; }
    .rack-hist-row:last-child { border-bottom: none; }
    .rack-hist-8ball .rack-hist-header, .rack-hist-8ball .rack-hist-row { grid-template-columns: 0.8fr 1fr 1fr 1fr 1.5fr !important; }

    .select-wrap { position: relative; inline-size: 100%; }
    .select-wrap::after { content: '▼'; font-size: 0.7rem; color: #666; position: absolute; inset-inline-end: 15px; inset-block-start: 50%; transform: translateY(-70%); pointer-events: none; }

    /* Animations */
    @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
    @keyframes slideDown { from { transform: translateY(-20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

    /* Toast */
    #toast-container { position: fixed; inset-block-start: 10px; inset-inline-start: 50%; transform: translateX(-50%); z-index: 4000; inline-size: 90%; max-inline-size: 400px; pointer-events: none; }
    .toast { background: rgba(30,30,30, 0.95); backdrop-filter: blur(10px); color: white; padding: 16px; border-radius: 12px; margin-bottom: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.3); display: flex; align-items: center; justify-content: space-between; animation: slideDown 0.3s forwards; pointer-events: auto; border: 1px solid #333; }

    /* Predictor */
    .predictor-bar-container { inline-size: 100%; block-size: 6px; background: #333; border-radius: 3px; margin-top: 8px; overflow: hidden; }
    .predictor-bar { block-size: 100%; background: var(--success); inline-size: 0%; transition: inline-size 0.5s ease-out; }
    .pred-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
    .pred-badge { font-size: 0.75rem; padding: 4px 10px; border-radius: 6px; background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1); }
    .pred-safe { color: var(--success); border-color: var(--success-dim); }
    .pred-risk { color: var(--danger); border-color: var(--danger-dim); opacity: 0.8; }

    /* Setup & Session Grids */
    .setup-row { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
    .btn-ontop { background: #222; color: #666; border: 1px solid #444; padding-inline: 12px; block-size: 48px; border-radius: 12px; font-size: 0.7rem; font-weight: 800; text-transform: uppercase; white-space: nowrap; cursor: pointer; transition: all 0.2s; }
    .btn-ontop.active { background: var(--primary); color: white; border-color: var(--primary); box-shadow: 0 0 15px var(--primary-dim); }

    .sess-item-wrapper { background: var(--bg-card-highlight); border-radius: 12px; margin-bottom: 8px; overflow: hidden; }
    .sess-p1 { text-align: end; font-size: 0.9rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .sess-p2 { text-align: start; font-size: 0.9rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .sess-details { display: none; background: #111; border-top: 1px solid #333; padding: 10px; }
    .sess-details.open { display: block; animation: fadeIn 0.2s; }
    .sess-grid-9b { display: grid; grid-template-columns: 0.5fr 1.2fr 0.8fr 0.8fr 1.2fr 1.2fr; text-align: center; }
    .sess-grid-8b { display: grid; grid-template-columns: 0.5fr 1.2fr 0.8fr 1.2fr 1.2fr; text-align: center; }
    .sess-rack-header { font-size: 0.65rem; color: #888; font-weight: bold; padding-bottom: 6px; border-bottom: 1px solid #333; margin-bottom: 6px; }
    .sess-rack-row { font-size: 0.75rem; padding-block: 4px; color: #ccc; }
    
    /* Utility Overrides */
    [hidden] { display: none !important; }
    h3 { font-size: 1.1rem; font-weight: 700; margin-bottom: 0; letter-spacing: -0.5px; }
    h4 { font-size: 0.9rem; font-weight: 600; margin-bottom: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px; }
}
