@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');

/* ---- Variables (matching the Medtech OS design language) ---- */
:root {
    --color-primary: #2d3f86;
    --color-primary-dark: #232f6a;
    --color-accent: #e85036;
    --color-accent-dark: #d0442c;
    --color-text: #5e5e5e;
    --color-text-strong: #1a1a1a;
    --color-text-muted: #6b7280;
    --color-bg: #f6f6f6;
    --color-surface: #fff;
    --color-border: #d1d5db;
    --color-border-light: #e5e7eb;
    --color-error: #991b1b;
    --color-error-bg: #fef2f2;
    --color-error-border: #fecaca;
    --color-success: #166534;
    --color-success-bg: #f0fdf4;
    --color-success-border: #bbf7d0;
    --radius: 4px;
    --radius-lg: 8px;
}

/* ---- Reset ---- */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* ---- Base ---- */
body {
    font-family: "Poppins", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    line-height: 1.6;
    color: var(--color-text);
    background: var(--color-surface);
    padding-bottom: 2rem;
}

a {
    color: var(--color-primary);
}

h1, h2, h3 {
    color: var(--color-primary);
}

h1 {
    font-size: 1.75rem;
    margin-bottom: 0.5rem;
}

h2 {
    font-size: 1.25rem;
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
}

p {
    margin-bottom: 0.75rem;
}

.text-center {
    text-align: center;
}

.mini {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    line-height: 1.45;
}

/* ---- Nav ---- */
.topnav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    padding: 0.75rem 1.25rem;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border-light);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
    margin-bottom: 2rem;
}

.topnav .brand {
    font-weight: 600;
    color: var(--color-primary);
    text-decoration: none;
}

.topnav .nav-links {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.topnav .nav-links a {
    color: var(--color-text-muted);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.9rem;
}

.topnav .nav-links a:hover {
    color: var(--color-primary);
}

.sync-status {
    font-size: 0.8rem;
    font-weight: 600;
    color: #92400e;
    background: #fffbeb;
    border: 1px solid #fde68a;
    border-radius: 999px;
    padding: 0.2rem 0.7rem;
    white-space: nowrap;
}

.btn-link {
    background: none;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
    font-family: inherit;
    padding: 0;
}

.btn-link:hover {
    color: var(--color-primary);
}

/* ---- Layout ---- */
.container {
    max-width: 700px;
    margin: 2rem auto 0;
    padding: 0 1.5rem;
}

.topnav ~ .container, .topnav ~ .app {
    margin-top: 0;
}

.container.narrow {
    max-width: 460px;
}

.container.wide {
    max-width: none;
    padding: 0 2rem;
}

/* ---- Forms ---- */
label {
    display: block;
    font-weight: 600;
    margin-bottom: 0.25rem;
    font-size: 0.9rem;
    color: var(--color-text-strong);
}

input[type="text"],
input[type="email"] {
    display: block;
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-size: 0.95rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    margin-bottom: 1rem;
    font-family: inherit;
    height: 2.6rem;
}

input[type="text"]:focus,
input[type="email"]:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 2px rgba(45, 63, 134, 0.2);
}

button {
    font-family: inherit;
    cursor: pointer;
}

.btn-primary {
    background: var(--color-accent);
    color: #fff;
    border: none;
    padding: 0.6rem 1.5rem;
    font-size: 0.95rem;
    font-weight: 600;
    border-radius: var(--radius);
    display: inline-block;
}

.btn-primary:hover {
    background: var(--color-accent-dark);
}

.btn-primary:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

/* ---- Alerts ---- */
.alert {
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: 1rem;
}

.alert-error {
    background: var(--color-error-bg);
    border: 1px solid var(--color-error-border);
    color: var(--color-error);
}

.alert-success {
    background: var(--color-success-bg);
    border: 1px solid var(--color-success-border);
    color: var(--color-success);
}

.empty {
    padding: 2rem;
    border: 1px dashed var(--color-border);
    border-radius: var(--radius-lg);
    color: var(--color-text-muted);
    background: var(--color-bg);
}

/* ---- Dashboard ---- */
.guidance-options {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    margin: 1.25rem 0;
}

.guidance-option {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin: 0;
    padding: 0.85rem 1rem;
    border: 1px solid var(--color-border-light);
    border-radius: var(--radius);
    background: var(--color-surface);
    cursor: pointer;
    font-weight: 400;
}

