:root {
    --lego: #d01012;
    --lego-light: #ff4d3d;
    --bg: #f4f2ee;
    --card: #ffffff;
    --text: #1c1a17;
    --muted: #726c64;
    --faint: #aca59d;
    --line: #e2ddd5;
    --green: #2e9e5b;
    --orange: #e0791f;
    --purple: #8a4fc7;
    --accent: var(--lego);
    --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #141311;
        --card: #211f1c;
        --text: #f1ede7;
        --muted: #a59e95;
        --faint: #6d665f;
        --line: #38342f;
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: var(--ui);
    -webkit-tap-highlight-color: transparent;
}

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 760px;
    margin: 0 auto;
    padding: env(safe-area-inset-top) 12px env(safe-area-inset-bottom);
}

.topbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0 4px;
}

.brand {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.15rem;
    font-weight: 600;
}

/* A 2×2 brick drawn with CSS: a red tile with four studs. */
.logo {
    position: relative;
    width: 28px;
    height: 28px;
    border-radius: 7px;
    background: linear-gradient(var(--lego-light), var(--lego));
    background-image:
        radial-gradient(circle at 30% 30%, #ff6a5c 0 4px, #9e0a0c 4.5px 5.5px, transparent 6px),
        radial-gradient(circle at 70% 30%, #ff6a5c 0 4px, #9e0a0c 4.5px 5.5px, transparent 6px),
        radial-gradient(circle at 30% 70%, #ff6a5c 0 4px, #9e0a0c 4.5px 5.5px, transparent 6px),
        radial-gradient(circle at 70% 70%, #ff6a5c 0 4px, #9e0a0c 4.5px 5.5px, transparent 6px),
        linear-gradient(var(--lego-light), var(--lego));
    flex: none;
}

.icon-btn {
    width: 42px;
    height: 42px;
    border: none;
    border-radius: 12px;
    background: transparent;
    font-size: 1.35rem;
    display: grid;
    place-items: center;
}

.icon-btn:active { background: var(--line); }

.switches {
    display: flex;
    gap: 8px;
    padding: 4px 0;
}

.segmented {
    flex: 1;
    display: flex;
    padding: 3px;
    border-radius: 12px;
    background: var(--line);
}

.segmented button {
    flex: 1;
    padding: 8px 4px;
    border: none;
    border-radius: 9px;
    background: transparent;
    font-weight: 500;
}

.segmented button.on {
    background: var(--card);
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.15);
    font-weight: 600;
}

.status {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 4px;
    font-size: 0.9rem;
    color: var(--muted);
}

.status strong { color: var(--text); }

main {
    flex: 1;
    min-height: 0;
    display: flex;
}

.card {
    position: relative;
    flex: 1;
    overflow-y: auto;
    padding: 22px 20px 28px;
    border: 1px solid var(--line);
    border-top: 6px solid var(--accent);
    border-radius: 20px;
    background: var(--card);
    box-shadow: 0 6px 18px rgb(0 0 0 / 0.08);
    touch-action: pan-y;
    user-select: text;
}

.card.flipped { border-color: color-mix(in srgb, var(--accent) 45%, transparent); border-top-color: var(--accent); }

.badges {
    display: flex;
    gap: 6px;
    align-items: center;
    min-height: 26px;
    margin-bottom: 12px;
}

.badges .label {
    display: flex;
    align-items: center;
    gap: 7px;
    margin-right: auto;
    min-width: 0;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--muted);
}

.badges .label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.stud {
    flex: none;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--dot, var(--accent));
    box-shadow: inset 0 -2px 0 rgb(0 0 0 / 0.25), inset 0 1px 0 rgb(255 255 255 / 0.35);
}

.badge {
    flex: none;
    padding: 2px 9px;
    border-radius: 99px;
    font-size: 0.78rem;
    font-weight: 600;
}

.badge.known { background: color-mix(in srgb, var(--green) 18%, transparent); color: var(--green); }
.badge.review { background: color-mix(in srgb, var(--orange) 18%, transparent); color: var(--orange); }
.badge.notes { background: color-mix(in srgb, var(--purple) 18%, transparent); color: var(--purple); }

.note-btn {
    flex: none;
    min-height: 28px;
    padding: 2px 10px;
    border: 1px solid var(--line);
    border-radius: 99px;
    background: var(--card);
    font-size: 0.8rem;
    font-weight: 600;
}

.card h1 {
    margin: 4px 0 6px;
    font-size: 1.9rem;
    line-height: 1.15;
}

.card.flipped h1 { font-size: 1.55rem; }

.subtitle {
    margin: 0 0 14px;
    color: var(--muted);
    font-size: 1rem;
    line-height: 1.4;
}

.lego-view {
    display: flex;
    gap: 12px;
    margin: 14px 0;
    padding: 14px;
    border-radius: 14px;
    background: color-mix(in srgb, var(--accent) 9%, transparent);
}

.lego-view .logo { width: 24px; height: 24px; border-radius: 6px; background: var(--accent); background-image:
    radial-gradient(circle at 30% 30%, rgb(255 255 255 / 0.35) 0 3px, rgb(0 0 0 / 0.25) 3.5px 4.5px, transparent 5px),
    radial-gradient(circle at 70% 30%, rgb(255 255 255 / 0.35) 0 3px, rgb(0 0 0 / 0.25) 3.5px 4.5px, transparent 5px),
    radial-gradient(circle at 30% 70%, rgb(255 255 255 / 0.35) 0 3px, rgb(0 0 0 / 0.25) 3.5px 4.5px, transparent 5px),
    radial-gradient(circle at 70% 70%, rgb(255 255 255 / 0.35) 0 3px, rgb(0 0 0 / 0.25) 3.5px 4.5px, transparent 5px); }

.lego-view p { margin: 2px 0 0; font-style: italic; line-height: 1.5; }

.block { margin-top: 20px; }

.block h3 {
    margin: 0 0 6px;
    font-size: 0.74rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--accent);
}

