:root {
  --tap: 44px;
  --body: 16px;
  --line: 1.55;
  --pad: 1rem;
  --footer-h: 64px;
  --header-h: 52px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --bg: #f7f8fa;
  --fg: #1a1d21;
  --muted: #5f6368;
  --border: #dee2e6;
  --accent: #0d6efd;
  --ok: #198754;
  --bad: #dc3545;
  --surface: #fff;
  --input-line: #758997;
}

/* Round-053 S2-1: theme = system (media query) | light | dark (explicit data-theme on <html>). */
/* Round-059d P2: map bootstrap text-muted / body colors onto site dark tokens. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121417;
    --fg: #e8eaed;
    --muted: #9aa0a6;
    --border: #333;
    --accent: #8ab4f8;
    --surface: #1a1d21;
    --input-line: #91abbd;
    --bs-body-color: var(--fg);
    --bs-emphasis-color: var(--fg);
    --bs-secondary-color: var(--muted);
    --bs-border-color: var(--border);
    /* Audit 2026-11-11: Bootstrap's list/card/table/input backgrounds stayed #fff under light
       text (2.1:1 on マイセット). Red text (.text-danger) was 4.07:1 on the dark page. */
    --bs-body-bg: var(--surface);
    --bs-secondary-bg: #252a31;
    --bs-tertiary-bg: var(--surface);
    --bs-danger-rgb: 255, 123, 114;
    /* Sweep 11/13: Bootstrap's link (#0d6efd) and code (#d63384) colors were 4.09:1 on the
       dark page (報告・QA・取込の画面). Bootstrap's own dark values. */
    --bs-link-color: #6ea8fe;
    --bs-link-color-rgb: 110, 168, 254;
    --bs-link-hover-color: #8bb9fe;
    --bs-link-hover-color-rgb: 139, 185, 254;
    --bs-code-color: #e685b5;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121417;
  --fg: #e8eaed;
  --muted: #9aa0a6;
  --border: #333;
  --accent: #8ab4f8;
  --surface: #1a1d21;
  --input-line: #91abbd;
  --bs-body-color: var(--fg);
  --bs-emphasis-color: var(--fg);
  --bs-secondary-color: var(--muted);
  --bs-border-color: var(--border);
  --bs-body-bg: var(--surface);
  --bs-secondary-bg: #252a31;
  --bs-tertiary-bg: var(--surface);
  --bs-danger-rgb: 255, 123, 114;
  --bs-link-color: #6ea8fe;
  --bs-link-color-rgb: 110, 168, 254;
  --bs-link-hover-color: #8bb9fe;
  --bs-link-hover-color-rgb: 139, 185, 254;
  --bs-code-color: #e685b5;
  color-scheme: dark;
}

/* Round-053 S2-1: font scale (maイ の設定). 標準 = 16px. */
:root[data-font="sm"] {
  --body: 14px;
}

:root[data-font="lg"] {
  --body: 18.5px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: var(--body);
}

body.app-shell {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  line-height: var(--line);
  min-height: 100dvh;
  padding-bottom: calc(var(--footer-h) + var(--safe-b));
  /* UI-mobile T01: a long URL or file name breaks instead of widening the page. */
  overflow-wrap: break-word;
}

/* UI-mobile T01: Django as_p forms everywhere (マイページ・アップロード・登録・再設定).
   Label above, field full width, 48px tall, 16px text so iOS does not zoom. */
.app-main form > p > label {
  display: block;
  margin: 0 0 0.3rem;
  font-weight: 700;
}
.app-main form > p > :is(select, textarea, input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="submit"])),
.form-field > :is(select, textarea, input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="submit"])) {
  display: block;
  width: 100%;
  max-width: 100%;
  min-height: 48px;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--input-line);
  border-radius: 12px;
  background-color: var(--surface);
  color: var(--fg);
  font: inherit;
  font-size: max(1rem, 16px);
}
.app-main form > p:has(> input[type="checkbox"]) {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-end;
  align-items: center;
  gap: 0.6rem;
  min-height: 44px;
}
.app-main form > p:has(> input[type="checkbox"]) > label {
  margin: 0;
}
.app-main form > p > input[type="checkbox"] {
  width: 1.25rem;
  height: 1.25rem;
}
.app-main form .helptext {
  display: block;
  margin-top: 0.25rem;
  color: var(--muted);
  font-size: 0.875rem;
}
.app-main select {
  max-width: 100%;
}
/* UI-mobile T05: Bootstrap fields and buttons on the older pages (login, sign-up, reset,
   sets, upload) get the same 48px / 16px as the rest; small buttons keep a 44px target. */
