:root {
    --bg: #f4f5f7;
    --card: #ffffff;
    --ink: #1c2230;
    --muted: #6b7280;
    --line: #e3e6ec;
    --accent: #2f6df6;
    --accent-ink: #ffffff;
    --danger: #c2362f;
    --radius: 12px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;   /* don't auto-enlarge text on iOS */
    min-height: 100vh;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    padding: 0.75rem 1rem;
    /* extend the bar under the iPhone status bar / notch when installed */
    padding-top: calc(0.75rem + env(safe-area-inset-top));
    padding-left: calc(1rem + env(safe-area-inset-left));
    padding-right: calc(1rem + env(safe-area-inset-right));
    background: var(--card);
    border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; text-decoration: none; color: var(--ink); font-size: 1.1rem; }
.topbar nav { display: flex; align-items: center; flex-wrap: wrap; gap: 0.4rem 1rem; }
.topbar nav a { text-decoration: none; color: var(--muted); padding: 0.2rem 0; }
.topbar nav a:hover { color: var(--ink); }

main {
    max-width: 720px;
    margin: 1.25rem auto;
    padding: 0 1rem;
    padding-left: calc(1rem + env(safe-area-inset-left));
    padding-right: calc(1rem + env(safe-area-inset-right));
    padding-bottom: calc(72px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

/* Bottom icon nav (matches the other apps). */
.bottom-nav {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: flex;
    background: var(--card); border-top: 1px solid var(--line);
    padding: 4px 4px env(safe-area-inset-bottom, 0px);
}
.bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 500;
    padding: 8px 4px; min-height: 54px; border-radius: var(--radius);
}
.bottom-nav a.active { color: var(--accent); }
.bottom-nav svg { width: 22px; height: 22px; }

.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 1.25rem;
}
.card.narrow { max-width: 420px; margin: 0 auto; width: 100%; }
h1 { margin-top: 0; font-size: 1.4rem; }
h2 { margin-top: 0; font-size: 1.15rem; }

label { display: block; margin-bottom: 0.75rem; font-size: 0.9rem; color: var(--muted); }
input[type=text], input[type=email], input[type=password], select {
    display: block;
    width: 100%;
    margin-top: 0.25rem;
    padding: 0.55rem 0.65rem;
    font-size: 1rem;
    color: var(--ink);
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 8px;
}
input:focus, select:focus { outline: 2px solid var(--accent); border-color: var(--accent); }

.row { display: flex; gap: 0.75rem; align-items: flex-end; }
.row .grow { flex: 1; }
.row label { margin-bottom: 0; }