.block p { margin: 0; line-height: 1.55; }
.block .muted { color: var(--muted); font-size: 0.92rem; }

.block ul { margin: 0; padding: 0; list-style: none; }
.block li { position: relative; padding-left: 16px; margin-bottom: 6px; line-height: 1.5; }
.block li::before { content: "•"; position: absolute; left: 2px; color: var(--accent); }

.block ol { margin: 0; padding: 0; list-style: none; counter-reset: step; }
.block ol li { padding-left: 34px; margin-bottom: 10px; counter-increment: step; }
.block ol li::before {
    content: counter(step);
    left: 0;
    top: 0;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-size: 0.8rem;
    font-weight: 700;
    display: grid;
    place-items: center;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 11px;
    border: 1px solid color-mix(in srgb, var(--dot) 40%, transparent);
    border-radius: 99px;
    background: color-mix(in srgb, var(--dot) 13%, transparent);
    font-size: 0.88rem;
    font-weight: 500;
}

.chip .stud { width: 10px; height: 10px; }

.reading { display: flex; gap: 10px; margin-bottom: 12px; }
.reading .kind { flex: none; width: 22px; text-align: center; }
.reading a { color: var(--accent); font-weight: 600; text-decoration: none; line-height: 1.4; }
.reading a:hover { text-decoration: underline; }
.reading .meta { font-size: 0.8rem; color: var(--muted); margin-top: 2px; }

.my-notes {
    padding: 12px;
    border-radius: 12px;
    background: color-mix(in srgb, var(--purple) 8%, transparent);
    white-space: pre-wrap;
    line-height: 1.5;
}

.block.notes h3 { color: var(--purple); }

.hint {
    margin-top: 28px;
    font-size: 0.9rem;
    color: var(--muted);
    text-align: center;
}

.empty, .summary {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 24px;
    text-align: center;
    color: var(--muted);
}

.summary .big, .empty .big { font-size: 3.5rem; }
.summary h2, .empty h2 { margin: 0; color: var(--text); }
.summary .score { font-size: 1.3rem; color: var(--text); }
.summary .buttons { display: flex; flex-direction: column; gap: 8px; width: min(320px, 100%); margin-top: 8px; }

.actions {
    display: flex;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: 8px;
    padding: 12px 0 14px;
}

.btn {
    flex: 1;
    min-height: 48px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--card);
    font-weight: 600;
    white-space: nowrap;
}

