:root {
  --bg-a: #fff8d9;
  --bg-b: #c9f0dc;
  --bg-c: #a7d9ff;
  --panel: rgba(255, 255, 255, 0.88);
  --ink: #203040;
  --muted: #596574;
  --accent: #1e88a8;
  --accent-strong: #116b82;
  --danger: #e33838;
  --track: #fffef6;
  --track-edge: #5b6f86;
  --goal: #31a86b;
  --shadow: 0 18px 44px rgba(39, 58, 82, 0.16);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
  background:
    radial-gradient(circle at 14% 18%, rgba(255, 255, 255, 0.72) 0 9%, transparent 10%),
    radial-gradient(circle at 84% 12%, rgba(255, 255, 255, 0.58) 0 7%, transparent 8%),
    linear-gradient(135deg, var(--bg-a), var(--bg-b) 52%, var(--bg-c));
}

body.theme-normal {
  --bg-a: #e9f4ff;
  --bg-b: #dcead3;
  --bg-c: #f7d28d;
  --accent: #2f76b8;
  --accent-strong: #225b91;
}

body.theme-hard {
  --bg-a: #f6b2a4;
  --bg-b: #6f7ab8;
  --bg-c: #25345f;
  --panel: rgba(255, 255, 255, 0.9);
  --accent: #d04f4f;
  --accent-strong: #a9363c;
}

body.theme-extreme {
  --bg-a: #f29b7f;
  --bg-b: #935b93;
  --bg-c: #182643;
  --panel: rgba(255, 255, 255, 0.9);
  --accent: #bd3e58;
  --accent-strong: #8e2d43;
}

body.theme-oni {
  --bg-a: #df6c62;
  --bg-b: #543c78;
  --bg-c: #15192f;
  --panel: rgba(255, 255, 255, 0.91);
  --accent: #9d2438;
  --accent-strong: #6f1727;
}

body.theme-onigashima {
  --bg-a: #c84c4f;
  --bg-b: #402c65;
  --bg-c: #101426;
  --panel: rgba(255, 255, 255, 0.92);
  --accent: #7d1b2c;
  --accent-strong: #58111e;
}

button {
  border: 0;
  font: inherit;
  cursor: pointer;
}

.app-shell {
  width: min(1180px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 22px 0 28px;
}

.top-panel,
.game-area {
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.top-panel {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 14px;
  align-items: center;
  padding: 20px;
  border: 2px solid rgba(255, 255, 255, 0.62);
  border-radius: 8px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 700;
}

h1 {
  margin: 0;
  font-size: clamp(1.7rem, 3vw, 2.8rem);
  line-height: 1.04;
  letter-spacing: 0;
  white-space: nowrap;
}

.control-grid {
  display: grid;
  grid-template-columns: minmax(0, 2.2fr) minmax(130px, 0.8fr);
  gap: 12px;
  align-items: end;
}

fieldset {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

legend {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 4px;
  padding: 4px;
  border: 1px solid rgba(32, 48, 64, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.64);
}

#courseButtons {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.segmented button {
  min-height: 40px;
  padding: 8px 6px;
  border-radius: 6px;
  color: var(--ink);
  background: transparent;
  font-size: clamp(0.72rem, 1.15vw, 0.95rem);
  font-weight: 800;
  white-space: nowrap;
}

.segmented button.is-active {
  color: #fff;
  background: var(--accent);
}

.game-area {
  margin-top: 16px;
  padding: 16px;
  border-radius: 8px;
}

.status-bar,
.hint-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
}

.status-label {
  display: block;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

#statusText {
  font-size: 1.22rem;
}

.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.primary-button,
.secondary-button {
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 900;
}

.primary-button {
  color: #fff;
  background: var(--accent);
}

.primary-button:hover,
.primary-button:focus-visible {
  background: var(--accent-strong);
}

.secondary-button {
  color: var(--ink);
  background: rgba(255, 255, 255, 0.76);
  border: 1px solid rgba(32, 48, 64, 0.16);
}

.canvas-wrap {
  position: relative;
  margin-top: 14px;
  overflow: hidden;
  border: 3px solid rgba(32, 48, 64, 0.2);
  border-radius: 8px;
  background: #314055;
  touch-action: none;
}

canvas {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
}

.message-layer {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  pointer-events: none;
  opacity: 0;
  transform: scale(0.98);
  transition: opacity 160ms ease, transform 160ms ease;
}

.message-layer.is-visible {
  opacity: 1;
  transform: scale(1);
}

.message-box {
  width: min(440px, 92%);
  padding: 18px;
  border: 2px solid rgba(255, 255, 255, 0.72);
  border-radius: 8px;
  color: var(--ink);
  text-align: center;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 16px 38px rgba(0, 0, 0, 0.18);
}

.message-box p {
  margin: 0 0 6px;
  font-size: 1.7rem;
  font-weight: 900;
}

.message-box span {
  color: var(--muted);
  font-weight: 700;
}

.hint-row {
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 700;
}

#courseInfo {
  color: var(--ink);
}

