:root {
    --stone-100: #f5f5f4;
    --stone-200: #e7e5e4;
    --stone-300: #d6d3d1;
    --white: #ffffff;
    --ink: #0f172a;
    --slate-700: #334155;
    --slate-500: #64748b;
    --slate-400: #94a3b8;
    --slate-300: #cbd5e1;
    --teal-700: #0f766e;
    --teal-800: #115e59;
    --teal-50: #f0fdfa;
    --amber: #b45309;
    --rose: #be123c;
    --radius: 14px;
    --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
    --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: var(--sans);
    background: var(--stone-100);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}
.wordmark { height: 26px; width: auto; display: block; }
.brandmark { height: 46px; width: auto; display: block; margin: 2px auto 12px; }
.muted { color: var(--slate-400); }
.small { font-size: 0.75rem; }
.link { text-decoration: none; }
.link:hover { color: var(--slate-700); }
.serif { font-family: var(--serif); }

/* ---- top bar + tabs ---- */
.topbar { position: sticky; top: 0; z-index: 10; background: rgba(245,245,244,.9);
    backdrop-filter: blur(8px); border-bottom: 1px solid var(--stone-300); }
.topbar-inner, .app { max-width: 640px; margin: 0 auto; }
.topbar-inner { padding: 14px 20px; display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 8px; }
/* Bottom padding clears the fixed footer. */
.app { padding: 24px 20px 76px; }

/* The navigation is the header's second row, not a bar at the bottom: the
   bottom belongs to the footer. It inherits the header's own background, so it
   must not paint one of its own. */
.tabbar { border-top: 1px solid var(--stone-200); display: flex; }
.tabbar-inner { max-width: 640px; margin: 0 auto; width: 100%; display: flex; }
.tab { flex: 1; border: 0; background: none; padding: 11px 0; font-size: .8rem;
    color: var(--slate-400); cursor: pointer; position: relative; }
.tab.active { color: var(--teal-700); font-weight: 600; }
/* The current tab is underlined as well as coloured — colour alone is not a
   distinction everybody can see. */
.tab.active::after { content: ""; position: absolute; left: 50%; transform: translateX(-50%);
    bottom: 0; width: 34px; height: 2px; border-radius: 2px; background: var(--teal-700); }
