:root {
  --bg: #06080c;
  --panel: #0c1018;
  --text: #d8e2ec;
  --muted: #7a8a9a;
  --cyan: #2ee8ff;
  --magenta: #ff3ecf;
  --danger: #ff5a3a;
  --lime: #b8ff4a;
  --amber: #ffb44a;
  --line: #1a2434;
  --focus: #2ee8ff;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

html {
  overflow-x: clip;
  max-width: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  min-width: 0;
  max-width: 100vw;
  overflow-x: clip;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  line-height: 1.45;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.shell {
  width: 100%;
  max-width: min(520px, 100%);
  margin: 0 auto;
  padding: calc(0.75rem + var(--safe-top)) 0.75rem calc(2rem + var(--safe-bottom));
  min-width: 0;
}

.masthead {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
  min-width: 0;
}

.back {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  min-width: 0;
}

.back:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.brand {
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  color: var(--muted);
  min-width: 0;
  overflow-wrap: anywhere;
}

.brand strong {
  display: block;
  color: var(--cyan);
  font-size: 0.9rem;
  letter-spacing: 0.04em;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  min-width: 0;
}

.ghost-button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.68rem;
  padding: 0.35rem 0.5rem;
  border-radius: 4px;
  cursor: pointer;
  min-width: 0;
  white-space: normal;
}

.ghost-button:focus-visible,
.btn-primary:focus-visible,
.btn-ghost:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.ghost-button[aria-pressed="false"] {
  opacity: 0.65;
}

.btn-help {
  color: var(--cyan);
  border-color: #2a4a58;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.4rem 0.65rem;
}

.hero {
  margin-bottom: 0.75rem;
  min-width: 0;
}

.kicker {
  font-size: 0.68rem;
  color: var(--magenta);
  letter-spacing: 0.12em;
  margin: 0 0 0.25rem;
}

.hero h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
}

.hero h1 span {
  display: block;
  font-size: 0.95rem;
  color: var(--muted);
  font-weight: 500;
  margin-top: 0.15rem;
}

.catch {
  margin: 0.35rem 0 0;
  font-size: 0.88rem;
  color: var(--text);
}

.console {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.65rem;
  overflow-x: clip;
}

.console-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 0.35rem;
  min-width: 0;
}

.play-intro {
  margin-bottom: 0.65rem;
  padding: 0.55rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #080b10;
  min-width: 0;
}

.play-intro-steps {
  list-style: none;
  margin: 0 0 0.5rem;
  padding: 0;
  display: grid;
  gap: 0.4rem;
  font-size: 0.82rem;
}

.play-intro-steps li {
  display: grid;
  grid-template-columns: 1.6rem 1fr;
  gap: 0.45rem;
  align-items: start;
  min-width: 0;
}

.step-badge {
  display: grid;
  place-items: center;
  width: 1.45rem;
  height: 1.45rem;
  border: 1px solid var(--cyan);
  color: var(--cyan);
  font-size: 0.72rem;
  font-weight: 700;
  border-radius: 4px;
}

.step-body {
  min-width: 0;
  line-height: 1.45;
}

.legend-shape {
  display: inline-block;
  width: 0.75rem;
  height: 0.75rem;
  margin: 0 0.12rem;
  vertical-align: -0.1em;
  flex-shrink: 0;
}

.legend-runner {
  border-radius: 50%;
  background: #f0f6fc;
  border: 1px solid #9ab0c0;
}

.legend-node-a {
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 6px color-mix(in srgb, var(--cyan) 55%, transparent);
}

.legend-node-b {
  border-radius: 50%;
  background: var(--magenta);
  box-shadow: 0 0 6px color-mix(in srgb, var(--magenta) 45%, transparent);
}

.legend-sweep {
  width: 0;
  height: 0;
  border-left: 0.4rem solid transparent;
  border-right: 0.4rem solid transparent;
  border-bottom: 0.72rem solid var(--danger);
  background: transparent;
  opacity: 0.9;
}

.legend-data {
  border-radius: 50%;
  background: color-mix(in srgb, var(--lime) 35%, transparent);
  border: 2px solid var(--lime);
}

.play-legend {
  list-style: none;
  margin: 0 0 0.5rem;
  padding: 0;
  display: grid;
  gap: 0.25rem;
  font-size: 0.74rem;
  color: var(--muted);
}

.play-legend li {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  min-width: 0;
}

.btn-primary {
  width: 100%;
  max-width: 100%;
  margin-top: 0.35rem;
  padding: 0.65rem;
  border: none;
  border-radius: 6px;
  background: linear-gradient(135deg, #163040, #0e2838);
  color: var(--cyan);
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  border: 1px solid #1e4058;
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.35rem;
  margin-bottom: 0.5rem;
  font-size: 0.68rem;
  min-width: 0;
}

.hud div {
  background: #080b10;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.3rem 0.35rem;
  min-width: 0;
  overflow-wrap: anywhere;
}

.hud span {
  display: block;
  color: var(--muted);
  font-size: 0.62rem;
  letter-spacing: 0.04em;
}

.hud strong {
  font-size: 0.82rem;
  color: var(--lime);
}

.rule-hint {
  font-size: 0.78rem;
  color: var(--cyan);
  margin: 0 0 0.45rem;
  text-align: center;
  min-width: 0;
}

.canvas-wrap {
  position: relative;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  aspect-ratio: 390 / 420;
  max-height: min(62vh, 520px);
  background: #050608;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid #1a2434;
}

#city-canvas {
  display: block;
  width: 100% !important;
  max-width: 100%;
  height: 100% !important;
  touch-action: none;
  cursor: crosshair;
}

