:root {
  --indigo: #4f46e5;
  --indigo-dark: #4338ca;
  --bg: #f5f6fb;
  --card: #ffffff;
  --ink: #1f2233;
  --muted: #6b7280;
  --line: #e5e7eb;
  --green: #16a34a;
  --green-bg: #ecfdf5;
  --red: #dc2626;
  --red-bg: #fef2f2;
  --amber: #d97706;
  --radius: 16px;
  --shadow: 0 6px 20px rgba(31, 34, 51, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  font-size: 17px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Layout ------------------------------------------------------------------ */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
  padding: env(safe-area-inset-top) 16px 0;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 56px;
  text-decoration: none;
  color: var(--ink);
  font-weight: 700;
  min-width: 0;
}
.header-user {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  height: 56px;
}
.header-username {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header-link {
  font-size: 13px;
  font-weight: 600;
  color: var(--indigo-dark);
  text-decoration: none;
  white-space: nowrap;
}
.user-menu {
  position: relative;
}
.user-menu-trigger {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  user-select: none;
}
.user-menu-trigger::-webkit-details-marker { display: none; }
.user-menu-trigger::after {
  content: "";
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid var(--muted);
}
.user-menu[open] .user-menu-trigger {
  border-color: #c7cbe6;
  background: #f8f9ff;
}
.user-menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  width: min(260px, calc(100vw - 24px));
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px;
  z-index: 20;
}
.lang-form { margin: 0 0 12px; }
.lang-label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 6px;
}
.lang-select {
  width: 100%;
  font-size: 15px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
}
.lang-hint {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}
.logout-form { margin: 0; }
.btn-logout {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.btn-logout-menu {
  width: 100%;
  padding: 10px 12px;
}
.btn-logout:active { transform: translateY(1px); }
.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--indigo);
  color: #fff;
  font-weight: 800;
}
.brand-name { font-size: 18px; }

.container {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

.app-footer {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
}

/* Cards ------------------------------------------------------------------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 18px;
}

/* Hero -------------------------------------------------------------------- */
.hero { text-align: center; margin: 8px 0 20px; }
.hero h1 { font-size: 28px; line-height: 1.2; margin: 0 0 10px; }
.lede { color: var(--muted); margin: 0 auto; max-width: 34ch; }

/* Upload form ------------------------------------------------------------- */
.photo-drop {
  display: block;
  border: 2px dashed #c7cbe6;
  border-radius: var(--radius);
  background: #f8f9ff;
  padding: 8px;
  cursor: pointer;
  overflow: hidden;
}
.photo-drop-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 36px 16px;
  color: var(--muted);
  text-align: center;
}
.photo-icon { font-size: 40px; }
.photo-text { font-weight: 600; color: var(--ink); }
.photo-sub { font-size: 13px; color: var(--muted); }
.photo-privacy {
  display: block;
  margin-top: 10px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--muted);
  max-width: 28ch;
}
#photo-preview { width: 100%; border-radius: 12px; display: block; }
.photo-preview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
  width: 100%;
}
.photo-preview-grid img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  background: #f3f4f6;
}
.field-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; }

