/* ============================================================
   素数判定シミュレーター
   ============================================================ */

:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --panel-2: #fbfcfe;
  --border: #e2e6f0;
  --border-strong: #cdd4e4;
  --text: #1b2133;
  --muted: #6a7285;
  --faint: #9aa3b8;

  --accent: #4f46e5;
  --accent-fg: #ffffff;
  --accent-soft: #eef0fe;

  --prime-fg: #9a5b00;
  --prime-bg: #fdf0d0;
  --prime-bd: #f0b429;

  --comp-fg: #93a0b4;
  --comp-bg: #f1f4f9;
  --comp-bd: #e2e8f2;

  --cand-fg: #8b94a8;
  --cand-bg: #f7f9fc;
  --cand-bd: #e6ebf4;

  --unit-fg: #8b94a8;
  --unit-bg: transparent;
  --unit-bd: #c3cbdb;

  --cur-fg: #b91c1c;
  --cur-bg: #ffe2e2;
  --cur-bd: #ef4444;

  --base-fg: #3730a3;
  --base-bg: #e2e6ff;
  --base-bd: #6366f1;

  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 6px 18px rgba(16, 24, 40, .05);
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #0e1117;
  --panel: #171b24;
  --panel-2: #1c212c;
  --border: #262c39;
  --border-strong: #38404f;
  --text: #e6e9f0;
  --muted: #9aa4b8;
  --faint: #6b7689;

  --accent: #818cf8;
  --accent-fg: #10131a;
  --accent-soft: #232842;

  --prime-fg: #fbbf24;
  --prime-bg: rgba(251, 191, 36, .14);
  --prime-bd: rgba(251, 191, 36, .55);

  --comp-fg: #5d6a80;
  --comp-bg: rgba(93, 106, 128, .12);
  --comp-bd: rgba(93, 106, 128, .35);

  --cand-fg: #8b94a8;
  --cand-bg: rgba(139, 148, 168, .07);
  --cand-bd: rgba(139, 148, 168, .28);

  --unit-fg: #8b94a8;
  --unit-bg: transparent;
  --unit-bd: #3d4657;

  --cur-fg: #fca5a5;
  --cur-bg: rgba(239, 68, 68, .16);
  --cur-bd: #ef4444;

  --base-fg: #c7d2fe;
  --base-bg: rgba(99, 102, 241, .18);
  --base-bd: rgba(129, 140, 248, .7);

  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 14px;
  line-height: 1.55;
  min-height: 100vh;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0; font-weight: 650; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- ヘッダー ---------- */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  border-radius: 11px;
  background: linear-gradient(140deg, var(--accent), #7c3aed);
  color: #fff;
  font-size: 19px;
  font-family: var(--mono);
}

.brand-text h1 {
  font-size: 19px;
  letter-spacing: .01em;
}