#city-canvas:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
}

.canvas-hint {
  position: absolute;
  left: 50%;
  top: 0.5rem;
  transform: translateX(-50%);
  margin: 0;
  padding: 0.35rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--cyan);
  background: rgba(5, 8, 12, 0.88);
  border: 1px solid #2a4a58;
  border-radius: 4px;
  pointer-events: none;
  max-width: calc(100% - 1rem);
  text-align: center;
  overflow-wrap: anywhere;
}

.operator-panel {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-top: 0.45rem;
  font-size: 0.72rem;
  color: var(--muted);
  min-width: 0;
}

.operator-panel img {
  width: 36px;
  height: 36px;
  opacity: 0.88;
  filter: grayscale(1);
  flex-shrink: 0;
}

.operator-msg {
  flex: 1;
  min-width: 0;
  margin: 0;
  padding: 0.35rem 0.5rem;
  border-left: 2px solid var(--line);
  overflow-wrap: anywhere;
}

.inspector-panel {
  margin-top: 0.5rem;
  font-size: 0.72rem;
  min-width: 0;
}

.inspector-panel summary {
  cursor: pointer;
  color: var(--muted);
}

.inspector-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 0.2rem 0.75rem;
  margin-top: 0.35rem;
  min-width: 0;
}

.summary-panel {
  margin-top: 0.65rem;
  padding: 0.75rem;
  border: 1px solid var(--lime);
  border-radius: 6px;
  background: #081008;
  min-width: 0;
}

.summary-panel[hidden] {
  display: none !important;
}

.summary-panel h2 {
  margin: 0 0 0.35rem;
  font-size: 1.1rem;
  color: var(--lime);
}

.cta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.65rem;
}

.btn-ghost {
  flex: 1;
  min-width: 0;
  padding: 0.55rem;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
}

.records {
  font-size: 0.72rem;
  color: var(--muted);
  margin: 0.5rem 0 0;
  min-width: 0;
}

.help-dialog {
  width: min(480px, calc(100% - 16px - var(--safe-top) - var(--safe-bottom)));
  max-height: calc(100dvh - 16px - var(--safe-top) - var(--safe-bottom));
  padding: 0;
  border: 1px solid #2a4a58;
  color: var(--text);
  background: rgba(6, 10, 14, 0.98);
  box-shadow: 0 0 48px rgba(46, 232, 255, 0.12);
  overflow: auto;
}

.help-dialog::backdrop {
  background: rgba(0, 4, 8, 0.82);
  backdrop-filter: blur(6px);
}

.help-dialog form {
  padding: 0.75rem 0.85rem calc(0.85rem + var(--safe-bottom));
}

.help-dialog h2 {
  margin: 0 0 0.5rem;
  font-size: 1.15rem;
  color: var(--cyan);
}

.play-intro-steps--dialog {
  font-size: 0.8rem;
}

.capture-hide {
  display: block;
}

html[data-capture="1"] .canvas-wrap {
  aspect-ratio: 9 / 14;
  max-height: 72vh;
}

html[data-capture="1"] .capture-hide {
  display: none !important;
}

html[data-capture="1"] .capture-safe {
  display: block !important;
}

html[data-capture="1"] .console-toolbar {
  display: none !important;
}

/* プレイ中：モバイルでヘッダー・ヒーローを隠し、盤面を viewport 内へ */
.shell.is-playing .hero {
  display: none;
}

.shell.is-playing .play-intro {
  display: none;
}

@media (max-width: 520px) {
  .shell.is-playing .masthead .brand,
  .shell.is-playing .masthead .header-actions {
    display: none;
  }

  .shell.is-playing .masthead {
    margin-bottom: 0.25rem;
  }

  .shell.is-playing {
    padding-top: calc(0.35rem + var(--safe-top));
    padding-left: max(0.5rem, env(safe-area-inset-left));
    padding-right: max(0.5rem, env(safe-area-inset-right));
    padding-bottom: calc(0.75rem + var(--safe-bottom));
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
  }

  .shell.is-playing .console {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .shell.is-playing .canvas-wrap {
    flex: 1 1 auto;
    min-height: 132px;
    max-height: min(48dvh, 400px);
    aspect-ratio: auto;
  }

  .shell.is-playing .operator-panel,
  .shell.is-playing .inspector-panel,
  .shell.is-playing .records {
    flex-shrink: 0;
  }
}

/* 開始前：360×640 でも開始ボタンが viewport 内に収まるよう圧縮 */
@media (max-height: 700px) {
  .shell:not(.is-playing) .hero .kicker {
    display: none;
  }

  .shell:not(.is-playing) .masthead .brand {
    display: none;
  }

  .play-intro {
    padding: 0.45rem 0.5rem;
  }

  .play-intro-steps {
    font-size: 0.76rem;
    gap: 0.3rem;
  }

  .play-legend {
    font-size: 0.7rem;
    gap: 0.18rem;
  }

  .hero h1 {
    font-size: 1.15rem;
  }

  .hero {
    margin-bottom: 0.4rem;
  }

  .shell {
    padding-bottom: 1rem;
  }
}

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

@media (min-width: 520px) {
  .hero h1 {
    font-size: 1.5rem;
  }
}