:root {
    --bg: #F4F5F7;
    --surface: #FFFFFF;
    --surface-2: #ECEEF1;
    --text: #1D1F23;
    --muted: #6B7280;
    --border: #E3E5E9;
    --accent: #4263EB;
    --danger: #E03131;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .05);
    --radius: 14px;
    --topbar-h: 64px;
    --note-size: 15px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111215;
        --surface: #1C1D21;
        --surface-2: #26282D;
        --text: #ECEDEF;
        --muted: #9097A0;
        --border: #2B2D33;
        --accent: #748FFC;
        --danger: #FF6B6B;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; }

.sr-only {
    position: absolute; width: 1px; height: 1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Top bar ---------- */

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    -webkit-backdrop-filter: saturate(180%) blur(16px);
    backdrop-filter: saturate(180%) blur(16px);
    border-bottom: 1px solid var(--border);
}

.topbar-row {
    display: flex;
    align-items: center;
    gap: 12px;
    height: 56px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 8px;
    color: inherit;
    text-decoration: none;
    font-weight: 700;
    font-size: 17px;
}
.brand img { border-radius: 7px; }

.nav {
    display: flex;
    align-items: center;
    gap: 4px;
    margin: 0 auto;
}

.range {
    min-width: 150px;
    text-align: center;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.actions { display: flex; align-items: center; gap: 4px; }

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--text);
    cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg {
    width: 22px; height: 22px;
    fill: none; stroke: currentColor; stroke-width: 2;
    stroke-linecap: round; stroke-linejoin: round;
}
.icon-btn.spinning svg { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.pill-btn {
    height: 34px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    font-weight: 600;
    cursor: pointer;
}
.pill-btn:hover { background: var(--surface-2); }

.daystrip { display: none; }

/* ---------- Board: 5 day columns ---------- */

.board {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 14px;
    padding: 16px max(16px, env(safe-area-inset-right)) calc(32px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    max-width: 1800px;
    margin: 0 auto;
}

.day { min-width: 0; scroll-margin-top: calc(var(--topbar-h) + 8px); }

.day-head {
    display: flex;
    align-items: baseline;
    gap: 6px;
    margin: 0 0 10px;
    padding: 0 4px;
    font-size: 15px;
    font-weight: 700;
}
.day-head .date { color: var(--muted); font-weight: 500; }
.day.today .day-head .dow { color: var(--accent); }
.day-head .badge {
    margin-left: auto;
    padding: 1px 8px;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .04em;
    align-self: center;
}

.lessons { display: flex; flex-direction: column; gap: 10px; }

.empty {
    padding: 18px 12px;
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    color: var(--muted);
    text-align: center;
    font-size: 14px;
}
.board.loading .empty { visibility: hidden; }

/* ---------- Lesson card ---------- */

.lesson {
    --c: #868E96;
    position: relative;
    padding: 10px 12px 10px 16px;
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    cursor: text;
    transition: opacity .2s, border-color .15s;
}
.lesson::before {
    content: "";
    position: absolute;
    left: 0; top: 10px; bottom: 10px;
    width: 4px;
    border-radius: 0 4px 4px 0;
    background: var(--c);
}
.lesson:focus-within { border-color: var(--c); }
.lesson.past:not(:focus-within) { opacity: .6; }
.lesson.now { border-color: var(--c); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 22%, transparent); }

.lesson-head {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 24px;
    margin-bottom: 4px;
}

.cls {
    flex: none;
    padding: 1px 8px;
    border-radius: 6px;
    background: color-mix(in srgb, var(--c) 15%, transparent);
    color: var(--c);
    font-weight: 700;
    font-size: 14px;
    white-space: nowrap;
}
@media (prefers-color-scheme: light) {
    .cls { color: color-mix(in srgb, var(--c) 80%, #000); }
}

.time {
    color: var(--muted);
    font-size: 13px;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.extra {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted);
    font-size: 13px;
}

.state {
    margin-left: auto;
    flex: none;
    color: var(--muted);
    font-size: 12px;
    transition: opacity .4s;
}
.state.fade { opacity: 0; }
.state.error { color: var(--danger); font-weight: 600; }

.note {
    display: block;
    width: 100%;
    min-height: 1.45em;
    margin: 0;
    padding: 2px 0;
    border: 0;
    outline: 0;
    resize: none;
    overflow: hidden;
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: var(--note-size);
    line-height: 1.45;
}
.note::placeholder { color: var(--muted); opacity: .7; }

.removed-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px dashed var(--border);
    color: var(--danger);
    font-size: 13px;
}
.removed-bar button {
    margin-left: auto;
    padding: 2px 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    font-size: 13px;
    cursor: pointer;
}
.lesson.removed .cls { text-decoration: line-through; }

/* ---------- Toast ---------- */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(20px + env(safe-area-inset-bottom));
    transform: translate(-50%, 20px);
    max-width: calc(100% - 32px);
    padding: 10px 16px;
    border-radius: 12px;
    background: var(--text);
    color: var(--bg);
    font-size: 14px;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s, transform .2s;
    z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Login ---------- */

.login-page {
    display: grid;
    place-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 16px;
}
.login-card {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    width: 100%;
    max-width: 340px;
    padding: 32px 24px;
    border-radius: 20px;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    text-align: center;
}
.login-logo { align-self: center; border-radius: 16px; }
.login-card h1 { margin: 4px 0 8px; font-size: 24px; }
.login-card input, .login-card button {
    height: 46px;
    border-radius: 12px;
    font-size: 16px;
}
.login-card input {
    padding: 0 14px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
}
.login-card button {
    border: 0;
    background: var(--accent);
    color: #fff;
    font-weight: 600;
    cursor: pointer;
}
.login-error { margin: 0; color: var(--danger); font-size: 14px; }

/* ---------- Medium screens: smaller columns ---------- */

@media (max-width: 1200px) and (min-width: 821px) {
    .board { gap: 10px; padding-left: 10px; padding-right: 10px; }
    .lesson { padding: 8px 10px 8px 14px; }
    .extra { display: none; }
}

/* ---------- Phone / narrow: one column, stacked days ---------- */

@media (max-width: 820px) {
    :root { --note-size: 16px; } /* 16px stops iOS from zooming on focus */
    .brand, .logout { display: none; }
    .nav { margin-left: 0; }
    .topbar-row { gap: 4px; height: 52px; }
    .icon-btn { width: 38px; height: 38px; }
    .range { min-width: 0; font-size: 15px; }

    .daystrip {
        display: flex;
        gap: 6px;
        padding: 0 0 10px;
    }
    .daystrip button {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 5px 0 6px;
        border: 0;
        border-radius: 12px;
        background: transparent;
        cursor: pointer;
    }
    .daystrip .d-dow { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
    .daystrip .d-num { font-size: 18px; font-weight: 700; line-height: 1.2; }
    .daystrip .d-count { font-size: 11px; color: var(--muted); }
    .daystrip button.is-today .d-num { color: var(--accent); }
    .daystrip button.active { background: var(--surface); box-shadow: var(--shadow); }

    .board {
        grid-template-columns: 1fr;
        gap: 22px;
        padding-top: 12px;
    }
    .day-head { font-size: 17px; }
    .lesson { padding: 12px 14px 12px 18px; }
}
