:root {
  color-scheme: light;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  --ink: #182026;
  --muted: #62717d;
  --paper: #f6f2e8;
  --panel: #fffdf7;
  --line: #d9d1bf;
  --path: #167a6b;
  --blocked: #bec5c2;
  --danger: #b9473f;
  --gold: #d5902f;
  --blue: #2f6fbd;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 16%, rgba(47, 111, 189, 0.14), transparent 28rem),
    linear-gradient(135deg, #f7f1df 0%, #e9f2ed 55%, #edf1f8 100%);
}

button {
  font: inherit;
}

.app-shell {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.game-panel {
  width: min(100%, 960px);
  padding: 22px;
  border: 1px solid rgba(24, 32, 38, 0.12);
  border-radius: 8px;
  background: rgba(255, 253, 247, 0.9);
  box-shadow: 0 24px 70px rgba(61, 67, 58, 0.18);
  backdrop-filter: blur(10px);
}

.top-bar {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
}

.top-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--path);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
}

h1 {
  max-width: 520px;
  margin: 0;
  font-size: clamp(1.45rem, 2.4vw, 2.2rem);
  line-height: 1.12;
  letter-spacing: 0;
}

.icon-button,
.segmented-control button {
  border: 1px solid rgba(24, 32, 38, 0.16);
  border-radius: 7px;
  color: var(--ink);
  background: #fffaf0;
  cursor: pointer;
}

.icon-button {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  font-size: 1.45rem;
  line-height: 1;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin: 22px 0 16px;
}

.status-grid div {
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbf7ed;
}

.status-grid span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.status-grid strong {
  display: block;
  margin-top: 4px;
  font-size: 1.55rem;
  line-height: 1;
}

.status-grid strong.danger {
  color: var(--danger);
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 18px;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(4, minmax(64px, 1fr));
  width: min(100%, 560px);
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eee6d6;
}

.segmented-control button {
  min-height: 34px;
  border: 0;
  background: transparent;
  font-weight: 800;
}

.segmented-control button.active {
  background: var(--panel);
  box-shadow: 0 1px 6px rgba(24, 32, 38, 0.12);
}

.toggle-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.toggle-group button {
  min-height: 34px;
  padding: 0 16px;
  border: 1.5px solid var(--line);
  border-radius: 20px;
  background: #f5f0e8;
  color: var(--muted);
  font-weight: 800;
  cursor: pointer;
  transition:
    background 120ms ease,
    border-color 120ms ease,
    color 120ms ease;
}

.toggle-group button.active {
  border-color: var(--path);
  background: var(--path);
  color: #fff;
}

.board-wrap {
  position: relative;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  gap: 6px;
  width: min(100%, 820px);
  margin: 0 auto;
  aspect-ratio: 1;
}

.cell {
  position: relative;
  display: grid;
  min-width: 0;
  place-items: center;
  border: 1px solid rgba(24, 32, 38, 0.14);
  border-radius: 8px;
  background: #f3eee2;
  color: #6c6e68;
  font-weight: 900;
  font-size: clamp(0.72rem, 2vw, 1.1rem);
  cursor: pointer;
  transition:
    transform 140ms ease,
    background 140ms ease,
    border-color 140ms ease;
}

.cell:hover {
  transform: translateY(-1px);
}

.cell.blocked {
  background:
    linear-gradient(135deg, transparent 45%, rgba(24, 32, 38, 0.13) 45% 55%, transparent 55%),
    #d4d7cf;
}

.cell.current {
  border-color: var(--gold);
  background: #ffe6ad;
  color: #4d3510;
  box-shadow: inset 0 0 0 3px rgba(213, 144, 47, 0.24);
}

.cell.visited {
  border-color: rgba(47, 111, 189, 0.44);
  background: #dceaf8;
  color: #173e6d;
}

.cell.start::before,
.cell.end::before {
  position: absolute;
  top: 4px;
  left: 4px;
  padding: 2px 4px;
  border-radius: 5px;
  color: #fff;
  background: var(--blue);
  font-size: 0.56rem;
  line-height: 1;
  content: "S";
}

.cell.end::before {
  background: var(--danger);
  content: "E";
}

.cell.locked {
  cursor: default;
}

.cell.fog {
  background: #d0cbbf;
  color: #8a857d;
}

.status-grid.has-timer {
  grid-template-columns: repeat(5, 1fr);
}

#timerDisplay.danger {
  color: var(--danger);
}

.message-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 48px;
  margin-top: 16px;
}