.guidance-option:hover {
    border-color: var(--color-primary);
}

.guidance-option input {
    width: 1.1rem;
    height: 1.1rem;
    accent-color: var(--color-primary);
    flex-shrink: 0;
}

.guidance-name {
    font-weight: 600;
    color: var(--color-text-strong);
    flex: 1;
}

.guidance-count {
    color: var(--color-text-muted);
    font-size: 0.85rem;
    white-space: nowrap;
}

.hard-filter {
    margin: -0.4rem 0 1.25rem;
}

.resume-session {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
    margin-bottom: 1rem;
    border: 1px solid var(--color-primary);
    border-radius: var(--radius);
    background: rgba(45, 63, 134, 0.05);
    text-decoration: none;
}

.resume-session .guidance-name {
    color: var(--color-primary);
}

.resume-session:hover {
    background: rgba(45, 63, 134, 0.1);
}

/* The picker summary only acts as a reveal button on phones; on larger
   screens the picker is always open and the summary is hidden. */
.study-picker summary {
    display: none;
    list-style: none;
    text-align: center;
    cursor: pointer;
}

.study-picker summary::-webkit-details-marker {
    display: none;
}

@media (max-width: 760px) {
    .study-picker:not([open]) summary {
        display: block;
    }
}

/* ---- Account ---- */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.6rem;
    margin: 1.25rem 0 1.75rem;
}

.stat {
    background: var(--color-bg);
    border: 1px solid var(--color-border-light);
    border-radius: var(--radius);
    padding: 0.8rem;
}

.stat .num {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--color-primary);
    line-height: 1.2;
}

.stat .lbl {
    font-size: 0.75rem;
    color: var(--color-text-muted);
    margin-top: 2px;
}

.danger-zone {
    margin-top: 2.5rem;
}

.btn-danger {
    background: #c0392b;
    color: #fff;
    border: none;
    padding: 0.6rem 1.5rem;
    font-size: 0.95rem;
    font-weight: 600;
    border-radius: var(--radius);
}

.btn-danger:hover {
    background: #96281b;
}

.btn-danger:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ---- Study ---- */
.app {
    max-width: 980px;
    margin: 0 auto;
    padding: 0 1rem 1rem;
}

.progress-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin: 0.5rem 0 1rem;
}

.progress-segments {
    flex: 1;
    display: flex;
    height: 10px;
    border-radius: 999px;
    overflow: hidden;
}

.seg {
    flex: 1;
    background: var(--color-border-light);
}

.seg.good { background: #27ae60; }
.seg.bad { background: var(--color-error); }
.seg.current { background: var(--color-primary); }

.progress-count {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    white-space: nowrap;
}

.progress-count strong {
    color: var(--color-text-strong);
    font-weight: 600;
}

.stage {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 250px;
    gap: 1rem;
    align-items: stretch;
}

/* Tall default so flipping between short fronts and backs doesn't reflow the
   page; only unusually long content grows past this. */
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border-light);
    border-radius: var(--radius-lg);
    min-height: 62vh;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.tag, .notion-link {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.25rem 0.6rem;
    font-size: 0.75rem;
    font-weight: 600;
    line-height: 1.5;
    border-radius: 999px;
    color: var(--color-text-muted);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
}

.tag:hover, .notion-link:hover {
    color: var(--color-primary);
    border-color: var(--color-primary);
}

.tag.hard.active {
    background: #fef2ee;
    color: var(--color-accent-dark);
    border-color: var(--color-accent);
}

/* Hard + Edit float right-aligned at the top of the card, no divider. */
.cardTop {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 0.6rem;
    padding: 0.85rem 1.1rem 0;
}

.tag kbd {
    font-family: inherit;
    font-size: 0.65rem;
    font-weight: 400;
    padding: 0 0.25rem;
    border-radius: 3px;
    background: rgba(0, 0, 0, 0.08);
}

.content {
    padding: 1.5rem;
    flex: 1;
    display: flex;
    justify-content: center;
}

/* Auto margins center short content vertically while keeping the top of
   overflowing content reachable when the area scrolls. */
.content .prompt {
    margin-top: auto;
    margin-bottom: auto;
}

@keyframes cardflip {
    from { transform: rotateY(80deg); opacity: 0; }
    to { transform: none; opacity: 1; }
}

.content.flip .prompt {
    animation: cardflip 90ms ease-out;
}

