:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --border: #dfe3e8;
  --text: #1f2933;
  --text-muted: #62727f;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
}

header.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 24px;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 10;
}

header.app-header h1 {
  font-size: 16px;
  margin: 0;
  white-space: nowrap;
}

nav.tabs {
  display: flex;
  gap: 4px;
}

nav.tabs button {
  border: none;
  background: transparent;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-muted);
}

nav.tabs button.active {
  background: #e8effe;
  color: var(--primary-dark);
  font-weight: 600;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px;
}

.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}

.grid {
  display: grid;
  gap: 16px;
}

.stat-grid {
  grid-template-columns: repeat(4, 1fr);
  margin-bottom: 20px;
}

.stat-card .label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.stat-card .value {
  font-size: 26px;
  font-weight: 700;
}

.stat-card .sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

.section-title {
  font-size: 15px;
  font-weight: 700;
  margin: 28px 0 12px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}

th { color: var(--text-muted); font-weight: 600; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.badge.pass { background: #dcfce7; color: var(--success); }
.badge.fail { background: #fef3c7; color: var(--warning); }

.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* Practice setup */
.mode-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin: 16px 0;
}

.mode-card {
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  background: var(--surface);
}

.mode-card.selected {
  border-color: var(--primary);
  background: #f0f5ff;
}

.mode-card h3 { margin: 0 0 6px; font-size: 14px; }
.mode-card p { margin: 0; font-size: 12px; color: var(--text-muted); }

.form-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.form-row label { font-size: 13px; width: 110px; color: var(--text-muted); }

select, input[type="number"], input[type="text"], input[type="password"] {
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  font-size: 14px;
}

button.primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 10px 22px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}
button.primary:hover { background: var(--primary-dark); }

button.ghost {
  background: transparent;
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}

/* Quiz screen */
.quiz-layout {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 16px;
  height: calc(100vh - 130px);
}

.pdf-pane {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: #525659;
  display: flex;
  flex-direction: column;
}

.pdf-toolbar {
  background: #3a3d40;
  color: #fff;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.pdf-toolbar input {
  width: 50px;
  padding: 4px 6px;
  border-radius: 4px;
  border: none;
}

.pdf-toolbar button.ghost { color: #fff; border-color: #6b7075; }

.pdf-pane iframe {
  flex: 1;
  border: none;
}

.answer-pane {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.progress-bar {
  height: 8px;
  border-radius: 4px;
  background: #e5e7eb;
  overflow: hidden;
}
.progress-bar > div {
  height: 100%;
  background: var(--primary);
}

.choice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.choice-btn {
  padding: 18px;
  font-size: 18px;
  font-weight: 700;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: var(--surface);
  cursor: pointer;
}
.choice-btn:hover { border-color: var(--primary); }
.choice-btn.correct { border-color: var(--success); background: #dcfce7; }
.choice-btn.wrong { border-color: var(--danger); background: #fee2e2; }
.choice-btn:disabled { cursor: default; }
.choice-btn.selected-choice { border-color: var(--primary); background: #f0f5ff; }

.feedback {
  padding: 12px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
}
.feedback.correct { background: #dcfce7; color: var(--success); }
.feedback.wrong { background: #fee2e2; color: var(--danger); }

.result-summary {
  text-align: center;
  padding: 40px 20px;
}
.result-summary .score {
  font-size: 48px;
  font-weight: 800;
  margin: 12px 0;
}

svg.chart text { font-size: 11px; fill: var(--text-muted); }

/* ---- login / account ---- */
.user-area {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
}
.user-area button.ghost { padding: 6px 12px; font-size: 13px; }

.login-wrap {
  max-width: 380px;
  margin: 80px auto;
  padding: 0 16px;
}
.login-wrap h1 { font-size: 18px; margin: 0 0 16px; text-align: center; }
.stack-form label { display: block; font-size: 13px; color: var(--text-muted); margin: 12px 0 4px; }
.stack-form input { width: 100%; }
.stack-form button.primary { width: 100%; margin-top: 18px; }
.form-error { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 1em; }
.form-ok { color: var(--success); font-size: 13px; margin-top: 10px; }

/* PDF を別タブで開くリンク(埋め込み表示できない端末向けにも使う) */
.pdf-open {
  color: #fff;
  font-size: 12px;
  border: 1px solid #6b7075;
  border-radius: 6px;
  padding: 4px 10px;
  text-decoration: none;
  white-space: nowrap;
}
.pdf-fallback {
  display: none;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 24px 16px;
  color: #e5e7eb;
  font-size: 13px;
  text-align: center;
}
.pdf-fallback a.primary {
  background: var(--primary);
  color: #fff;
  padding: 10px 22px;
  border-radius: 8px;
  font-weight: 600;
  text-decoration: none;
}
/* Android の Chrome などはページ内に PDF を表示できないため、iframe の代わりに案内を出す */
body.no-inline-pdf .pdf-pane iframe { display: none; }
body.no-inline-pdf .pdf-fallback { display: flex; }
body.no-inline-pdf .pdf-pane { height: auto; }

/* ---- タブレット(縦向き)以下 ---- */
@media (max-width: 1024px) {
  .mode-cards { grid-template-columns: repeat(2, 1fr); }
  /* 問題PDFと解答欄を縦に並べる */
  .quiz-layout {
    grid-template-columns: 1fr;
    height: auto;
  }
  .pdf-pane { height: 70vh; height: 70dvh; }
}

/* ---- スマホ ---- */
@media (max-width: 820px) {
  header.app-header {
    flex-wrap: wrap;
    height: auto;
    padding: 8px 12px 0;
    gap: 6px 12px;
  }
  header.app-header h1 { font-size: 15px; }
  .user-area button.ghost { padding: 4px 8px; font-size: 12px; }
  /* タブは4等分で並べ、隠れるものが出ないようにする */
  nav.tabs {
    order: 3;
    width: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    padding-bottom: 6px;
  }
  nav.tabs button { padding: 8px 2px; font-size: 12px; line-height: 1.3; }

  main { padding: 12px; }
  .card { padding: 14px; overflow-x: auto; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat-card .value { font-size: 22px; }
  .two-col { grid-template-columns: 1fr; }
  .two-col { margin-top: 12px; }
  table { font-size: 12px; }
  th, td { padding: 6px 8px; white-space: nowrap; }

  .form-row { flex-wrap: wrap; }
  .form-row label { width: 100%; }
  .form-row select, .form-row input { flex: 1; min-width: 0; }
  /* iOS は 16px 未満の入力欄にフォーカスすると画面が拡大されるため */
  select, input[type="number"], input[type="text"], input[type="password"], textarea { font-size: 16px !important; }

  .pdf-pane { height: 60vh; height: 60dvh; }
  .pdf-toolbar { flex-wrap: wrap; }
  .choice-btn { padding: 14px; }
  .result-summary { padding: 28px 12px; }
  .result-summary .score { font-size: 40px; }
  .result-summary > div:last-child { flex-wrap: wrap; }
  .login-wrap { margin: 40px auto; }
}

@media (max-width: 480px) {
  .mode-cards { grid-template-columns: 1fr; }
  header.app-header h1 { width: 100%; }
  .user-area { margin-left: 0; }
  #pm-choice-buttons { grid-template-columns: repeat(4, 1fr) !important; }
}