.message-row p {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

button:focus-visible,
.cell:focus-visible {
  outline: 3px solid rgba(47, 111, 189, 0.45);
  outline-offset: 2px;
}

[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  padding: 5px 10px;
  border-radius: 6px;
  background: #182026ee;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 140ms ease;
  z-index: 50;
}

[data-tooltip]:hover::after {
  opacity: 1;
}

.result-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(24, 32, 38, 0.55);
  backdrop-filter: blur(4px);
  z-index: 100;
}

.result-card {
  width: min(100%, 360px);
  padding: 36px 28px 28px;
  border-top: 4px solid var(--line);
  border-radius: 12px;
  text-align: center;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(24, 32, 38, 0.28);
}

.result-card.success {
  border-top-color: var(--path);
}

.result-card.fail {
  border-top-color: var(--danger);
}

.result-status {
  margin: 0 0 12px;
  font-size: 2rem;
  font-weight: 900;
  line-height: 1;
}

.result-card.success .result-status {
  color: var(--path);
}

.result-card.fail .result-status {
  color: var(--danger);
}

.result-detail {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0 0 24px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.5;
}

.result-highlight {
  color: var(--gold);
  font-size: 1rem;
  font-weight: 900;
}

.result-button {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 8px;
  color: #fff;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 800;
  cursor: pointer;
  transition: opacity 140ms ease;
}

.result-button:hover {
  opacity: 0.85;
}

.result-card.success .result-button {
  background: var(--path);
}

.result-card.fail .result-button {
  background: var(--danger);
}

#rankingStatus {
  display: block;
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  min-height: 1em;
}

.result-hint {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
  opacity: 0.65;
}

.welcome-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  overflow-y: auto;
  padding: 24px;
  background: rgba(24, 32, 38, 0.6);
  backdrop-filter: blur(4px);
  z-index: 100;
}

.welcome-card {
  width: min(100%, 500px);
  padding: 32px;
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 32px 80px rgba(24, 32, 38, 0.35);
}

.welcome-eyebrow {
  margin: 0 0 8px;
  color: var(--path);
  font-size: 0.78rem;
  font-weight: 800;
}

.welcome-title {
  margin: 0 0 24px;
  font-size: clamp(1.2rem, 3vw, 1.5rem);
  line-height: 1.2;
}

.welcome-section {
  margin-bottom: 20px;
}

.welcome-label {
  margin: 0 0 8px;
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.welcome-label-note {
  margin-left: 6px;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 600;
}

.welcome-rules {
  margin: 0;
  padding-left: 18px;
}

.welcome-rules li {
  margin-bottom: 5px;
  font-size: 0.87rem;
  line-height: 1.5;
}

.welcome-modes {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.welcome-mode-item {
  display: grid;
  grid-template-columns: 80px 1fr;
  gap: 8px;
  align-items: baseline;
  padding: 8px 12px;
  border-radius: 8px;
  background: #f3eee2;
  font-size: 0.85rem;
  line-height: 1.45;
}

.welcome-mode-item strong {
  color: var(--path);
  font-size: 0.82rem;
}

.welcome-mode-item span {
  color: var(--muted);
}

.welcome-start {
  width: 100%;
  margin-top: 8px;
  padding: 14px;
  border: none;
  border-radius: 8px;
  background: var(--path);
  color: #fff;
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  transition: background 150ms ease;
}

.welcome-start:hover {
  background: #125f54;
}

/* Danger pulse (lives = 1) */
@keyframes danger-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(185, 71, 63, 0); }
  50% { box-shadow: 0 0 0 12px rgba(185, 71, 63, 0.28); }
}

.board-wrap.danger {
  border-radius: 10px;
  animation: danger-pulse 1.1s ease-in-out infinite;
}

.board-wrap.danger .cell:not(.current):not(.visited):not(.blocked):not(.fog) {
  background: #f5ebe8;
  border-color: rgba(185, 71, 63, 0.18);
}

body.dark .board-wrap.danger .cell:not(.current):not(.visited):not(.blocked):not(.fog) {
  background: #2e2020;
  border-color: rgba(185, 71, 63, 0.22);
}

/* Ripple wave on success */
@keyframes ripple-wave {
  0% { transform: scale(1); box-shadow: none; }
  40% { transform: scale(1.1); box-shadow: 0 0 0 5px rgba(22, 122, 107, 0.35); }
  100% { transform: scale(1); box-shadow: none; }
}

.cell.ripple {
  animation: ripple-wave 0.5s ease-out both;
  animation-delay: var(--ripple-delay, 0ms);
}