.badge { display: inline-block; min-width: 18px; height: 18px; line-height: 18px;
    margin-left: 6px; padding: 0 5px; border-radius: 999px; font-size: 11px;
    color: #fff; background: var(--teal-700); }
.badge:empty { display: none; }
.muted-badge { background: var(--slate-400); }

/* ---- generic controls ---- */
.h2 { font-family: var(--serif); font-size: 1.25rem; margin: 0 0 4px; }
.lead { color: var(--slate-500); font-size: .9rem; margin: 0 0 16px; }
.input, textarea.input {
    width: 100%; border: 1px solid var(--stone-300); background: var(--white);
    border-radius: var(--radius); padding: 14px 16px; font-size: 1rem; color: var(--ink);
    font-family: var(--serif); }
textarea.input { resize: vertical; line-height: 1.4; }
.input:focus { outline: 2px solid var(--teal-700); outline-offset: 0; border-color: transparent; }
.btn { border: 1px solid var(--stone-300); background: var(--white); color: var(--slate-700);
    border-radius: var(--radius); padding: 12px 16px; font-size: .95rem; cursor: pointer; }
.btn:hover { background: var(--stone-100); }
.btn-primary { background: var(--teal-700); color: #fff; border-color: var(--teal-700); }
.btn-primary:hover { background: var(--teal-800); }
.btn-dark { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn:disabled { opacity: .4; cursor: default; }
.btn-block { width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px; }
.row { display: flex; gap: 12px; }
.grow { flex: 1; }
.grow2 { flex: 2; }
.linkish { background: none; border: 0; color: var(--teal-700); cursor: pointer; font-size: .8rem; padding: 0; }
.linkish:hover { text-decoration: underline; }

/* ---- pills ---- */
.pill { display: inline-block; border-radius: 999px; padding: 2px 9px; font-size: 11px;
    font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.pill.who { background: #ccfbf1; color: #115e59; }
.pill.what { background: #e2e8f0; color: #334155; }
.pill.when { background: #fef3c7; color: #92400e; }
.pill.where { background: #ffe4e6; color: #9f1239; }
.pill.which { background: #e0e7ff; color: #3730a3; }
.pill.other { background: var(--stone-200); color: var(--slate-500); }

/* ---- cards ---- */
.card { border: 1px solid var(--stone-300); background: var(--white);
    border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.card .q { font-family: var(--serif); font-size: 1.1rem; line-height: 1.35; margin: 8px 0 2px; }
.card .a { color: var(--slate-500); font-size: .9rem; }
/* The question is edited where it is read, so it must not look like a form
   field until you reach for it: the box appears on hover, the teal ring on
   focus — the same ring .input uses. The negative margin keeps the text on the
   same left edge as the answer below it once padding is added. */
.card .q[contenteditable="true"] { border-radius: 6px; padding: 2px 5px; margin-left: -5px;
    margin-right: -5px; cursor: text; outline: none; }
.card .q[contenteditable="true"]:hover { background: var(--stone-100); }
.card .q[contenteditable="true"]:focus { background: var(--white); outline: 2px solid var(--teal-700); }
.card .q.blank { outline: 2px solid var(--rose); }
/* min-width lets a long question wrap instead of pushing the ✕ off the card. */
.cardtext { flex: 1; min-width: 0; }
.card.off { opacity: .4; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.iconbtn { background: none; border: 0; color: var(--slate-300); cursor: pointer; font-size: 1.1rem; line-height: 1; }
.iconbtn:hover { color: var(--slate-700); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.chip { font-size: 12px; background: var(--white); border: 1px solid var(--stone-300);
    border-radius: 8px; padding: 4px 8px; }
.chip b { color: var(--slate-700); font-weight: 600; }
.langbadge { display: inline-flex; align-items: center; gap: 4px; background: #ccfbf1;
    color: #115e59; border-radius: 999px; padding: 2px 10px; font-size: 12px; font-weight: 600; }

/* ---- mode toggle ---- */
.seg { display: flex; background: var(--stone-200); border-radius: 12px; padding: 4px; margin-bottom: 6px; }
.seg-opt { flex: 1; border: 0; background: none; padding: 8px 0; border-radius: 9px;
    font-size: .9rem; color: var(--slate-500); cursor: pointer; }
.seg-opt.on { background: #fff; color: var(--teal-700); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.seg-sub { margin: 0 0 18px; }

/* ---- quiz controls ---- */
.btn-ghost { background: #fff; }
.quiz-reveal, .quiz-check { display: flex; gap: 8px; }
.check-q { text-align: center; font-family: var(--serif); color: var(--slate-700); margin: 18px 0 8px; }

/* ---- study card (signature) ---- */
.study-meta { display: flex; justify-content: space-between; font-size: 12px;
    color: var(--slate-400); margin-bottom: 16px; }
.studycard { position: relative; border: 1px solid var(--stone-300); background: var(--white);
    border-radius: 18px; padding: 24px; min-height: 200px; overflow: hidden;
    box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.studycard::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0;
    width: 6px; background: var(--teal-700); }
.studycard .q { font-family: var(--serif); font-size: 1.5rem; line-height: 1.3; margin: 12px 0 0; }
.answer { margin-top: 20px; padding-top: 20px; border-top: 1px solid var(--stone-200); }
.answer .label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--slate-400); }
.answer .val { font-family: var(--serif); font-size: 1.25rem; color: var(--teal-800); margin-top: 4px; }
.grades { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-top: 16px; }
.grade { border: 1px solid var(--stone-300); background: #fff; border-radius: var(--radius);
    padding: 12px 0; display: flex; flex-direction: column; align-items: center; cursor: pointer; }
.grade b { font-size: .9rem; font-weight: 600; }
.grade span { font-size: 11px; color: var(--slate-400); }
.grade.again { border-color: #fecdd3; color: var(--rose); }
.grade.good { border-color: #99f6e4; color: var(--teal-800); }
.grade.easy { border-color: var(--slate-300); color: var(--slate-700); }

/* ---- library ---- */
.set { border: 1px solid var(--stone-300); background: #fff; border-radius: var(--radius);
    overflow: hidden; margin-bottom: 12px; }
.set-head { width: 100%; display: flex; align-items: center; justify-content: space-between;
    gap: 12px; padding: 14px 16px; background: none; border: 0; text-align: left; cursor: pointer; }
.set-title { font-family: var(--serif); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.set-sub { font-size: 12px; color: var(--slate-400); margin-top: 2px; }
.set-body { border-top: 1px solid var(--stone-200); padding: 12px 16px; }
.set-card { font-size: .9rem; margin-bottom: 8px; }
.set-card .q { font-family: var(--serif); color: var(--slate-700); }
.set-card .a { color: var(--slate-400); }
.danger { background: none; border: 0; color: var(--slate-400); cursor: pointer; font-size: .8rem; }
.danger:hover { color: var(--rose); }

.empty { text-align: center; padding: 64px 0; }
.empty .t { font-family: var(--serif); font-size: 1.25rem; color: var(--slate-700); }
.center { text-align: center; }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.errbox { background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c;
    border-radius: 10px; padding: 10px 12px; font-size: .9rem; margin-top: 12px; }
.langgrid { display: flex; flex-wrap: wrap; gap: 8px; }
.langbtn { border: 1px solid var(--stone-300); background: #fff; border-radius: 999px;
    padding: 8px 16px; font-size: .9rem; cursor: pointer; }
.langbtn:hover { border-color: var(--teal-700); color: var(--teal-700); }

/* ---- login ---- */
.login-body { display: flex; min-height: 100vh; align-items: center; justify-content: center; }
.login-card { text-align: center; width: 100%; max-width: 320px; padding: 24px; }
.login-form { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; }
.login-error { color: var(--rose); font-size: .85rem; }

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

/* ============ v2: auth, banks, leaderboard ============ */
.auth { max-width: 360px; margin: 0 auto; padding: 48px 24px; }
.auth h2 { font-family: var(--serif); font-size: 1.5rem; margin-bottom: 4px; }
.auth .lead { margin-bottom: 20px; }
.auth .field { display: block; width: 100%; margin-bottom: 10px; }
.auth .btn-primary { width: 100%; margin-top: 4px; }
.auth .switch { text-align: center; margin-top: 16px; font-size: .85rem; color: var(--slate-500); }
.auth .switch button { margin-left: 4px; }
.notice { background: var(--teal-50); border: 1px solid #99f6e4; color: var(--teal-800);
    border-radius: 12px; padding: 14px; font-size: .9rem; margin-top: 12px; }

/* topbar with bank switcher */
.bar { max-width: 640px; margin: 0 auto; }
/* The chevron is ours, not the browser's. The native one is drawn inside the
   right padding, so at 10px it sat almost on the border, and a bank name long
   enough to fill the box ran straight into it — Safari, Chrome and Firefox
   each placing it differently on top of that. As a background image it is a
   fixed 10px from the edge everywhere, and the 30px right padding reserves the
   space so the text can never reach it. */
.bank-select { font-family: var(--sans); font-size: .95rem; color: var(--ink);
    border: 1px solid var(--stone-300); border-radius: 10px;
    padding: 7px 30px 7px 10px; max-width: 190px;
    appearance: none; -webkit-appearance: none;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12' fill='none' stroke='%2364748b' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M2.5 4.5 6 8l3.5-3.5'/%3E%3C/svg%3E")
        no-repeat right 10px center;
    text-overflow: ellipsis; }
.role { display: inline-block; font-size: 11px; text-transform: uppercase; letter-spacing: .03em;
    border-radius: 999px; padding: 2px 8px; font-weight: 600; }
.role.owner { background: #fef3c7; color: #92400e; }
.role.contributor { background: #ccfbf1; color: #115e59; }
.role.viewer { background: var(--stone-200); color: var(--slate-500); }

.bankbar { max-width: 640px; margin: 0 auto; padding: 10px 20px 0; display: flex;
    align-items: center; justify-content: space-between; gap: 10px; }
.bankbar .left { display: flex; align-items: center; gap: 8px; min-width: 0; }
.manage { max-width: 640px; margin: 0 auto; padding: 8px 20px 0; }
.panel { background: #fff; border: 1px solid var(--stone-300); border-radius: 12px; padding: 14px; }
.member { display: flex; align-items: center; justify-content: space-between; padding: 6px 0;
    border-bottom: 1px solid var(--stone-200); font-size: .9rem; }
.member:last-child { border-bottom: 0; }

.invites { max-width: 640px; margin: 0 auto; padding: 10px 20px 0; }
.invite { background: #fffbeb; border: 1px solid #fde68a; border-radius: 12px; padding: 10px 12px;
    font-size: .9rem; display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }

/* leaderboard */
.lb-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.lb-row { display: flex; align-items: center; gap: 12px; background: #fff; border: 1px solid var(--stone-300);
    border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; }
.lb-row.me { border-color: var(--teal-700); background: var(--teal-50); }
.lb-rank { font-family: var(--serif); font-size: 1.1rem; color: var(--slate-400); width: 26px; text-align: center; }
.lb-name { flex: 1; font-weight: 600; color: var(--ink); }
.lb-name .you { font-weight: 400; color: var(--teal-700); font-size: .8rem; margin-left: 6px; }
.lb-pts { font-family: var(--serif); font-size: 1.2rem; color: var(--teal-800); }
.lb-sub { font-size: 11px; color: var(--slate-400); }
.lb-stats { text-align: right; }

/* ---------- account menu ---------- */
.topright { display: flex; align-items: center; gap: 10px; }
/* The anchor for the menu. position: relative so the panel below can hang off
   it, and the header must not clip it. */
.menuwrap { position: relative; }
.avatarbtn { border: 0; background: none; padding: 0; cursor: pointer; border-radius: 999px;
    line-height: 0; }
.avatarbtn:focus-visible { outline: 2px solid var(--teal-700); outline-offset: 2px; }
.avatar { width: 32px; height: 32px; border-radius: 999px; display: inline-flex;
    align-items: center; justify-content: center; object-fit: cover;
    background: var(--stone-200); color: var(--slate-700); font-size: .78rem;
    font-weight: 600; letter-spacing: .02em; border: 1px solid var(--stone-300); }
.avatar.emoji { font-size: 1.1rem; background: var(--teal-50); }
.avatar.big { width: 44px; height: 44px; font-size: .95rem; }
.avatar.big.emoji { font-size: 1.5rem; }

.menu { position: absolute; top: calc(100% + 8px); right: 0; z-index: 40; width: 260px;
    background: var(--white); border: 1px solid var(--stone-300); border-radius: 12px;
    box-shadow: 0 12px 28px rgba(15, 23, 42, .13); padding: 8px; }
.menu-who { display: flex; align-items: center; gap: 10px; padding: 6px 8px 10px; }
.menu-id { display: flex; flex-direction: column; min-width: 0; }
.menu-id b { font-size: .92rem; }
.menu-id span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-sep { height: 1px; background: var(--stone-200); margin: 6px 0; }
.menu-item { display: block; width: 100%; text-align: left; border: 0; background: none;
    padding: 9px 10px; border-radius: 8px; font-size: .9rem; font-family: var(--sans);
    color: var(--ink); cursor: pointer; text-decoration: none; }
.menu-item:hover { background: var(--stone-100); }
/* Menu rows are icon + label on one line, the icon a fixed 16px so every
   label starts at the same x. */
.menu-item { display: flex; align-items: center; gap: 10px; }
.ic { width: 16px; height: 16px; flex: none; color: var(--slate-400); }
.menu-item:hover .ic { color: var(--slate-700); }
.menu-item.accent .ic { color: var(--teal-700); }

/* The picture controls: a row of square buttons under the identity, acting on
   the avatar directly above them. */
.picrow { display: flex; gap: 6px; padding: 0 10px 4px; }
.picbtn { width: 34px; height: 34px; flex: none; padding: 0; border: 0; border-radius: 9px;
    display: grid; place-items: center; cursor: pointer;
    background: var(--teal-50); color: var(--teal-700); }
.picbtn .ic { color: inherit; width: 17px; height: 17px; }
.picbtn:hover { background: var(--teal-700); color: #fff; }
.picbtn.danger { background: var(--stone-100); color: var(--slate-500); }
.picbtn.danger:hover { background: var(--rose); color: #fff; }
.menu-item.quiet { color: var(--slate-500); font-size: .85rem; }
.menu-item.accent { color: var(--teal-700); font-weight: 600; }
.menu-lbl { padding: 8px 10px 4px; font-size: 11px; text-transform: uppercase;
    letter-spacing: .04em; color: var(--slate-400); }
.iconpick { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; padding: 0 6px 4px; }
.iconbtn-pick { border: 1px solid transparent; background: none; cursor: pointer;
    font-size: 1.05rem; line-height: 1; padding: 4px 0; border-radius: 8px; }
.iconbtn-pick:hover { background: var(--stone-100); }
.iconbtn-pick.current { border-color: var(--teal-700); background: var(--teal-50); }
.menu-msg { margin: 4px 10px; font-size: .82rem; color: var(--teal-800); }
.menu-msg.bad { color: var(--rose); }
.menu-foot { padding: 2px 10px 6px; }

/* ---------- footer ---------- */
.app-footer { position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
    display: flex; align-items: center; justify-content: center; gap: .4rem; flex-wrap: wrap;
    padding: 9px 16px calc(env(safe-area-inset-bottom, 0px) + 9px);
    background: rgba(245, 245, 244, .94); border-top: 1px solid var(--stone-200);
    font-size: .78rem; color: var(--slate-500); }
@supports (backdrop-filter: blur(6px)) { .app-footer { backdrop-filter: blur(6px); } }
.foot-name { font-weight: 700; color: var(--slate-700); }
.foot-version { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-weight: 600;
    color: var(--slate-400); }
.foot-sep { color: var(--teal-700); opacity: .7; }
.app-footer a { color: var(--teal-700); font-weight: 600; text-decoration: none; }
.app-footer a:hover, .app-footer a:focus-visible { text-decoration: underline; }

/* ---------- toast ---------- */
/* Above the footer, not over it. */
.toast { position: fixed; left: 50%; bottom: 72px; transform: translateX(-50%) translateY(6px);
    z-index: 60; max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px;
    background: var(--ink); color: var(--stone-100); font-size: .85rem; text-align: center;
    box-shadow: 0 10px 30px rgba(15, 23, 42, .25);
    opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease; }
.toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- quiz: typed answer ---------- */
.quiz-answer { display: flex; gap: 8px; margin-top: 16px; }
.quiz-answer .input { flex: 1; min-width: 0; }
