:root {
  color-scheme: light;
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-muted: #edf1f5;
  --text: #1d2733;
  --muted: #667383;
  --line: #c9d2dd;
  --line-strong: #263241;
  --accent: #7657c8;
  --accent-strong: #573b9e;
  --accent-soft: #ece7ff;
  --accent-faint: #f6f3ff;
  --match: #7b4aa1;
  --match-soft: #f3e8ff;
  --single: #247345;
  --single-soft: #d9f8e5;
  --single-flash: #8ff0ae;
  --danger: #c93434;
  --danger-soft: #fde8e8;
  --shadow: 0 24px 60px rgba(29, 39, 51, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(135deg, rgba(118, 87, 200, 0.1), transparent 42%),
    var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button {
  font: inherit;
}

.app-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 72px;
  padding: 14px clamp(18px, 4vw, 48px);
  border-bottom: 1px solid rgba(201, 210, 221, 0.7);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(18px);
}

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

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 2px solid var(--line-strong);
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 1.2rem;
  font-weight: 800;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.1rem, 2.5vw, 1.45rem);
  line-height: 1;
  letter-spacing: 0;
  white-space: nowrap;
}

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

.menu button {
  min-width: 74px;
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-weight: 650;
}

.menu button:hover,
.menu button:focus-visible {
  border-color: var(--line);
  background: var(--surface-muted);
  outline: none;
}

.menu button:last-child:hover,
.menu button:last-child:focus-visible {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.workspace {
  display: grid;
  min-height: calc(100vh - 72px);
  place-items: center;
  padding: clamp(20px, 5vw, 56px);
}

.puzzle-panel {
  width: min(100%, 820px);
  padding: clamp(18px, 4vw, 32px);
  border: 1px solid rgba(201, 210, 221, 0.8);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 22px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent-strong);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.panel-header h2 {
  margin: 0;
  font-size: clamp(1.35rem, 4vw, 2rem);
  line-height: 1.05;
  letter-spacing: 0;
}

.status-pill {
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  min-height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

.panel-actions {
  position: relative;
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 8px;
}

.help-widget {
  position: relative;
}

.help-button {
  display: grid;
  width: 32px;
  height: 32px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #ffffff;
  color: var(--accent-strong);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 900;
  line-height: 1;
}

.help-button:hover,
.help-button:focus-visible,
.help-widget.is-open .help-button {
  border-color: var(--accent);
  background: var(--accent-soft);
  outline: none;
}

.help-popover {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: 12;
  width: min(82vw, 360px);
  max-height: min(520px, calc(100vh - 170px));
  overflow: auto;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  transform: translateY(-4px);
  transition:
    opacity 150ms ease,
    transform 150ms ease,
    visibility 150ms ease;
  visibility: hidden;
}

.help-widget:hover .help-popover,
.help-widget:focus-within .help-popover,
.help-widget.is-open .help-popover {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
  visibility: visible;
}

.help-popover h3,
.help-popover h4,
.help-popover p,
.help-popover ul {
  margin: 0;
}

.help-popover h3 {
  margin-bottom: 10px;
  font-size: 1rem;
  line-height: 1.15;
}

.help-popover h4 {
  margin-top: 14px;
  margin-bottom: 6px;
  color: var(--accent-strong);
  font-size: 0.78rem;
  letter-spacing: 0;
  line-height: 1.2;
}

.help-popover p,
.help-popover li {
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.4;
}

.help-popover ul {
  display: grid;
  gap: 5px;
  padding-left: 18px;
}

.game-area {
  display: grid;
  align-items: start;
  grid-template-columns: minmax(0, 560px) minmax(112px, 132px);
  gap: clamp(16px, 3vw, 28px);
  justify-content: center;
}

.sudoku-board {
  display: grid;
  width: min(100%, 560px);
  aspect-ratio: 1;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  margin-inline: auto;
  border: 3px solid var(--line-strong);
  background: var(--line-strong);
}

.cell {
  position: relative;
  display: grid;
  width: 100%;
  min-width: 0;
  aspect-ratio: 1;
  place-items: center;
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--text);
  cursor: pointer;
  font-size: clamp(1rem, 5vw, 2rem);
  font-weight: 750;
  line-height: 1;
}

.known-value {
  position: relative;
  z-index: 1;
}

.notes-grid {
  position: absolute;
  inset: 7%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  color: var(--muted);
  font-size: clamp(0.42rem, 1.45vw, 0.78rem);
  font-weight: 800;
  line-height: 1;
  pointer-events: none;
}

.note {
  display: grid;
  place-items: center;
}

.cell.has-value .notes-grid {
  display: none;
}

.cell.is-connected {
  background: var(--accent-faint);
}

.cell.has-match {
  background: var(--match-soft);
  color: var(--match);
}

.note.is-note-match {
  border-radius: 4px;
  background: var(--match-soft);
  color: var(--match);
}

.cell.has-conflict {
  background: var(--danger-soft);
  color: var(--danger);
}

.cell.has-single-option {
  background: var(--single-soft);
  color: var(--single);
}

.cell.is-single-option-flashing {
  animation: single-option-flash 0.5s ease-out;
}

.cell:nth-child(3n) {
  border-right-color: var(--line-strong);
  border-right-width: 2px;
}

.cell:nth-child(9n) {
  border-right-width: 0;
}

.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  border-bottom-color: var(--line-strong);
  border-bottom-width: 2px;
}

.cell:nth-child(n + 73) {
  border-bottom-width: 0;
}