/* Direction display (random mode) */
.direction-display {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 12px;
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f3eee2;
  font-size: 0.83rem;
}

.dir-label {
  font-weight: 800;
  color: var(--muted);
  white-space: nowrap;
}

.dir-map {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}

.dir-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 700;
}

.dir-sep {
  color: var(--muted);
  font-size: 0.7rem;
}

.dir-actual {
  color: var(--path);
  font-size: 1.05rem;
}

/* Dark mode */
body.dark {
  color-scheme: dark;
  background:
    radial-gradient(circle at 18% 16%, rgba(47, 111, 189, 0.1), transparent 28rem),
    linear-gradient(135deg, #1a1a1f 0%, #141c1a 55%, #15161e 100%);
}

body.dark .game-panel {
  border-color: rgba(255, 255, 255, 0.07);
  background: rgba(24, 26, 30, 0.97);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.55);
}

body.dark .eyebrow { color: #4ecdb4; }
body.dark h1 { color: #e0d8c8; }
body.dark .message-row p { color: #8090a0; }

body.dark .icon-button,
body.dark #undoButton,
body.dark .segmented-control button {
  border-color: rgba(255, 255, 255, 0.1);
  background: #272b30;
  color: #c8c0b0;
}

body.dark .status-grid div {
  border-color: rgba(255, 255, 255, 0.08);
  background: #20232a;
}

body.dark .status-grid span { color: #808898; }
body.dark .status-grid strong { color: #d8d0c0; }

body.dark .segmented-control {
  border-color: rgba(255, 255, 255, 0.09);
  background: #1e2128;
}

body.dark .segmented-control button.active {
  background: #2c3038;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.35);
  color: #e0d8c8;
}

body.dark .toggle-group button {
  border-color: rgba(255, 255, 255, 0.1);
  background: #23262e;
  color: #808898;
}

body.dark .toggle-group button.active {
  border-color: var(--path);
  background: var(--path);
  color: #fff;
}

body.dark .cell {
  border-color: rgba(255, 255, 255, 0.08);
  background: #272b30;
  color: #909298;
}

body.dark .cell:hover { transform: translateY(-1px); }

body.dark .cell.current {
  border-color: var(--gold);
  background: #3a2a10;
  color: #f0c060;
  box-shadow: inset 0 0 0 3px rgba(213, 144, 47, 0.2);
}

body.dark .cell.visited {
  border-color: rgba(47, 111, 189, 0.4);
  background: #18222e;
  color: #70a8d8;
}

body.dark .cell.blocked {
  background:
    linear-gradient(135deg, transparent 45%, rgba(255, 255, 255, 0.07) 45% 55%, transparent 55%),
    #1e2024;
}

body.dark .cell.fog {
  background: #1e2024;
  color: #383a3e;
}

body.dark .result-card {
  background: #1e2228;
  color: #d8d0c0;
}

body.dark .result-detail { color: #808898; }
body.dark .result-hint { color: #606870; }

body.dark .welcome-card {
  background: #1e2228;
  color: #d8d0c0;
}

body.dark .welcome-eyebrow { color: #4ecdb4; }
body.dark .welcome-title { color: #e0d8c8; }
body.dark .welcome-label { color: #808898; }
body.dark .welcome-rules li { color: #c8c0b0; }

body.dark .welcome-mode-item {
  background: #23262e;
}

body.dark .welcome-mode-item span { color: #808898; }

body.dark .direction-display {
  border-color: rgba(255, 255, 255, 0.09);
  background: #23262e;
}

body.dark [data-tooltip]::after {
  background: #e8e0d0ee;
  color: #182026;
}

@media (max-width: 560px) {
  .app-shell {
    padding: 12px;
  }

  .game-panel {
    padding: 16px;
  }

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

  .status-grid.has-timer {
    grid-template-columns: repeat(3, 1fr);
  }

  .welcome-card {
    padding: 24px 20px;
  }

  .welcome-mode-item {
    grid-template-columns: 1fr;
    gap: 2px;
  }

  .status-grid strong {
    font-size: 1.25rem;
  }

  .controls {
    align-items: stretch;
    flex-direction: column;
  }

  .segmented-control {
    width: 100%;
  }

  .board {
    gap: 4px;
  }

  .cell {
    border-radius: 6px;
    font-size: clamp(0.62rem, 2.8vw, 0.9rem);
  }

  .cell.start::before,
  .cell.end::before {
    top: 3px;
    left: 3px;
    padding: 1px 3px;
    font-size: 0.48rem;
  }
}