button {
    font-size: 1rem;
    min-height: 44px;            /* comfortable touch target */
    padding: 0.6rem 1.1rem;
    border: 0;
    border-radius: 8px;
    background: var(--accent);
    color: var(--accent-ink);
    cursor: pointer;
    touch-action: manipulation;  /* no 300ms tap delay / double-tap zoom */
}
button:hover { filter: brightness(1.05); }
button:active { filter: brightness(0.95); }
button:disabled { opacity: 0.6; cursor: default; }
button.secondary { background: #e9edf6; color: var(--ink); white-space: nowrap; }

.linklike {
    min-height: 0;
    background: none;
    color: var(--accent);
    padding: 0.2rem 0;
    font-size: inherit;
}
.linklike.danger { color: var(--danger); }
.inline { display: inline; margin: 0; }

.checkbox {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0;
    color: var(--ink);
}
.checkbox input { width: auto; margin: 0; }

.muted { color: var(--muted); font-size: 0.85rem; }
.error { color: var(--danger); }

/* Add-form status line + the "confirm the detected language" prompt. */
.status { font-size: 0.85rem; margin: 0.5rem 0 0; }
.status.muted { color: var(--muted); }
.status.ok { color: #1d7a4d; }
.status.error { color: var(--danger); }
.status.confirm {
    color: #7a4a00;
    background: #fff5e6;
    border: 1px solid #ffd591;
    padding: 0.5rem 0.7rem;
    border-radius: 8px;
}

/* Language group headings + per-group count badge. */
.lang-group h2 { display: flex; align-items: center; gap: 0.5rem; }
.count {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted);
    background: #f0f2f6;
    border-radius: 999px;
    padding: 0.05rem 0.5rem;
}
.flag { font-size: 1.2rem; line-height: 1; }

/* Collapsible "Add a word" panel (collapsed by default). */
.add-card > summary {
    list-style: none;
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.15rem;
    font-weight: 700;
}
.add-card > summary::-webkit-details-marker { display: none; }
.add-card > summary .chev {
    color: var(--muted);
    transition: transform 0.15s ease;
}
.add-card[open] > summary { margin-bottom: 0.9rem; }
.add-card[open] > summary .chev { transform: rotate(90deg); }
.flash {
    background: #eaf2ff;
    border: 1px solid #c6dbff;
    color: #1b3c79;
    padding: 0.6rem 0.85rem;
    border-radius: 8px;
}

/* Word list as a two-column table: foreign word left, translation right. */
table.vocab { width: 100%; border-collapse: collapse; }
table.vocab td {
    padding: 0.55rem 0.4rem;
    border-top: 1px solid var(--line);
    vertical-align: top;
}
table.vocab tr:first-child td { border-top: 0; }
.vocab .src { font-weight: 600; width: 45%; }
.vocab .src small {
    display: block;
    font-weight: 400;
    font-size: 0.72rem;
    color: var(--muted);
}
.vocab .src small.when { opacity: 0.8; }
.vocab .dst { color: var(--ink); }
.vocab .act { width: 1.5rem; text-align: right; }
.vocab .act button { font-size: 0.95rem; color: var(--muted); padding: 0 0.15rem; }
.vocab .act button:hover { color: var(--danger); }

/* Inline "hear pronunciation" button next to each foreign word. */
.speak {
    min-height: 0;
    background: none;
    border: 0;
    padding: 0 0.15rem;
    margin-left: 0.3rem;
    font-size: 0.9rem;
    line-height: 1;
    vertical-align: baseline;
    opacity: 0.55;
}
.speak:hover, .speak:active { opacity: 1; }

/* Filter chips + search */
.controls-card { padding: 0.7rem; }
.lang-filter {
    display: flex;
    gap: 0.4rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 0.6rem;
    padding-bottom: 0.2rem;
}
.chip {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    min-height: 36px;
    padding: 0.3rem 0.75rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--ink);
    background: #f0f2f6;
    border: 0;
    border-radius: 999px;
    white-space: nowrap;
}
.chip.active { background: var(--accent); color: var(--accent-ink); }
.chip .flag { font-size: 1rem; }
#word-search {
    width: 100%;
    margin: 0;
    padding: 0.6rem 0.7rem;
    font-size: 1rem;
    border: 1px solid var(--line);
    border-radius: 8px;
}
.sr-only {
    position: absolute;
    width: 1px; height: 1px;
    margin: -1px; padding: 0; border: 0;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

code { background: #f0f2f6; padding: 0.05rem 0.35rem; border-radius: 5px; }

/* Users admin table — scrolls horizontally instead of overflowing on iPhone. */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 0.75rem; }
table.users { width: 100%; border-collapse: collapse; font-size: 0.9rem; min-width: 460px; }
table.users th, table.users td {
    text-align: left;
    padding: 0.5rem 0.6rem;
    border-bottom: 1px solid var(--line);
    vertical-align: top;
    white-space: nowrap;
}
.actions { display: flex; flex-direction: column; gap: 0.35rem; align-items: flex-start; }

@media (max-width: 560px) {
    main { margin-top: 0.75rem; }
    .card { padding: 1rem; }
    .row { flex-direction: column; align-items: stretch; gap: 0.25rem; }
    .row label { margin-bottom: 0.75rem; }
    /* Full-width primary actions are easier to thumb-tap (but not inline text links). */
    button[type="submit"]:not(.linklike), button.secondary { width: 100%; }
    .topbar nav { gap: 0.3rem 0.85rem; font-size: 0.95rem; }
}

/* Word list: source + translation link to the detail page (word.php). */
.vocab .word-text { color: var(--ink); text-decoration: none; }
.vocab .word-text:hover { color: var(--accent); text-decoration: underline; }
.vocab .act { width: 1%; text-align: right; }
.row-go { display: inline-flex; color: var(--muted); padding: .25rem; border-radius: 8px; }
.row-go:hover { color: var(--accent); }
.row-go svg { width: 20px; height: 20px; display: block; }

/* Per-word detail page (word.php) */
.word-detail .back { display: inline-block; margin: 0 0 .5rem; color: var(--accent); text-decoration: none; font-size: .9rem; }
.word-detail .back:hover { text-decoration: underline; }
.word-head { border-bottom: 1px solid var(--line); padding-bottom: 1rem; margin-bottom: .5rem; }
.word-term { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; margin: 0; font-size: 1.6rem; }
.word-term .flag { font-size: 1.3rem; }
.word-detail .translation { margin: .35rem 0 0; font-size: 1.15rem; color: var(--accent); font-weight: 600; }
.word-detail .meta { margin: .35rem 0 0; color: var(--muted); font-size: .85rem; }
.word-detail .note { margin: .5rem 0 0; color: var(--ink); }

.enrich { margin-top: 1.25rem; }
.enrich h2 { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 .4rem; }
.enrich-body p { margin: 0 0 .35rem; line-height: 1.5; }
/* The example sentence sits in the language being learned — show it prominently. */
.enrich-body .example { font-size: 1.1rem; }
.enrich-body .example-tr { font-size: .95rem; }          /* native-language gloss, secondary */
.enrich-body .example-src { font-size: .72rem; opacity: .7; margin-top: .15rem; }
.enrich-body .loading::after { content: ' '; }
.enrich-body .loading { opacity: .7; }

.word-actions { display: flex; align-items: center; gap: 1rem; margin-top: 1.5rem;
    padding-top: 1rem; border-top: 1px solid var(--line); }

/* ---- Learn mode (learn.php) ---- */
.learn-session .back, .learn-setup + * .back { color: var(--accent); text-decoration: none; font-size: .9rem; }

/* Setup: pile picker */
.learn-setup { display: flex; flex-direction: column; gap: 1rem; }
.pile-picker { border: 0; padding: 0; margin: 0; display: grid; gap: .5rem; }
.pile-picker legend { font-size: .9rem; color: var(--muted); margin-bottom: .35rem; padding: 0; }
.pile { display: grid; grid-template-columns: auto 1fr; align-items: baseline; gap: .1rem .6rem;
    border: 1px solid var(--line); border-radius: 10px; padding: .6rem .8rem; cursor: pointer; }
.pile:hover { border-color: var(--accent); }
.pile input { grid-row: span 2; align-self: center; }
.pile-main { font-weight: 600; }
.pile-count { display: inline-block; min-width: 1.4em; text-align: center; margin-left: .3rem;
    padding: 0 .4em; border-radius: 999px; background: var(--bg); color: var(--muted); font-size: .8rem; }
.pile-blurb { grid-column: 2; font-size: .82rem; }
.pile.is-empty { opacity: .55; }
.field { display: flex; flex-direction: column; gap: .25rem; font-size: .9rem; color: var(--muted); }

/* Session */
.learn-top { display: flex; justify-content: space-between; align-items: center;
    color: var(--muted); font-size: .85rem; margin-bottom: 1rem; }
.learn-scope { text-transform: uppercase; letter-spacing: .04em; font-size: .75rem; }
.learn-card { text-align: center; }
.learn-prompt { font-size: 1.7rem; font-weight: 700; margin: .5rem 0 .15rem; }
.learn-ask { margin: 0 0 1rem; font-size: .9rem; }
.learn-note { margin: -.5rem 0 1rem; }
#answer { width: 100%; text-align: center; font-size: 1.2rem; padding: .7rem; }
.learn-feedback { margin: 1rem 0; padding: .7rem .85rem; border-radius: 10px; text-align: left; }
.learn-feedback.is-right { background: #e7f6ec; color: #1c7a3f; }
.learn-feedback.is-wrong { background: #fdeceb; color: var(--danger); }
.learn-feedback .ans { font-weight: 700; }
.learn-feedback .speak { background: none; padding: 0 .2rem; font-size: 1rem; }
.learn-actions { display: flex; gap: .6rem; justify-content: center; flex-wrap: wrap; margin-top: 1rem; }
.learn-done { text-align: center; }
.learn-done h2 { margin: .25rem 0 .5rem; }
.learn-score { font-size: 1.1rem; margin: 0 0 1rem; }
.btn-link { display: inline-block; margin-left: .25rem; color: var(--accent); text-decoration: none; padding: .55rem; }

/* ---- Stats (stats.php) ---- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: .6rem; }
.stat { background: var(--bg); border-radius: 10px; padding: .7rem .4rem; text-align: center; }
.stat b { display: block; font-size: 1.5rem; line-height: 1.1; }
.stat span { font-size: .75rem; color: var(--muted); }

.bar-row { display: grid; grid-template-columns: 9.5rem 1fr 2.2rem; align-items: center; gap: .6rem; margin: .4rem 0; }
.bar-label { font-size: .85rem; }
.bar-label small { display: block; font-size: .7rem; }
.bar-track { background: var(--bg); border-radius: 999px; height: 12px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.bar-fill.is-learned { background: #1c7a3f; }
.bar-num { text-align: right; font-size: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.stat-table { width: 100%; border-collapse: collapse; }
.stat-table th { text-align: left; font-size: .75rem; color: var(--muted); font-weight: 600;
    padding: 0 .4rem .4rem; border-bottom: 1px solid var(--line); }
.stat-table td { padding: .5rem .4rem; border-top: 1px solid var(--line); }
.stat-table td:first-child { display: flex; align-items: center; gap: .4rem; }

.miss-badge { font-size: .8rem; font-weight: 600; color: var(--danger);
    background: #fdeceb; border-radius: 999px; padding: .05rem .5rem; white-space: nowrap; }
