/* =========================================================
   Mandarinworld styles, written PHONE-FIRST.
   The basic rules are for phones. Wider screens get extra
   rules inside @media (min-width: ...) blocks further down.
   ========================================================= */

:root {
  --red: #c8102e;
  --red-dark: #9e0c24;
  --red-soft: #fff5f6;
  --ink: #1f1f1f;
  --muted: #666;
  --line: #e3e3e3;
  --bg: #faf8f5;
  --card: #fff;
  --ok: #1d7a3a;
  --ok-bg: #e7f5ec;
  --warn: #9a5b00;
  --warn-bg: #fff3dd;
  --bad: #b3261e;
  --bad-bg: #fdecea;
  --radius: 12px;
  --tap: 44px; /* smallest comfortable size to tap with a finger */
  --page-max: 72rem; /* widest the content gets on big screens */
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px; /* 16px stops phones zooming in when you tap a box */
  color: var(--ink);
  background: var(--bg);
  line-height: 1.5;
}

h1 { font-size: 1.4rem; margin: 0 0 1rem; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
[hidden] { display: none !important; }
.desktop-only { display: none; } /* shown from 900px wide, see below */

/* ---------- Top bar ---------- */
header.top {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.top-inner {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: .5rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  column-gap: 1rem;
}
.brand {
  font-weight: 700;
  color: var(--red);
  text-decoration: none;
  font-size: 1.1rem;
  line-height: var(--tap);
}
nav { display: flex; flex-wrap: wrap; column-gap: 1rem; align-items: center; }
nav a, nav button.link {
  color: var(--ink);
  text-decoration: none;
  font: inherit;
  font-size: .95rem;
  white-space: nowrap;
  line-height: var(--tap); /* tall enough to tap */
}
nav a:hover, nav button.link:hover { color: var(--red); }
nav a[aria-current="page"] { color: var(--red); font-weight: 600; }

/* ---------- Page area ---------- */
main.page {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 1rem 1rem 3rem;
}
main.page.narrow, main.page.auth-page { max-width: 30rem; } /* forms stay a comfortable width */

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
}

/* Two columns on wide screens (stacked on phones). */
.two-col { display: grid; gap: 0 1.25rem; align-items: start; }

/* ---------- Forms ---------- */
label { display: block; font-weight: 600; margin: 1rem 0 .25rem; }
.card > label:first-child { margin-top: 0; }
input, textarea, select {
  width: 100%;
  min-height: var(--tap);
  padding: .6rem .8rem;
  font: inherit;
  color: inherit;
  border: 1px solid #ccc;
  border-radius: 8px;
  background: #fff;
}
textarea { min-height: 4.5rem; resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--red); outline-offset: 1px; border-color: var(--red); }
input[readonly] { background: #f3f3f3; }
input[type=checkbox] { width: 1.25rem; height: 1.25rem; min-height: 0; accent-color: var(--red); flex: none; }
.hint { font-size: .85rem; color: var(--muted); margin: .25rem 0 0; }
.check { display: flex; align-items: center; gap: .5rem; font-weight: 400; min-height: var(--tap); margin: 0; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  margin-top: 1.25rem;
  padding: .6rem 1.25rem;
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--red);
  border: 1px solid var(--red);
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}
.btn:hover { background: var(--red-dark); border-color: var(--red-dark); }
.btn:disabled { opacity: .6; cursor: wait; }
.btn.full { width: 100%; }
.btn.secondary { background: #fff; color: var(--red); }
.btn.secondary:hover { background: var(--red-soft); }
.btn.danger { background: #fff; color: var(--bad); border-color: var(--bad); }
.btn.danger:hover { background: var(--bad-bg); }
.btn.small { margin-top: 0; padding: .4rem .85rem; font-size: .9rem; min-height: 40px; }
.btn-row { display: flex; gap: .75rem; flex-wrap: wrap; }
.btn-row .btn { flex: 1 1 auto; } /* on phones, buttons share the row */
button.link { background: none; border: 0; padding: 0; cursor: pointer; }

/* ---------- Messages and badges ---------- */
.message { padding: .75rem 1rem; border-radius: 8px; margin: 1rem 0 0; }
.message.error { color: var(--bad); background: var(--bad-bg); }
.message.success { color: var(--ok); background: var(--ok-bg); }
.message.info { color: var(--warn); background: var(--warn-bg); }
.card > .message:first-child, h1 + .message { margin-top: 0; margin-bottom: .5rem; }

.badge { display: inline-block; padding: .15rem .6rem; border-radius: 999px; font-size: .8rem; font-weight: 600; white-space: nowrap; }
.badge.active, .badge.admin { color: var(--ok); background: var(--ok-bg); }
.badge.expiring { color: var(--warn); background: var(--warn-bg); }
.badge.expired { color: var(--bad); background: var(--bad-bg); }
.badge.inactive { color: var(--muted); background: #eee; }

.links { margin: 1.25rem 0 0; }
.links a { color: var(--red); display: inline-block; min-height: var(--tap); line-height: var(--tap); }

/* ---------- Log in / sign up: welcome panel ---------- */
.auth { display: grid; gap: 1rem; }
.auth-panel { display: none; } /* phones: the form comes first, no extra panel */
.auth-panel .hanzi-hero { color: #fff; }
.auth-panel ul { padding-left: 1.2rem; margin: 1rem 0 0; }
.auth-panel li { margin: .4rem 0; }

/* ---------- Install card ---------- */
.install-steps { margin-top: 1rem; padding: .85rem 1rem; background: var(--bg); border-radius: 10px; }
.install-steps-title { margin: 0 0 .35rem; font-weight: 600; }
.install-steps ol { margin: 0; padding-left: 1.3rem; }
.install-steps li { margin: .35rem 0; }
.install-warning { margin: .75rem 0 0; padding: .6rem .75rem; border-radius: 8px; color: var(--bad); background: var(--bad-bg); font-size: .9rem; }
#install-browser { margin-top: .75rem; }

/* ---------- Home ---------- */
.hanzi-hero { font-size: 3rem; margin: 0; color: var(--red); line-height: 1.2; }
.lesson-list { display: grid; gap: .5rem; margin-top: 1rem; }
.lesson {
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  min-height: 3.75rem;
  padding: .75rem 1rem; border: 1px solid var(--line); border-radius: 10px;
  color: var(--ink); text-decoration: none; background: #fff;
}
.lesson:hover { border-color: var(--red); }
.lesson.locked { color: var(--muted); background: #fafafa; }
.lesson .small { display: block; }

/* ---------- Word list ---------- */
.filters { display: grid; gap: .5rem; }
.filters label { margin: 0 0 .25rem; font-size: .85rem; color: var(--muted); }
.toggles { display: flex; column-gap: 1.25rem; flex-wrap: wrap; margin-top: .5rem; }
.word-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: .6rem; margin-top: .75rem; }
.word {
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  padding: .9rem .5rem; text-align: center;
  overflow-wrap: anywhere;
}
.word .hanzi { font-size: 2rem; line-height: 1.2; margin: 0; }
.word .pinyin { color: var(--red); font-size: 1rem; margin: .35rem 0 0; }
.word .meaning { margin: .3rem 0 0; font-size: .95rem; }
.word .level { color: var(--muted); font-size: .75rem; margin: .4rem 0 0; }
.hide-pinyin .word .pinyin, .hide-meaning .word .meaning { visibility: hidden; }

/* ---------- Quiz ---------- */
main.page.quiz-page { max-width: 40rem; }
.quiz-top { display: flex; justify-content: space-between; align-items: center; gap: 1rem; font-size: .9rem; color: var(--muted); }
.progress { height: 8px; background: #eee; border-radius: 999px; overflow: hidden; margin: .5rem 0 1rem; }
.progress > div { height: 100%; width: 0; background: var(--red); transition: width .25s ease; }
.quiz-prompt { color: var(--muted); margin: 0; font-size: .95rem; }
.quiz-meaning { font-size: 1.6rem; font-weight: 700; margin: .25rem 0 1rem; line-height: 1.3; overflow-wrap: anywhere; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.choice {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 6.5rem;
  padding: .75rem .5rem;
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  overflow-wrap: anywhere;
}
.choice:hover:not(:disabled) { border-color: var(--red); }
.choice:disabled { cursor: default; }
.choice .hanzi { font-size: 1.9rem; line-height: 1.2; }
.choice .pinyin { color: var(--red); font-size: .95rem; margin-top: .25rem; }
.choice .key { position: absolute; top: .35rem; left: .5rem; font-size: .7rem; color: var(--muted); display: none; }
.choice.correct { border-color: var(--ok); background: var(--ok-bg); }
.choice.wrong { border-color: var(--bad); background: var(--bad-bg); }
.choice.faded { opacity: .55; }
.hide-choice-pinyin .choice .pinyin { display: none; }
.feedback { min-height: 1.5rem; margin: 1rem 0 0; font-weight: 600; }
.feedback.ok { color: var(--ok); }
.feedback.bad { color: var(--bad); }
.result-score { font-size: 2.5rem; font-weight: 700; color: var(--red); margin: 0; }
.review-list { list-style: none; padding: 0; margin: .5rem 0 0; }
.review-list li { display: flex; gap: .75rem; align-items: baseline; padding: .5rem 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.review-list li:last-child { border-bottom: 0; }
.review-list .hanzi { font-size: 1.4rem; }
.review-list .pinyin { color: var(--red); }

/* ---------- Admin ---------- */
.tabs { display: flex; gap: .5rem; margin-bottom: 1rem; }
.tabs button {
  flex: 1;
  min-height: var(--tap);
  font: inherit;
  font-weight: 600;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.tabs button[aria-selected="true"] { background: var(--red); border-color: var(--red); color: #fff; }

.toolbar { display: grid; gap: .5rem; margin-bottom: 1rem; }

.user-list { display: grid; gap: .75rem; align-items: start; }
.user-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.user-card.expiring { border-left: 4px solid var(--warn); }
.user-card.expired { border-left: 4px solid var(--bad); }
.user-card.active, .user-card.admin { border-left: 4px solid var(--ok); }
.user-head { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.user-head strong { font-size: 1.05rem; overflow-wrap: anywhere; }
.user-meta { color: var(--muted); font-size: .9rem; margin: .35rem 0 0; overflow-wrap: anywhere; }
.user-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin-top: .75rem; }
.user-actions .btn { width: 100%; }
.inline-row { display: flex; gap: .5rem; align-items: center; margin-top: .5rem; flex-wrap: wrap; }
.inline-row input:not([type=checkbox]), .inline-row select { width: auto; flex: 1; min-width: 9rem; }
.user-card details { margin-top: .75rem; }
.user-card summary { cursor: pointer; color: var(--red); font-size: .9rem; min-height: var(--tap); display: flex; align-items: center; }
.user-card label { margin-top: .5rem; font-size: .9rem; }
.note-status { font-size: .85rem; color: var(--ok); }

.form-grid { display: grid; gap: 0 1rem; }

.topic-row { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .6rem 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.topic-row:last-child { border-bottom: 0; }
.topic-row label { display: inline-flex; align-items: center; gap: .35rem; margin: 0; font-weight: 400; min-height: var(--tap); }
#topic-form { border-top: 1px solid var(--line); margin-top: .5rem; padding-top: .25rem; }
#topic-form .btn { margin-top: .5rem; }

/* Word table: on phones each row becomes a small card. */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .55rem .5rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .85rem; color: var(--muted); font-weight: 600; }
td.hanzi { font-size: 1.35rem; }
td.actions { white-space: nowrap; text-align: right; }
td.actions .btn { margin-left: .25rem; }
table.stack thead { display: none; }
table.stack tr {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: .1rem .75rem;
  padding: .75rem 0;
  border-bottom: 1px solid var(--line);
}
table.stack td { border: 0; padding: 0; }
table.stack td.hanzi { grid-row: span 4; font-size: 1.8rem; align-self: center; min-width: 3.5rem; }
table.stack td.actions { grid-column: 1 / -1; text-align: left; margin-top: .5rem; display: flex; gap: .5rem; }
table.stack td.actions .btn { margin: 0; flex: 1; }
table.stack td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: .85rem; }
table.stack td.muted-cell { color: var(--muted); font-size: .9rem; }
#word-filter { margin-bottom: .5rem; }

dialog { border: 0; border-radius: var(--radius); padding: 1.25rem; max-width: 26rem; width: calc(100% - 2rem); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.temp-password {
  font-family: ui-monospace, Consolas, monospace; font-size: 1.3rem; text-align: center;
  padding: .75rem; background: var(--bg); border: 1px dashed var(--red); border-radius: 8px;
  margin: 1rem 0; user-select: all; overflow-wrap: anywhere;
}

/* =========================================================
   Bigger phones and small tablets (600px and up)
   ========================================================= */
@media (min-width: 600px) {
  main.page { padding: 1.5rem 1.5rem 3rem; }
  .top-inner { padding: .5rem 1.5rem; flex-wrap: nowrap; }
  h1 { font-size: 1.6rem; }
  .card { padding: 1.5rem; }
  .btn-row .btn { flex: 0 0 auto; }
  .word-grid { grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr)); gap: .75rem; }
  .word .hanzi { font-size: 2.3rem; }
  .filters { grid-template-columns: 2fr 1fr; }
  .toolbar { grid-template-columns: 1fr 16rem; }
  .form-grid { grid-template-columns: 1fr 1fr; }
  .form-grid .full-row { grid-column: 1 / -1; }
  .user-actions { display: flex; flex-wrap: wrap; }
  .user-actions .btn { width: auto; }

  /* Word table back to a normal table. */
  table.stack thead { display: table-header-group; }
  table.stack tr { display: table-row; padding: 0; }
  table.stack td { display: table-cell; border-bottom: 1px solid var(--line); padding: .55rem .5rem; }
  table.stack td.hanzi { font-size: 1.35rem; }
  table.stack td.actions { display: table-cell; text-align: right; margin: 0; }
  table.stack td.actions .btn { flex: none; margin-left: .25rem; }
  table.stack td[data-label]::before { content: none; }
  table.stack td.muted-cell { color: inherit; font-size: inherit; }
}

/* =========================================================
   Computers and big tablets (900px and up)
   ========================================================= */
@media (min-width: 900px) {
  main.page { padding-top: 2rem; }
  h1 { font-size: 1.8rem; }

  .two-col { grid-template-columns: 1fr 1fr; }
  .two-col.wide-left { grid-template-columns: 3fr 2fr; }

  /* Log in / sign up: welcome panel beside the form. */
  main.page.auth-page { max-width: 58rem; }
  .auth { grid-template-columns: 1fr 1fr; gap: 0; align-items: stretch; }
  .auth .card { margin: 0; border-radius: 0 var(--radius) var(--radius) 0; }
  .auth-panel {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 2.5rem;
    background: linear-gradient(150deg, var(--red), var(--red-dark));
    color: #fff;
    border-radius: var(--radius) 0 0 var(--radius);
  }
  .auth-panel h2 { font-size: 1.5rem; margin: .5rem 0; }

  /* Word list: filters stay in a side panel while you scroll. */
  .words-layout { display: grid; grid-template-columns: 19rem 1fr; gap: 1.5rem; align-items: start; }
  .words-layout .side { position: sticky; top: 4.5rem; }
  .words-layout .filters { grid-template-columns: 1fr; }
  .words-layout .toggles { flex-direction: column; }
  .word-grid { grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr)); }
  .word { padding: 1.25rem .75rem; }
  .word .hanzi { font-size: 2.6rem; }

  /* Quiz: bigger choices, keyboard numbers shown. */
  .quiz-meaning { font-size: 2rem; }
  .choice { min-height: 8rem; }
  .choice .hanzi { font-size: 2.4rem; }
  .choice .key { display: block; }
  .desktop-only { display: block; }
}

/* =========================================================
   Wide computer screens (1200px and up)
   ========================================================= */
@media (min-width: 1200px) {
  .user-list { grid-template-columns: 1fr 1fr; }
}