.prompt {
    width: 100%;
    max-width: 760px;
    font-size: clamp(1.2rem, 2.6vw, 1.7rem);
    line-height: 1.35;
    color: var(--color-text-strong);
    text-align: left;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.prompt.back {
    font-size: clamp(0.95rem, 2vw, 1.2rem);
    line-height: 1.5;
    font-weight: 300;
}

.prompt code {
    font-family: Menlo, Consolas, monospace;
    font-size: .9em;
    background: var(--color-bg);
    border-radius: var(--radius);
    padding: 1px 4px;
}

.actions {
    padding: 0.75rem 1.1rem;
    border-top: 1px solid var(--color-border-light);
    display: flex;
    gap: 0.6rem;
    flex-wrap: wrap;
    align-items: center;
    min-height: 4.1rem;
    background: var(--color-surface);
}

.actions .skip {
    margin-left: auto;
}


.actions kbd {
    font-family: inherit;
    font-size: 0.7rem;
    font-weight: 400;
    padding: 0.05rem 0.3rem;
    border-radius: 3px;
    background: rgba(0, 0, 0, 0.08);
    margin-left: 0.2rem;
}

.actions .primary kbd {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
}

.actions button {
    min-height: 2.6rem;
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text-strong);
    border-radius: var(--radius);
    padding: 0 0.9rem;
    font-size: 0.9rem;
    font-weight: 600;
}

.actions button:hover {
    background: var(--color-bg);
}

.actions button:focus-visible,
.tag:focus-visible,
.toolbar button:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

.actions .primary {
    background: var(--color-primary);
    color: #fff;
    border-color: var(--color-primary);
    padding: 0 1.1rem;
}

.actions .primary:hover {
    background: var(--color-primary-dark);
}

.actions .good {
    background: var(--color-success-bg);
    color: var(--color-success);
    border-color: var(--color-success-border);
}

.actions .bad {
    background: var(--color-error-bg);
    color: var(--color-error);
    border-color: var(--color-error-border);
}

.sidebar {
    background: var(--color-surface);
    border: 1px solid var(--color-border-light);
    border-radius: var(--radius-lg);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
}

.list {
    position: relative;
    max-height: 60vh;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    padding-right: 2px;
}

.dotrow {
    display: grid;
    grid-template-columns: 18px 1fr;
    gap: 0.5rem;
    align-items: start;
    font-size: 0.75rem;
    color: var(--color-text-muted);
    line-height: 1.4;
    cursor: pointer;
}

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

.dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-top: 4px;
    background: var(--color-border-light);
    border: 1px solid var(--color-border);
}

.dot.ok { background: #27ae60; border-color: #27ae60; }
.dot.miss { background: var(--color-error); border-color: var(--color-error); }
.dot.current { background: var(--color-primary); border-color: var(--color-primary); }

.toolbar {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center;
}

.toolbar button {
    min-height: 2.3rem;
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text-strong);
    border-radius: var(--radius);
    padding: 0 0.8rem;
    font-size: 0.85rem;
    font-weight: 600;
}

.toolbar button:hover {
    background: var(--color-bg);
}

/* ---- Dashboard grid ---- */
.dashboard-grid {
    display: grid;
    grid-template-columns: minmax(0, 5fr) minmax(0, 6fr);
    gap: 2.5rem;
    align-items: start;
}

.dashboard-grid h2 {
    margin-top: 0;
}

@media (max-width: 900px) {
    .dashboard-grid {
        grid-template-columns: 1fr;
    }
    /* On phones the study controls come first; the leaderboard follows. */
    .dashboard-leaderboard {
        order: 2;
    }
}

/* ---- Leaderboard ---- */
.leaderboard-header {
    display: flex;
    align-items: baseline;
    gap: 1.25rem;
}

.view-tabs {
    display: flex;
    gap: 0.75rem;
    font-size: 0.85rem;
}

.view-tabs a {
    color: var(--color-text-muted);
    text-decoration: none;
}

.view-tabs a:hover {
    color: var(--color-primary);
}

.view-tabs .active {
    color: var(--color-primary);
    font-weight: 600;
    border-bottom: 2px solid var(--color-primary);
}

/* Shrinks to the table's width so the legend centers under the blocks, not
   the whole column. */
.leaderboard-wrap {
    width: fit-content;
    max-width: 100%;
    overflow-x: auto;
}

.leaderboard {
    border-collapse: collapse;
}