.brand-text p {
  margin: 1px 0 0;
  color: var(--muted);
  font-size: 12.5px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.mode-switch {
  display: inline-flex;
  padding: 3px;
  gap: 3px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.mode-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color .15s, color .15s;
}

.mode-btn:hover { color: var(--text); }

.mode-btn.is-active {
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}

.icon-btn {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
  transition: border-color .15s, background-color .15s;
}

.icon-btn:hover { border-color: var(--border-strong); }

.icon-btn-sm {
  width: 28px;
  height: 28px;
  font-size: 14px;
  border-radius: 8px;
}

.btn-xs {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 8px;
}

/* ---------- グリッドの最大画面表示 ---------- */

.grid-only-max { display: none; }

body.is-maximized { overflow: hidden; }

.grid-panel.is-maximized {
  position: fixed;
  inset: 0;
  z-index: 60;
  border-radius: 0;
  border: 0;
  box-shadow: none;
}

.grid-panel.is-maximized .grid-scroll {
  height: auto;
  flex: 1 1 auto;
  min-height: 0;
}

.grid-panel.is-maximized .grid-only-max { display: inline-flex; }

/* ---------- ツールバー ---------- */

.toolbar {
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.btn {
  font: inherit;
  font-size: 13px;
  padding: 7px 14px;
  border-radius: 9px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .15s, border-color .15s, color .15s;
}

.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
  min-width: 88px;
}

.btn-primary:hover { color: var(--accent-fg); filter: brightness(1.07); }

.btn-accent {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.control-label {
  color: var(--muted);
  font-size: 12.5px;
}

.speed {
  display: flex;
  align-items: center;
  gap: 9px;
}

.speed input[type="range"] {
  width: 150px;
  accent-color: var(--accent);
}

.speed output {
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--text);
  min-width: 84px;
  font-variant-numeric: tabular-nums;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
}

.check input { accent-color: var(--accent); }

.jump {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.jump input[type="number"] {
  font: inherit;
  font-family: var(--mono);
  font-size: 13px;
  width: 118px;
  padding: 6px 9px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
}

.jump input[type="number"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.hint {
  margin: 0;
  color: var(--faint);
  font-size: 12px;
}

/* ---------- レイアウト ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 14px;
  align-items: stretch;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
}

.panel-head h2 {
  font-size: 13px;
  letter-spacing: .04em;
  color: var(--muted);
  text-transform: none;
}

.head-tools {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.tag {
  font-size: 11.5px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

/* ---------- 凡例 ---------- */

.legend {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.lg {
  font-size: 11.5px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.lg::before {
  content: "";
  width: 11px;
  height: 11px;
  border-radius: 3px;
  border: 1px solid;
}

.lg-prime::before { background: var(--prime-bg); border-color: var(--prime-bd); }
.lg-comp::before { background: var(--comp-bg); border-color: var(--comp-bd); }
.lg-cand::before { background: var(--cand-bg); border-color: var(--cand-bd); border-style: dashed; }
.lg-unit::before { background: transparent; border-style: dashed; border-color: var(--unit-bd); }

/* ---------- グリッド ---------- */

.grid-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.grid-scroll {
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  height: clamp(300px, 56vh, 660px);
  padding: 12px 14px 0;
  scrollbar-width: thin;
}

.grid-layer {
  position: relative;
  height: 0;
}

.grid-hint {
  margin: 0;
  padding: 8px 14px;
  border-top: 1px solid var(--border);
  color: var(--faint);
  font-size: 11.5px;
}

.cell {
  position: absolute;
  top: 0;
  left: 0;
  width: 72px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  border: 1px solid var(--cand-bd);
  background: var(--cand-bg);
  color: var(--cand-fg);
  font-family: var(--mono);
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  user-select: none;
  transition: background-color .22s ease, border-color .22s ease, color .22s ease, opacity .22s ease;
}

.cell.is-empty {
  background: transparent;
  border-style: dashed;
  border-color: transparent;
  cursor: default;
}

/* ふるいモードの候補（まだ判定していない数） */
.cell.is-candidate {
  border-style: dashed;
  border-color: var(--cand-bd);
}

.cell.is-prime {
  background: var(--prime-bg);
  border-color: var(--prime-bd);
  color: var(--prime-fg);
  font-weight: 600;
}

.cell.is-composite {
  background: var(--comp-bg);
  border-color: var(--comp-bd);
  color: var(--comp-fg);
  opacity: .68;
}

.cell.is-composite::after {
  content: "×";
  position: absolute;
  top: -3px;
  right: 3px;
  font-size: 12px;
  line-height: 1;
  color: var(--comp-fg);
  opacity: .9;
  pointer-events: none;
}

.cell.is-unit {
  background: var(--unit-bg);
  border-style: dashed;
  border-color: var(--unit-bd);
  color: var(--unit-fg);
}

.cell.is-current {
  border-color: var(--cur-bd);
  color: var(--cur-fg);
  background: var(--cur-bg);
  opacity: 1;
  z-index: 2;
  animation: pulse 1.1s ease-in-out infinite;
}

.cell.is-current.is-composite::after { color: var(--cur-fg); }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, .45); }
  50% { box-shadow: 0 0 0 5px rgba(239, 68, 68, 0); }
}

/* ---------- 判定プロセス ---------- */

.process-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: clamp(360px, 66vh, 720px);
}

.process-body {
  position: relative;
  padding: 12px 14px 14px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.p-summary {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.p-line {
  display: flex;
  gap: 10px;
  align-items: baseline;
  font-size: 12.5px;
}

.p-line span {
  color: var(--muted);
  flex: none;
  min-width: 92px;
}

.p-line b {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.p-note {
  margin: 2px 0 0;
  font-size: 11.5px;
  color: var(--faint);
}

.steps {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.step {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) 62px 18px;
  align-items: center;
  gap: 7px;
  padding: 4px 7px;
  border-radius: 7px;
  border: 1px solid transparent;
  font-family: var(--mono);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.step .d {
  text-align: right;
  color: var(--faint);
}

.step .eq {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.step .rem { text-align: right; }

.step .mark {
  text-align: center;
  font-weight: 700;
}

.step.is-hit {
  background: var(--cur-bg);
  border-color: var(--cur-bd);
  color: var(--cur-fg);
}

.step.is-hit .d,
.step.is-hit .rem { color: var(--cur-fg); }

.step.is-done .mark { color: var(--comp-fg); }

.step.is-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.step.is-active .d,
.step.is-active .rem { color: var(--accent); }

.step.is-future { opacity: .35; }

.p-verdict {
  margin-top: 12px;
  padding: 9px 12px;
  border-radius: 9px;
  font-size: 12.5px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--muted);
}

.p-verdict.is-prime {
  background: var(--prime-bg);
  border-color: var(--prime-bd);
  color: var(--prime-fg);
}

.p-verdict.is-composite {
  background: var(--comp-bg);
  border-color: var(--comp-bd);
  color: var(--comp-fg);
}

.p-verdict.is-unit {
  border-style: dashed;
  border-color: var(--unit-bd);
}

.p-inspect-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.p-inspect-head button {
  font: inherit;
  font-size: 11.5px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--muted);
  border-radius: 7px;
  padding: 3px 9px;
  cursor: pointer;
}

.p-inspect-head button:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- 統計 ---------- */

.stats-panel {
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  flex-wrap: wrap;
  align-items: stretch;
}

.stat {
  flex: 1 1 130px;
  min-width: 130px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
}

.stat-wide {
  flex: 3 1 280px;
}

.stat-label {
  font-size: 11.5px;
  color: var(--muted);
}

.stat-value {
  font-family: var(--mono);
  font-size: 18px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.gap-bars {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 34px;
  margin-top: 4px;
}

.gap-bars i {
  flex: 1;
  min-width: 2px;
  background: var(--accent);
  border-radius: 2px 2px 0 0;
  opacity: .75;
}

/* ---------- テストバナー ---------- */

.test-banner {
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel);
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.7;
  white-space: pre-wrap;
}

.test-banner.is-pass { border-color: #16a34a; color: #15803d; }
.test-banner.is-fail { border-color: #dc2626; color: #b91c1c; }

[data-theme="dark"] .test-banner.is-pass { color: #4ade80; border-color: #166534; }
[data-theme="dark"] .test-banner.is-fail { color: #fca5a5; border-color: #7f1d1d; }

/* ---------- レスポンシブ ---------- */

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .process-panel { max-height: 420px; }
  .grid-scroll { height: clamp(260px, 44vh, 420px); }
}

@media (max-width: 620px) {
  body { padding: 12px; gap: 10px; }
  .brand-text p { display: none; }
  .speed input[type="range"] { width: 110px; }
  .stat { flex: 1 1 100px; min-width: 100px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; animation-iteration-count: 1 !important; }
}