.field { display: block; margin-top: 18px; }
.field-label { display: block; font-weight: 600; margin-bottom: 8px; }
.field legend { font-weight: 600; margin-bottom: 8px; padding: 0; }
.field input[type="number"] {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 17px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.segmented-option {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 6px;
  border: 1px solid var(--line);
  border-radius: 12px;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  background: #fff;
}
.segmented-option input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.segmented-option:has(input:checked) {
  border-color: var(--indigo);
  background: #eef0ff;
  color: var(--indigo-dark);
}

.field-error, .field-error * { color: var(--red); font-size: 14px; margin-top: 6px; }

/* How it works ------------------------------------------------------------ */
.how { margin-top: 8px; }
.how h2 { font-size: 18px; }
.how ol { padding-left: 20px; color: var(--muted); }
.how li { margin-bottom: 6px; }
.how strong { color: var(--ink); }

/* Buttons ----------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  color: #fff;
  background: var(--indigo);
  transition: transform 0.05s ease, background 0.2s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn-primary { background: var(--indigo); }
.btn-primary:hover { background: var(--indigo-dark); }
.btn-secondary {
  background: #fff;
  color: var(--indigo-dark);
  border: 1px solid #c7cbe6;
}
.btn-secondary:hover { background: #f8f9ff; }
.btn-block { width: 100%; margin-top: 18px; }
.btn-ghost { background: transparent; color: var(--indigo-dark);
  border: 1px solid var(--line); }
.btn-text {
  background: transparent;
  color: var(--indigo-dark);
  border: none;
  box-shadow: none;
  font-size: 15px;
  font-weight: 600;
  padding: 10px 8px;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.btn-text:hover { color: var(--indigo); background: transparent; }
.btn-easier { background: var(--green); }
.btn-same { background: var(--indigo); }
.btn-harder { background: var(--red); }
.hint { text-align: center; color: var(--muted); font-size: 14px; margin-top: 10px; }

/* Alerts ------------------------------------------------------------------ */
.alert { border-radius: 12px; padding: 12px 14px; margin-bottom: 14px; font-size: 15px; }
.alert-error { background: var(--red-bg); color: var(--red); border: 1px solid #fecaca; }
.alert-success { background: var(--green-bg); color: var(--green); border: 1px solid #bbf7d0; }
.alert-info { background: #eff6ff; color: #1d4ed8; border: 1px solid #bfdbfe; }

/* Exercise / quiz --------------------------------------------------------- */
.exercise-head { margin-bottom: 14px; }
.exercise-head h1 { font-size: 22px; margin: 6px 0; }
.instructions { color: var(--muted); margin: 0; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag {
  font-size: 12px;
  font-weight: 600;
  color: var(--indigo-dark);
  background: #eef0ff;
  border-radius: 999px;
  padding: 4px 10px;
}
.tag-diff { background: #fff7ed; color: var(--amber); }

.q-number { font-size: 13px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 8px; }
.q-prompt { font-size: 18px; margin: 0 0 14px; }
.q-prompt.fill { line-height: 2.2; }
.q-hint { font-size: 14px; color: var(--muted); margin: -8px 0 12px; }

.options { display: flex; flex-direction: column; gap: 10px; }
.options-inline { flex-direction: row; gap: 12px; }
.option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  flex: 1;
}
.option input { width: 20px; height: 20px; accent-color: var(--indigo); flex: none; }
.option:has(input:checked) { border-color: var(--indigo); background: #eef0ff; }

.text-input, .blank-input, .match-select {
  font-size: 17px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  width: 100%;
}
.blank-input {
  display: inline-block;
  width: auto;
  min-width: 120px;
  padding: 6px 10px;
  border: none;
  border-bottom: 2px solid var(--indigo);
  border-radius: 0;
  background: #f8f9ff;
}
.blank-input.block { display: block; width: 100%; margin-bottom: 10px;
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }

.matching { display: flex; flex-direction: column; gap: 10px; }
.match-row { display: grid; grid-template-columns: 1fr 1.2fr; gap: 10px;
  align-items: center; }
.match-left { font-weight: 600; }

/* Results ----------------------------------------------------------------- */
.score-card { text-align: center; }
.score-big { font-size: 44px; font-weight: 800; }
.score-pct { font-size: 18px; color: var(--muted); }
.score-msg { margin: 8px 0 0; }
.score-good { border-color: #bbf7d0; }
.score-good .score-big { color: var(--green); }
.score-mid .score-big { color: var(--amber); }
.score-low .score-big { color: var(--red); }

.review-item.is-correct { border-left: 4px solid var(--green); }
.review-item.is-wrong { border-left: 4px solid var(--red); }
.badge { font-size: 13px; font-weight: 700; }
.is-correct .badge { color: var(--green); }
.is-wrong .badge { color: var(--red); }
.answer-line { display: flex; gap: 8px; font-size: 15px; margin-top: 6px;
  flex-wrap: wrap; }
.answer-label { color: var(--muted); }
.answer-val.correct { color: var(--green); font-weight: 600; }
.explanation { margin: 10px 0 0; color: var(--muted); font-size: 15px;
  background: #f8f9ff; padding: 10px 12px; border-radius: 10px; }
.explanation.note { background: #fff7ed; }

.more-card h2 { margin-top: 0; }
.more-card > p { color: var(--muted); margin: 0 0 4px; }
.more-primary { margin: 0; }
.more-primary .btn-block { margin-top: 12px; }
.more-secondary {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 16px;
  margin: 8px 0 4px;
}
.more-secondary form { margin: 0; }
.more-exits {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: 18px;
}
.more-exits a {
  color: var(--muted);
  font-size: 14px;
  text-decoration: none;
}
.more-exits a:hover { color: var(--indigo-dark); text-decoration: underline; }
.more-card-secondary {
  background: #fff;
  border-color: var(--line);
}
.more-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  margin-bottom: 12px; }
.more-actions .btn { width: 100%; padding: 14px 6px; }

/* Set detail -------------------------------------------------------------- */
.cta-card { border: 1px solid #c7cbe6; background: #f8f9ff; }
.cta-card h2 { margin-top: 0; }
.variant-list { list-style: none; margin: 0; padding: 0; }
.variant-list li { border-bottom: 1px solid var(--line); }
.variant-list li:last-child { border-bottom: none; }
.variant-list a { display: flex; justify-content: space-between; align-items: center;
  gap: 10px; padding: 14px 2px; text-decoration: none; color: var(--ink); }
.variant-meta { color: var(--muted); font-size: 14px; white-space: nowrap; }
.app-footer + *, .btn-block + .btn-block { margin-top: 10px; }

.passage { background: #fffdf6; border-color: #f3e8c8; }
.passage-text { margin: 0; white-space: pre-wrap; line-height: 1.6; }

/* Login ------------------------------------------------------------------- */
.login-form .code-input {
  width: 100%;
  font-size: 28px;
  letter-spacing: 0.35em;
  text-align: center;
  font-weight: 700;
  padding: 16px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  font-variant-numeric: tabular-nums;
}

/* Name edit / library ----------------------------------------------------- */
.name-edit { margin: 10px 0 14px; }
.name-edit-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.name-input { flex: 1; margin: 0; }
.name-save { margin: 0; width: auto; padding: 12px 16px; flex-shrink: 0; }

.library-list { list-style: none; margin: 0; padding: 0; }
.library-item {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
}
.library-main { min-width: 0; flex: 1; }
.library-name {
  display: block;
  font-weight: 700;
  font-size: 17px;
  color: var(--indigo-dark);
  text-decoration: underline;
  text-underline-offset: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.library-main .tags { margin: 8px 0 4px; }
.library-date { display: block; color: var(--muted); font-size: 13px; margin-top: 4px; }
.library-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.library-redo { margin: 0; width: auto; padding: 10px 14px; flex-shrink: 0; }
.library-edit { position: relative; }
.library-edit-trigger {
  list-style: none;
  margin: 0;
  width: auto;
  padding: 10px 14px;
  cursor: pointer;
  display: inline-block;
}
.library-edit-trigger::-webkit-details-marker { display: none; }
.library-rename-form {
  margin-top: 10px;
  width: 100%;
  flex-basis: 100%;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f8f9ff;
}
.library-edit[open] {
  flex-basis: 100%;
  width: 100%;
}
.library-delete-form { margin: 0; }
.btn-danger-ghost {
  background: transparent;
  color: var(--red);
  border: 1px solid #fecaca;
  margin: 0;
  width: auto;
  padding: 10px 14px;
}
.btn-danger-ghost:hover { background: var(--red-bg); }