.leaderboard td {
    padding: 0.4rem 0.75rem 0.4rem 0;
    vertical-align: middle;
    white-space: nowrap;
}

.leaderboard-user {
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--color-text-strong);
}

.leaderboard-total {
    color: var(--color-text-muted);
    font-size: 0.85rem;
    text-align: right;
}

.blocks {
    display: flex;
    gap: 3px;
}

.block {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    background: #ebedf0;
    display: inline-block;
    flex: none;
}

.block.level-1 { background: #9be9a8; }
.block.level-2 { background: #40c463; }
.block.level-3 { background: #30a14e; }
.block.level-4 { background: #216e39; }

.legend {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 3px;
    margin-top: 0.75rem;
    font-size: 0.75rem;
}

.legend .block {
    width: 10px;
    height: 10px;
    border-radius: 2px;
}

/* ---- Sync preview / diff ---- */
.sync-actions {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.diff ul {
    list-style: none;
    padding-left: 0;
}

.diff > div > ul > li, .diff ul li {
    margin-bottom: 0.35rem;
    font-size: 0.9rem;
}

.diff ul ul {
    margin: 0.25rem 0 0.5rem 1rem;
}

.diff-field {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-muted);
    margin-right: 0.3rem;
}

.diff-old {
    color: var(--color-error);
    text-decoration: line-through;
}

.diff-new {
    color: var(--color-success);
}

/* ---- Sync progress ---- */
.sync-progress {
    padding: 1.5rem 0;
}

.spinner {
    width: 34px;
    height: 34px;
    margin: 0 auto 1rem;
    border: 4px solid var(--color-border-light);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.sync-history {
    margin-top: 2rem;
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.history-table td {
    padding: 0.35rem 0.75rem 0.35rem 0;
    color: var(--color-text-muted);
    white-space: nowrap;
    vertical-align: top;
}

.history-table td.history-detail {
    color: var(--color-text-strong);
    white-space: normal;
}

/* ---- Footer ---- */
.page-footer {
    max-width: 700px;
    margin: 1.5rem auto 0;
    padding: 0 2rem;
    font-size: 0.8rem;
    color: var(--color-text-muted);
}

.page-footer a {
    color: var(--color-text-muted);
    text-decoration: none;
}

.page-footer a:hover {
    color: var(--color-primary);
    text-decoration: underline;
}

/* ---- Responsive ---- */
@media (max-width: 760px) {
    .container, .container.wide {
        padding: 0 1rem;
        margin-top: 1rem;
    }
    .study-header {
        flex-direction: column;
    }
    .stats {
        justify-content: flex-start;
    }
    .stage {
        grid-template-columns: 1fr;
    }
    /* Phone layout: Shuffle/Reset above the card, answer buttons below the
       card content where thumbs are. */
    .sidebar {
        order: -1;
        padding: 0;
        border: none;
        box-shadow: none;
    }
    .card {
        min-height: 60vh;
    }
    .content {
        padding: 1.25rem;
    }
    /* No keyboard on phones, and the full card list eats too much screen.
       Navigation happens by swiping, so the prev/skip buttons hide too. */
    .actions kbd, .tag kbd, .nav-prev, .actions .skip {
        display: none;
    }
    .list {
        display: none;
    }
    /* App-like study layout: the card fills the screen, its content scrolls
       internally, the answer buttons stay pinned at the bottom, and the
       footer barely fits underneath. */
    body.study-page {
        display: flex;
        flex-direction: column;
        height: 100dvh;
        overflow: hidden;
        padding-bottom: 0;
    }
    body.study-page .topnav {
        flex: none;
        margin-bottom: 0.75rem;
    }
    body.study-page .app {
        flex: 1;
        display: flex;
        flex-direction: column;
        min-height: 0;
        width: 100%;
        padding-bottom: 0.25rem;
    }
    body.study-page .progress-row {
        flex: none;
    }
    body.study-page #root {
        flex: 1;
        min-height: 0;
    }
    body.study-page .stage {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        height: 100%;
        min-height: 0;
    }
    body.study-page .sidebar {
        flex: none;
    }
    body.study-page .card {
        flex: 1;
        min-height: 0;
    }
    body.study-page .content {
        overflow-y: auto;
        min-height: 0;
    }
    body.study-page .actions {
        flex: none;
    }
    body.study-page .page-footer {
        flex: none;
        margin: 0.2rem auto;
        font-size: 0.65rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation: none !important; }
}
