  :root {
    --bg: #0d1117;
    --panel: #161b22;
    --panel-2: #1f262e;
    --border: #30363d;
    --text: #c9d1d9;
    --muted: #8b949e;
    --accent: #58a6ff;
    --ok: #3fb950;
    --warn: #d29922;
    --bad: #f85149;
    --info: #79c0ff;
  }
  * { box-sizing: border-box; }
  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }
  body {
    margin: 0;
    font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-tap-highlight-color: rgba(88, 166, 255, 0.18);
    overscroll-behavior-y: contain;
  }
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    padding: 14px 22px;
    padding-top: max(14px, env(safe-area-inset-top));
    padding-left: max(22px, env(safe-area-inset-left));
    padding-right: max(22px, env(safe-area-inset-right));
    border-bottom: 1px solid var(--border);
    background: var(--panel);
    position: sticky;
    top: 0;
    z-index: 10;
  }
  header h1 { font-size: 16px; margin: 0; font-weight: 600; }
  header h1 a {
    display: inline-block;
    padding: 6px 4px;
    margin: -6px -4px;
    color: inherit;
    text-decoration: none;
  }
  .session-bar { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; font-size: 13px; color: var(--muted); min-width: 0; }
  .session-bar strong { color: var(--text); font-variant-numeric: tabular-nums; }
  .timer { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }
  .timer.warn { color: var(--warn); }
  .timer.danger { color: var(--bad); animation: pulse 1s infinite; }
  @keyframes pulse { 50% { opacity: 0.55; } }
  main {
    max-width: 980px;
    margin: 0 auto;
    padding: 24px 22px 80px;
    padding-left: max(22px, env(safe-area-inset-left));
    padding-right: max(22px, env(safe-area-inset-right));
    padding-bottom: calc(80px + env(safe-area-inset-bottom));
  }
  .card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 18px;
  }
  .meta {
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 8px;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
  }
  .topics { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 14px; }
  .topic-chip {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 2px 10px;
    font-size: 12px;
    color: var(--info);
  }
  .question-text {
    font-size: 15px;
    margin: 0 0 16px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  .options { list-style: none; padding: 0; margin: 0 0 18px; }
  .options li {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 6px;
    margin-bottom: 8px;
    cursor: pointer;
    transition: background 0.1s;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  @media (hover: hover) {
    .options li:hover { background: var(--panel-2); }
  }
  .options li:active { background: var(--panel-2); }
  .options li.selected { border-color: var(--accent); background: rgba(88, 166, 255, 0.08); }
  .options li.correct { border-color: var(--ok); background: rgba(63, 185, 80, 0.1); }
  .options li.wrong { border-color: var(--bad); background: rgba(248, 81, 73, 0.08); }
  .options input {
    margin-top: 4px;
    accent-color: var(--accent);
    width: 18px;
    height: 18px;
    flex-shrink: 0;
  }
  textarea, input[type="text"], input[type="password"], input[type="email"], input[type="number"] {
    font-size: 16px;
  }
  textarea {
    width: 100%;
    min-height: 70px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px 12px;
    color: var(--text);
    font-family: inherit;
    line-height: 1.5;
    resize: vertical;
  }
  .actions { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; align-items: center; }
  button {
    background: var(--accent);
    color: #0d1117;
    border: 0;
    padding: 10px 18px;
    min-height: 40px;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    font-size: 14px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }
  button:active:not(:disabled) { transform: scale(0.97); }
  button.ghost {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
  }
  button.danger { background: transparent; color: var(--bad); border: 1px solid var(--bad); }
  button:disabled { opacity: 0.5; cursor: not-allowed; }

  /* Start screen */
  .preset-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin: 16px 0; }
  .preset-card {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 18px;
    cursor: pointer;
    transition: all 0.15s;
  }
  @media (hover: hover) {
    .preset-card:hover { border-color: var(--accent); transform: translateY(-2px); }
  }
  .preset-card:active { border-color: var(--accent); background: var(--panel); }
  .preset-card h3 { margin: 0 0 6px; font-size: 18px; }
  .preset-card .nums { font-size: 26px; font-weight: 700; color: var(--accent); margin: 6px 0; }
  .preset-card .desc { font-size: 13px; color: var(--muted); }
  .custom-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 12px 0; }
  .custom-row label { color: var(--muted); font-size: 13px; }
  .custom-row input[type="number"] {
    width: 90px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 6px 8px;
    color: var(--text);
    font: inherit;
  }
  .toggle { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; cursor: pointer; }

  /* Result */
  .verdict {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 12px;
  }
  .verdict.correct { background: rgba(63, 185, 80, 0.18); color: var(--ok); }
  .verdict.partially_correct { background: rgba(210, 153, 34, 0.18); color: var(--warn); }
  .verdict.incorrect { background: rgba(248, 81, 73, 0.18); color: var(--bad); }
  .verdict.no_answer { background: var(--panel-2); color: var(--muted); }
  .explanation {
    color: var(--text);
    border-top: 1px solid var(--border);
    padding-top: 14px;
    margin-top: 14px;
    font-size: 13.5px;
    line-height: 1.65;
  }
  .explanation p { margin: 0 0 10px; }
  .explanation strong { color: var(--text); }
  .explanation code {
    background: var(--panel-2);
    color: var(--info);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 0.92em;
  }
  .explanation a {
    color: var(--accent);
    text-decoration: none;
    word-break: break-all;
  }
  .explanation a:hover { text-decoration: underline; }
  .explanation img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 12px auto;
    border-radius: 4px;
    background: white;
    padding: 6px;
  }
  .explanation ul, .explanation ol { padding-left: 24px; margin: 0 0 10px; }
  .explanation blockquote {
    border-left: 3px solid var(--border);
    margin: 8px 0;
    padding: 4px 12px;
    color: var(--muted);
  }
  .explanation table {
    border-collapse: collapse;
    margin: 10px 0;
  }
  .explanation th, .explanation td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    text-align: left;
  }
  .explanation .references, .explanation h4 + p a { word-break: break-all; }
  details summary { cursor: pointer; color: var(--accent); font-size: 13px; margin-top: 8px; }

  /* End summary */
  .summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin: 16px 0; }
  .summary-tile {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 14px;
    text-align: center;
  }
  .summary-tile .n { font-size: 28px; font-weight: 700; }
  .summary-tile .l { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-top: 4px; }
  .miss-list { list-style: none; padding: 0; margin: 12px 0 0; }
  .miss-list li { padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 13.5px; }
  .miss-list .vmark { font-weight: 700; margin-right: 8px; }
  .review-group { margin: 20px 0 0; }
  .review-group > h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
  .review-item {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    margin-bottom: 10px;
    padding: 0;
  }
  .review-item > summary {
    cursor: pointer;
    padding: 12px 14px;
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    font-size: 13.5px;
  }
  .review-item > summary::-webkit-details-marker { display: none; }
  .review-item > summary::before {
    content: "▸";
    color: var(--muted);
    transition: transform 0.15s;
    display: inline-block;
  }
  .review-item[open] > summary::before { transform: rotate(90deg); }
  .review-item > summary .qid { color: var(--muted); }
  .review-item > summary .ttaken { color: var(--muted); margin-left: auto; font-variant-numeric: tabular-nums; }
  .review-body { padding: 4px 16px 16px; border-top: 1px solid var(--border); }
  .review-body .question-text { margin: 12px 0 14px; }
  .review-body .options { margin: 0 0 14px; }
  .review-body .options li { cursor: default; }
  .review-body .options li:hover { background: transparent; }
  .review-body .user-reason {
    border-left: 3px solid var(--info);
    padding: 6px 12px;
    background: rgba(121, 192, 255, 0.06);
    margin: 12px 0;
    color: var(--text);
    font-size: 13.5px;
  }
  .review-body .user-reason .l { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); display: block; margin-bottom: 2px; }
  code { background: var(--panel-2); padding: 1px 5px; border-radius: 3px; font-size: 0.92em; }
  .hidden { display: none !important; }

  /* Auth + user chrome */
  .user-chip { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
  .user-chip strong { color: var(--text); }
  .user-chip button { padding: 4px 10px; font-size: 12px; background: transparent; color: var(--text); border: 1px solid var(--border); }
  .auth-card { max-width: 360px; margin: 60px auto 0; }
  .auth-card h2 { margin: 0 0 4px; }
  .auth-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 18px; }
  .auth-card .field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
  .auth-card label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
  .auth-card input {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 11px 12px;
    color: var(--text);
    font-family: inherit;
    font-size: 16px;
    min-height: 44px;
  }
  .auth-card .actions { margin-top: 6px; }
  .auth-card .switch { text-align: center; color: var(--muted); font-size: 13px; margin-top: 12px; }
  .auth-card .switch a { color: var(--accent); cursor: pointer; }
  .auth-card .err { color: var(--bad); font-size: 13px; margin: -6px 0 8px; min-height: 18px; }

  /* History */
  .history-grid { display: grid; gap: 10px; margin-top: 14px; }
  .history-row {
    display: grid;
    grid-template-columns: 1fr auto auto auto;
    align-items: center;
    gap: 12px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px 14px;
    font-size: 13.5px;
  }
  .history-row .when { color: var(--muted); font-size: 12px; }
  .history-row .preset { font-weight: 600; }
  .history-row .acc { font-variant-numeric: tabular-nums; color: var(--info); }
  .stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin: 10px 0 6px; }
  .stats-row .stat { background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; text-align: center; }
  .stats-row .stat .n { font-size: 22px; font-weight: 700; }
  .stats-row .stat .l { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
  .stats-row .recent-stat { border-color: rgba(88, 166, 255, 0.35); }
  .stats-row .recent-stat .n { color: var(--accent); }
  .trend {
    display: inline-block;
    margin-left: 4px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0;
    text-transform: none;
  }
  .trend.up   { color: var(--ok); }
  .trend.down { color: var(--bad); }
  .trend.flat { color: var(--muted); }

  /* Weakness sections */
  .section-title { margin: 22px 0 8px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
  .weak-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    align-items: center;
    gap: 14px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
    font-size: 13.5px;
  }
  .weak-row:last-child { border-bottom: 0; }
  .weak-row .label { color: var(--text); }
  .weak-row .count { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
  .weak-row .bar {
    width: 180px; height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden;
    position: relative;
  }
  .weak-row .bar-fill { height: 100%; border-radius: 4px; }
  .weak-row.bad   .bar-fill { background: var(--bad); }
  .weak-row.warn  .bar-fill { background: var(--warn); }
  .weak-row.ok    .bar-fill { background: var(--ok); }
  .weak-row .pct { min-width: 44px; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
  .weak-row.bad  .pct { color: var(--bad); }
  .weak-row.warn .pct { color: var(--warn); }
  .weak-row.ok   .pct { color: var(--ok); }
  .cat-row .label { font-size: 12.5px; }

  /* Share modal */
  .modal-backdrop {
    position: fixed; inset: 0; background: rgba(0,0,0,0.55);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
    animation: fade-in 0.12s ease-out;
  }
  @keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
  .modal {
    background: var(--panel); border: 1px solid var(--border);
    border-radius: 8px; padding: 22px; max-width: 520px; width: calc(100% - 32px);
  }
  .modal h3 { margin: 0 0 4px; font-size: 17px; }
  .modal .sub { color: var(--muted); font-size: 13px; margin-bottom: 14px; }
  .share-url {
    display: flex; gap: 8px; align-items: stretch;
    background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px;
    padding: 4px; margin-bottom: 10px;
  }
  .share-url input {
    flex: 1; background: transparent; border: 0; color: var(--text);
    font-family: inherit; font-size: 16px;
    padding: 6px 10px; outline: none; min-width: 0;
  }
  .share-url button { padding: 8px 14px; font-size: 13px; min-height: 36px; }
  .countdown { color: var(--warn); font-variant-numeric: tabular-nums; font-size: 13px; }
  .countdown.expired { color: var(--bad); }
  .modal .actions { justify-content: flex-end; margin-top: 12px; }
  button.share-btn {
    background: transparent; color: var(--info); border: 1px solid var(--info);
    padding: 8px 12px; font-size: 12px;
    min-height: 36px;
  }
  @media (hover: hover) {
    button.share-btn:hover { background: rgba(121, 192, 255, 0.1); }
  }

  /* Touch-friendly text links inside .actions rows */
  .text-link {
    color: var(--accent);
    text-decoration: none;
    padding: 8px 6px;
    min-height: 36px;
    display: inline-flex;
    align-items: center;
  }

  /* History row — new flexible layout (replaces old grid) */
  .history-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px 14px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 12px 14px;
    font-size: 13.5px;
  }
  .history-row .history-row-main {
    flex: 1 1 200px;
    min-width: 0;
    text-decoration: none;
    color: inherit;
    cursor: pointer;
  }
  .history-row .history-row-stats {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px 14px;
    font-variant-numeric: tabular-nums;
  }
  .history-row .ratio { color: var(--text); }
  .history-row .sid { font-size: 12px; }
  .history-row .when { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
  .history-row .preset { font-weight: 600; }

  /* ============================================================
     Mobile / narrow-viewport adjustments (iPhone, etc.)
     ============================================================ */
  @media (max-width: 720px) {
    body { font-size: 15px; }
    header {
      padding: 10px 14px;
      padding-top: max(10px, env(safe-area-inset-top));
      padding-left: max(14px, env(safe-area-inset-left));
      padding-right: max(14px, env(safe-area-inset-right));
    }
    header h1 { font-size: 15px; }
    .session-bar { gap: 10px; font-size: 12px; }
    .session-bar #progress { font-size: 13px; }
    .timer { font-size: 15px; }
    main {
      padding: 16px 14px;
      padding-left: max(14px, env(safe-area-inset-left));
      padding-right: max(14px, env(safe-area-inset-right));
      padding-bottom: calc(48px + env(safe-area-inset-bottom));
    }
    .card { padding: 16px; border-radius: 8px; }
    .preset-grid { grid-template-columns: 1fr; gap: 10px; }
    .preset-card { padding: 14px; }
    .preset-card .nums { font-size: 22px; }
    .options li { padding: 14px; gap: 12px; }
    .options input { width: 20px; height: 20px; }
    .question-text { font-size: 15.5px; }
    .summary-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .summary-tile { padding: 12px; }
    .summary-tile .n { font-size: 22px; }
    .stats-row { grid-template-columns: repeat(2, 1fr); }
    .stats-row .stat .n { font-size: 19px; }
    /* End-session action row stacks neatly */
    .actions { gap: 8px; }
    .actions > * { flex: 0 1 auto; }
    /* Auth card — full-bleed friendlier */
    .auth-card { margin: 24px auto 0; padding: 20px; }
    /* Weak rows — let bar shrink on tiny screens */
    .weak-row { grid-template-columns: 1fr auto; row-gap: 6px; }
    .weak-row .bar { grid-column: 1 / -1; width: 100%; }
    /* History stats can wrap below the link */
    .history-row { padding: 12px; }
    .history-row .history-row-stats { width: 100%; justify-content: space-between; }
    .history-row .sid { display: none; }
    /* Hide the username chip on tiny screens — buttons stay */
    .user-chip > span:first-child { display: none; }
    /* Review summaries lay out vertically */
    .review-item > summary { padding: 12px; gap: 6px; }
    .review-item > summary .ttaken { margin-left: 0; }
    .review-body { padding: 4px 12px 14px; }
    /* Modal padding tightens */
    .modal { padding: 18px; }
  }

  @media (max-width: 380px) {
    header h1 { font-size: 14px; }
    .summary-grid { grid-template-columns: 1fr 1fr; }
    .stats-row { grid-template-columns: 1fr 1fr; }
    .user-chip button { padding: 8px 10px; }
    .preset-card .nums { font-size: 20px; }
  }

  /* Honor prefers-reduced-motion (a11y) */
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      animation-duration: 0.001ms !important;
      transition-duration: 0.001ms !important;
    }
  }