.btn:active { filter: brightness(0.93); }
.btn.primary { border-color: var(--lego); background: var(--lego); color: #fff; }
.btn.good { border-color: var(--green); background: var(--green); color: #fff; }
.btn.bad { border-color: var(--orange); color: var(--orange); }
.btn.small { flex: 0 0 auto; min-height: 48px; }
/* Prev / Next: larger tap targets than the old chevron-only .small buttons */
.btn.nav {
    flex: 0 0 auto;
    min-width: 56px;
    min-height: 52px;
    padding: 10px 14px;
    font-size: 1.55rem;
    line-height: 1;
    font-weight: 500;
}
.btn.on-known { color: var(--green); border-color: var(--green); }
.btn.on-review { color: var(--orange); border-color: var(--orange); }
.btn:disabled { opacity: 0.45; }

@media (max-width: 560px) {
    .actions {
        gap: 10px;
        padding: 16px 0 20px;
    }
    .btn {
        min-height: 56px;
        padding: 12px 10px;
        font-size: 1.02rem;
        border-radius: 16px;
    }
    .btn.nav {
        min-width: 72px;
        min-height: 56px;
        padding: 12px 16px;
        font-size: 1.85rem;
    }
    .btn.small {
        min-height: 56px;
        min-width: 56px;
        padding: 12px 14px;
    }
}

.backdrop {
    position: fixed;
    inset: 0;
    background: rgb(0 0 0 / 0.35);
    z-index: 10;
}

.drawer {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 11;
    display: flex;
    flex-direction: column;
    width: min(380px, 90vw);
    padding-top: env(safe-area-inset-top);
    background: var(--bg);
    box-shadow: 4px 0 24px rgb(0 0 0 / 0.2);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
}

.drawer.open { transform: none; }

.drawer-head {
    display: flex;
    align-items: center;
    padding: 8px 12px;
}

.drawer-body {
    flex: 1;
    overflow-y: auto;
    padding: 0 12px 12px;
}

.drawer-foot {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    font-weight: 600;
    text-align: center;
}

.drawer h4, dialog h4 {
    margin: 18px 4px 6px;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--muted);
}

.panel {
    border-radius: 14px;
    background: var(--card);
    overflow: hidden;
}

.row {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 46px;
    padding: 8px 14px;
    border-bottom: 1px solid var(--line);
}

.row:last-child { border-bottom: none; }
.row input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--lego); flex: none; }
.row label, .row .grow { flex: 1; }
.row .count { color: var(--muted); font-size: 0.85rem; }
.row select { font: inherit; padding: 6px; border-radius: 8px; }

.source-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    border: none;
    width: 100%;
    background: none;
    font-weight: 700;
    text-align: left;
}

.source-head .chev { transition: transform 0.15s; }
.source-head.collapsed .chev { transform: rotate(-90deg); }
.source-head .count { margin-left: auto; font-weight: 500; color: var(--muted); font-size: 0.85rem; }

.links {
    display: flex;
    gap: 16px;
    padding: 0 14px 8px;
}

.links button {
    padding: 4px 0;
    border: none;
    background: none;
    color: var(--lego);
    font-size: 0.9rem;
}

.menu-btn {
    display: block;
    width: 100%;
    padding: 13px 14px;
    border: none;
    border-bottom: 1px solid var(--line);
    background: none;
    text-align: left;
    color: inherit;
    text-decoration: none;
}

.menu-btn:last-child { border-bottom: none; }
.menu-btn.danger { color: #c9372c; }
.menu-btn:disabled { opacity: 0.45; }

dialog {
    width: min(560px, 94vw);
    max-height: 90dvh;
    padding: 0;
    border: none;
    border-radius: 18px;
    background: var(--bg);
    color: var(--text);
}

dialog::backdrop { background: rgb(0 0 0 / 0.4); }

.dialog-body { padding: 18px; overflow-y: auto; }
.dialog-body h2 { margin: 0 0 4px; font-size: 1.2rem; }
.dialog-body > p { margin: 0 0 12px; color: var(--muted); font-size: 0.92rem; }
.dialog-body h4 { margin: 12px 0 6px; }

.dialog-body textarea {
    width: 100%;
    min-height: 220px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--card);
    color: var(--text);
    font: inherit;
    font-size: 1rem;
    line-height: 1.5;
}

.dialog-body .peek-lego { font-style: italic; color: var(--muted); margin: 6px 0 10px; line-height: 1.5; }
.dialog-body .peek-summary { line-height: 1.55; margin: 0; }

.dialog-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 18px 18px;
}

#print-area { display: none; }

@media print {
    @page { margin: 16mm 14mm; }

    body { background: #fff; color: #000; }
    #app, .drawer, .backdrop, dialog { display: none !important; }

    #print-area {
        display: block;
        font-family: var(--ui);
        font-size: 10.5pt;
    }

    #print-area h1 { margin: 0; font-size: 18pt; }
    #print-area .count { margin: 2pt 0 10pt; color: #555; }
    #print-area h2 { margin: 14pt 0 6pt; padding-bottom: 3pt; border-bottom: 1px solid #b0100f; color: #b0100f; font-size: 13pt; break-after: avoid; }
    #print-area .pc { margin: 0 0 10pt; padding-bottom: 8pt; border-bottom: 1px solid #ddd; break-inside: avoid; }
    #print-area .pc-title { font-size: 12pt; font-weight: 700; }
    #print-area .pc-sub { color: #555; margin: 1pt 0 3pt; }
    #print-area .pc-lego { font-style: italic; margin: 3pt 0; }
    #print-area p { margin: 3pt 0; line-height: 1.4; }
    #print-area b { color: #b0100f; font-size: 8.5pt; letter-spacing: 0.03em; }
    #print-area ul, #print-area ol { margin: 2pt 0 4pt; padding-left: 16pt; }
    #print-area li { margin: 1pt 0; line-height: 1.35; }
    #print-area .pc-links li { font-size: 9pt; color: #333; }
}