@media (max-width: 860px) {
  .status-bar,
  .hint-row {
    align-items: flex-start;
    flex-direction: column;
  }

  .button-row {
    width: 100%;
  }

  .primary-button,
  .secondary-button {
    flex: 1;
  }
}

@media (max-width: 700px) {
  .top-panel {
    grid-template-columns: 1fr;
  }

  .control-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 560px) {
  .app-shell {
    width: min(100vw - 16px, 1180px);
    padding-top: 8px;
  }

  .top-panel,
  .game-area {
    padding: 12px;
  }

  .segmented button {
    min-height: 46px;
    padding: 8px 6px;
    font-size: 0.9rem;
  }

  .message-box p {
    font-size: 1.35rem;
  }
}

/* LiNest v3: 難易度別のコース色とCanvas座標系は保持 */
body.linest-themed {
  --ink: var(--linest-text);
  --muted: var(--linest-muted);
  --panel: rgba(255, 255, 255, 0.96);
  --accent: var(--linest-green);
  --accent-strong: var(--linest-green-dark);
  --shadow: var(--linest-shadow);
}

body.linest-themed .app-shell,
body.linest-themed .top-panel > *,
body.linest-themed .control-grid > *,
body.linest-themed .status-bar > * {
  min-width: 0;
}

body.linest-themed .top-panel,
body.linest-themed .game-area {
  border: 1px solid var(--linest-border);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--linest-shadow);
}

body.linest-themed .top-panel {
  border-left: 6px solid var(--linest-green-bright);
}

body.linest-themed .segmented {
  border-color: var(--linest-border);
  background: var(--linest-green-soft);
}

body.linest-themed .segmented button {
  min-width: 0;
  color: var(--linest-text);
  line-height: 1.35;
  white-space: normal;
}

body.linest-themed .segmented button.is-active {
  color: #fff;
  background: var(--linest-green);
}

body.linest-themed .canvas-wrap {
  border-color: rgba(74, 55, 40, 0.28);
  box-shadow: 0 10px 24px rgba(74, 55, 40, 0.14);
}

body.linest-themed .message-box {
  border-color: var(--linest-border);
  color: var(--linest-text);
  box-shadow: var(--linest-shadow);
}

body.linest-themed :where(.primary-button, .secondary-button) {
  min-width: 0;
  line-height: 1.35;
  white-space: normal;
}

@media (max-width: 560px) {
  body.linest-themed #difficultyButtons {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  body.linest-themed .segmented button,
  body.linest-themed :where(.primary-button, .secondary-button) {
    padding-inline: 6px;
    font-size: 0.88rem;
  }

  body.linest-themed .button-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  body.linest-themed #newCourseButton {
    grid-column: 1 / -1;
  }
}