.app-main :is(.form-control, .form-select) {
  min-height: 48px;
  font-size: max(1rem, 16px);
}
.app-main .btn {
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.app-main .btn-sm {
  min-height: 44px;
}

/* UI-mobile T05 FormField (templates/partials/form_field.html). */
.form-field {
  margin: 0 0 1rem;
}
.form-field__label {
  display: block;
  margin: 0 0 0.3rem;
  font-weight: 700;
}
.form-field__check {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 44px;
  font-weight: 700;
  cursor: pointer;
}
.form-field__check input {
  width: 1.25rem;
  height: 1.25rem;
  flex: none;
}
.form-field__help {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.875rem;
}
.form-field__error,
.form-field__error ul {
  margin: 0.25rem 0 0;
  padding: 0;
  list-style: none;
  color: var(--bad);
  font-weight: 700;
}
.form-field.has-error > :is(input, select, textarea) {
  border-color: var(--bad);
}

/* Wide data tables scroll inside their own box, never the whole page. */
.table-scroll {
  max-width: 100%;
  overflow-x: auto;
}
.table-scroll > table {
  min-width: 36rem;
}
.app-main td code {
  word-break: break-all;
}

@media (min-width: 992px) {
  body.app-shell {
    padding-bottom: 0;
    padding-left: 220px;
  }
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: var(--header-h);
  padding: 0.5rem var(--pad);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-header__back,
.app-header__action {
  min-width: var(--tap);
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--accent);
  border: 0;
  background: transparent;
  font: inherit;
  padding: 0 0.5rem;
}

.app-header__title {
  flex: 1;
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-main {
  padding: var(--pad);
  max-width: 720px;
  margin: 0 auto;
}

.app-tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  min-height: calc(var(--footer-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.app-tabs a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  min-height: var(--footer-h);
  text-decoration: none;
  color: var(--muted);
  font-size: 0.72rem;
  position: relative;
}

.app-tabs__ico {
  display: inline-flex;
  line-height: 0;
}

.app-tabs__ico svg {
  display: block;
}

.app-tabs a[aria-current="page"] {
  color: var(--accent);
  font-weight: 600;
}

.app-tabs__badge {
  position: absolute;
  top: 0.35rem;
  right: calc(50% - 1.4rem);
  min-width: 1.15rem;
  height: 1.15rem;
  padding: 0 0.25rem;
  border-radius: 999px;
  background: var(--bad);
  color: #fff;
  font-size: 0.65rem;
  line-height: 1.15rem;
  text-align: center;
}

@media (min-width: 992px) {
  .app-tabs {
    top: 0;
    bottom: 0;
    right: auto;
    width: 220px;
    grid-template-columns: 1fr;
    grid-auto-rows: minmax(var(--tap), auto);
    align-content: start;
    padding: 1rem 0.5rem;
    border-top: 0;
    border-right: 1px solid var(--border);
  }

  .app-tabs a {
    flex-direction: row;
    justify-content: flex-start;
    gap: 0.75rem;
    padding: 0 0.75rem;
    font-size: 0.95rem;
  }

  .app-tabs__badge {
    position: static;
    margin-left: auto;
  }
}

.choice-btn {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  width: 100%;
  min-height: var(--tap);
  margin: 0 0 0.5rem;
  padding: 0.85rem 1rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface);
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.choice-btn input {
  margin-top: 0.2rem;
  width: 1.15rem;
  height: 1.15rem;
}

.choice-btn.is-correct {
  border-color: var(--ok);
  background: color-mix(in srgb, var(--ok) 12%, var(--surface));
}

.choice-btn.is-wrong {
  border-color: var(--bad);
  background: color-mix(in srgb, var(--bad) 12%, var(--surface));
}

.answer-bar {
  position: sticky;
  bottom: calc(var(--footer-h) + var(--safe-b));
  z-index: 30;
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 0;
  background: linear-gradient(transparent, var(--bg) 35%);
}

.answer-bar .btn {
  flex: 1;
  min-height: var(--tap);
}

.tap-target {
  min-width: var(--tap);
  min-height: var(--tap);
}

.stat-grid {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: 1fr;
}

@media (min-width: 480px) {
  .stat-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

.stat-block {
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.85rem 1rem;
  background: var(--surface);
}

.chart-wrap {
  max-width: 640px;
  margin: 0.5rem 0 1.5rem;
}

.chart-wrap canvas {
  width: 100% !important;
  max-height: 280px;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  max-height: 70dvh;
  overflow: auto;
  background: var(--surface);
  border-radius: 1rem 1rem 0 0;
  border: 1px solid var(--border);
  padding: 1rem;
  transform: translateY(110%);
  transition: transform 0.2s ease;
}

.sheet.is-open {
  transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .sheet {
    transition: none;
  }
}

.demo-banner {
  margin: 0;
  border-radius: 0;
  text-align: center;
}

.figure-img {
  display: block;
  max-width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 0.35rem;
}

/* Round-059/059a/059d P1: exam_text tables fit container; wrap; scroll only if needed */
.exam-table-scroll {
  position: relative;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.exam-table-scroll[data-overflow="1"] {
  box-shadow: inset -12px 0 8px -8px color-mix(in srgb, var(--fg) 28%, transparent);
}
.exam-table-scroll-hint {
  display: none;
  margin: 0 0 0.25rem;
  font-size: 0.75rem;
  color: var(--muted);
}
.exam-table-scroll[data-overflow="1"] .exam-table-scroll-hint {
  display: block;
}
.exam-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.5rem 0;
  font-size: 0.95rem;
  color: var(--fg);
  background: var(--surface);
  table-layout: auto;
}
.exam-table th,
.exam-table td {
  border: 1px solid var(--hub-line, var(--border));
  padding: 0.35rem 0.55rem;
  vertical-align: top;
  text-align: left;
  color: var(--fg);
  background: var(--surface);
  word-break: keep-all;
  overflow-wrap: anywhere;
  min-width: 4.5em;
}
.exam-table th:first-child,
.exam-table td:first-child {
  min-width: 5em;
}
.exam-table thead th {
  background: var(--hub-chip-bg, color-mix(in srgb, var(--fg) 8%, var(--surface)));
  font-weight: 700;
  color: var(--fg);
}