.cell:hover,
.cell:focus-visible {
  position: relative;
  z-index: 1;
  background: var(--accent-soft);
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.cell.is-selected {
  background: #ded6ff;
  outline: 3px solid var(--accent);
  outline-offset: -3px;
}

.cell.has-conflict:hover,
.cell.has-conflict:focus-visible,
.cell.has-conflict.is-selected {
  background: var(--danger-soft);
  outline-color: var(--danger);
}

.cell.has-match:hover,
.cell.has-match:focus-visible {
  background: var(--match-soft);
  outline-color: var(--match);
}

.cell.has-single-option:hover,
.cell.has-single-option:focus-visible,
.cell.has-single-option.is-selected {
  background: var(--single-soft);
  outline-color: var(--single);
}

.cell.has-conflict.has-match,
.cell.has-conflict.has-match:hover,
.cell.has-conflict.has-match:focus-visible {
  background: var(--danger-soft);
  color: var(--danger);
  outline-color: var(--danger);
}

.cell.has-conflict.has-single-option,
.cell.has-conflict.has-single-option:hover,
.cell.has-conflict.has-single-option:focus-visible {
  background: var(--danger-soft);
  color: var(--danger);
  outline-color: var(--danger);
}

.controls-panel {
  display: grid;
  gap: 12px;
  width: 100%;
}

.mode-toggle {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-muted);
}

.mode-toggle button,
.autofill-notes,
.single-option-hints,
.number-pad button {
  cursor: pointer;
}

.mode-toggle button {
  display: grid;
  min-width: 0;
  min-height: 36px;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.mode-toggle button:hover,
.mode-toggle button:focus-visible {
  border-color: var(--line);
  background: #ffffff;
  color: var(--text);
  outline: none;
}

.mode-toggle button.is-active {
  border-color: var(--accent);
  background: #ffffff;
  color: var(--accent-strong);
}

.autofill-notes,
.single-option-hints {
  display: grid;
  min-width: 0;
  min-height: 42px;
  place-items: center;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 800;
}

.autofill-notes:hover,
.autofill-notes:focus-visible,
.single-option-hints:hover,
.single-option-hints:focus-visible {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
  color: #ffffff;
  outline: none;
}

.single-option-hints {
  border-color: var(--single);
  background: var(--single-soft);
  color: var(--single);
}

@keyframes single-option-flash {
  0%,
  100% {
    background: var(--single-soft);
  }

  35% {
    background: var(--single-flash);
    box-shadow: inset 0 0 0 3px rgba(36, 115, 69, 0.55);
  }

  65% {
    background: #ffffff;
  }
}

.mode-icon {
  display: grid;
  width: 22px;
  height: 22px;
  place-items: center;
}

.known-icon::before {
  content: "";
  display: block;
  width: 15px;
  height: 15px;
  border: 2px solid currentColor;
  border-radius: 50%;
  background: currentColor;
  box-shadow: inset 0 0 0 4px #ffffff;
}

.notes-icon {
  grid-template-columns: repeat(3, 4px);
  grid-template-rows: repeat(3, 4px);
  gap: 3px;
}

.notes-icon::before {
  content: "";
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: currentColor;
  box-shadow:
    7px 0 0 currentColor,
    14px 0 0 currentColor,
    0 7px 0 currentColor,
    7px 7px 0 currentColor,
    14px 7px 0 currentColor,
    0 14px 0 currentColor,
    7px 14px 0 currentColor,
    14px 14px 0 currentColor;
}

.number-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  width: 100%;
}

.number-pad button {
  display: grid;
  min-width: 0;
  min-height: 42px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  font-weight: 800;
}

.number-pad button:hover,
.number-pad button:focus-visible {
  border-color: var(--accent);
  background: var(--accent-soft);
  outline: none;
}

.number-pad .delete-value {
  grid-column: 1 / -1;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 0.9rem;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(29, 39, 51, 0.36);
}

.modal-backdrop[hidden] {
  display: none;
}

.load-dialog {
  width: min(100%, 440px);
  max-height: min(620px, calc(100vh - 40px));
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.load-dialog-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--line);
}

.load-dialog-header h2 {
  margin: 0;
  font-size: 1.35rem;
  line-height: 1.1;
  letter-spacing: 0;
}

.dialog-close {
  position: relative;
  display: grid;
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--muted);
  cursor: pointer;
  font-weight: 800;
}

.dialog-close::before,
.dialog-close::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
}

.dialog-close::before {
  transform: rotate(45deg);
}

.dialog-close::after {
  transform: rotate(-45deg);
}

.dialog-close:hover,
.dialog-close:focus-visible {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-strong);
  outline: none;
}

.saved-puzzle-list {
  display: grid;
  gap: 8px;
  max-height: 440px;
  overflow: auto;
  padding: 16px 20px 20px;
}

.saved-puzzle-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.saved-puzzle-load {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  width: 100%;
  min-height: 54px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.saved-puzzle-load:hover,
.saved-puzzle-load:focus-visible {
  border-color: var(--accent);
  background: var(--accent-faint);
  outline: none;
}

.delete-saved-puzzle {
  position: relative;
  display: grid;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--muted);
  cursor: pointer;
}

.delete-saved-puzzle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.delete-saved-puzzle:hover,
.delete-saved-puzzle:focus-visible {
  border-color: var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
  outline: none;
}

.saved-puzzle-name {
  min-width: 0;
  overflow: hidden;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.saved-puzzle-date {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

@media (max-width: 760px) {
  .game-area {
    grid-template-columns: 1fr;
  }

  .controls-panel {
    max-width: 560px;
    margin-inline: auto;
  }

  .number-pad {
    grid-template-columns: repeat(5, minmax(42px, 1fr));
  }

  .number-pad .delete-value {
    grid-column: span 2;
  }
}

@media (max-width: 560px) {
  .app-bar {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
  }

  .menu {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    width: 100%;
  }

  .menu button {
    min-width: 0;
  }

  .panel-header {
    align-items: flex-start;
    flex-direction: column;
  }
}
