* {
  box-sizing: border-box;
}

:root {
  color-scheme: light;
  --background: #f5f5f7;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --ink: #18181b;
  --muted: #6b6b73;
  --line: #d2d2d7;
  --cell-line: #d1d1d6;
  --board-line: #18181b;
  --danger: #c62828;
  --danger-bg: #ffd9d9;
  --success: #15803d;
  --accent: #18181b;
  --accent-contrast: #ffffff;
  --star-color: #111114;
  --x-color: #8e8e93;
  --overlay: rgba(0, 0, 0, 0.28);
  --custom-fill: #dbeafe;
  --custom-fill-strong: #93c5fd;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --background: #111113;
  --surface: #1c1c1e;
  --surface-raised: #242426;
  --ink: #f5f5f7;
  --muted: #a7a7ad;
  --line: #3b3b40;
  --cell-line: #38383d;
  --board-line: #f1f1f3;
  --danger: #ff6b6b;
  --danger-bg: #4a2022;
  --success: #4cd47a;
  --accent: #f5f5f7;
  --accent-contrast: #111113;
  --overlay: rgba(0, 0, 0, 0.58);
  --custom-fill: #17335d;
  --custom-fill-strong: #285a96;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--background);
}

body {
  font-family:
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  color: var(--ink);
  display: flex;
  justify-content: center;
  padding:
    max(18px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(36px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  transition: background 160ms ease, color 160ms ease;
}

body.modal-open {
  overflow: hidden;
}

button,
select,
input {
  font: inherit;
}

.game {
  width: 100%;
  max-width: 560px;
  text-align: center;
}

.game-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.header-copy {
  flex: 1;
  padding-left: 42px;
}

.game-header h1 {
  margin: 0;
  font-size: clamp(1.8rem, 7vw, 2.3rem);
  line-height: 1.05;
}

.game-header p {
  margin: 6px 0 18px;
  color: var(--muted);
  font-size: 0.95rem;
}

.icon-button {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  font-size: 1.25rem;
  font-weight: 700;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.setup-card {
  margin-bottom: 16px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--surface);
  text-align: left;
}

.field-label {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.generator-row {
  display: flex;
  gap: 9px;
}

.generator-row select,
.select-setting select {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--surface-raised);
  color: var(--ink);
}

.generator-row select {
  flex: 1;
  padding: 11px 12px;
}

.primary-button,
.controls button {
  border: 0;
  border-radius: 11px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.primary-button {
  padding: 11px 16px;
  white-space: nowrap;
}

.board {
  width: min(94vw, 520px);
  aspect-ratio: 1;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(10, 1fr);
  background: var(--surface);
  border: 4px solid var(--board-line);
  border-radius: 3px;
  overflow: hidden;
}

.cell {
  appearance: none;
  -webkit-appearance: none;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--surface);
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(17px, 5vw, 30px);
  font-weight: 750;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.cell.star {
  color: var(--star-color);
}

.cell.x {
  color: var(--x-color);
  font-weight: 550;
}

.cell.user-x {
  opacity: 0.95;
}

.cell.auto-x {
  opacity: 0.95;
  font-weight: 550;
}

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

.status {
  min-height: 52px;
  margin-top: 10px;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 650;
  line-height: 1.35;
}

.status.solved {
  color: var(--success);
}

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

.controls {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 9px;
}

.controls button {
  padding: 12px 8px;
}

button:disabled,
select:disabled {
  opacity: 0.45;
  cursor: default;
}

button:active:not(:disabled) {
  transform: scale(0.985);
}

.instructions {
  margin-top: 14px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.instructions span {
  font-size: 0.76rem;
}

.install-hint {
  margin: 15px auto 0;
  padding: 10px 12px;
  border-radius: 11px;
  background: color-mix(in srgb, var(--surface) 78%, var(--line));
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}


.custom-region-tools {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.custom-region-tools.hidden {
  display: none;
}

.custom-region-copy {
  min-width: 0;
}

.custom-region-copy strong {
  display: block;
  margin-bottom: 2px;
  font-size: 0.88rem;
}

.custom-region-copy span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.3;
}

.custom-region-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 6px;
}

.custom-region-actions button {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 7px 10px;
  background: var(--surface-raised);
  color: var(--ink);
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
}

.board.custom-editor {
  touch-action: none;
}

.board.custom-editor .cell {
  background: var(--surface);
  touch-action: none;
}

.board.custom-editor .cell.custom-selected {
  background: var(--custom-fill);
}

.board.custom-editor .cell.custom-selected.custom-edge {
  background: var(--custom-fill-strong);
}

/* SETTINGS */

.settings-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding:
    24px
    max(10px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(10px, env(safe-area-inset-left));
  background: var(--overlay);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.settings-backdrop.hidden {
  display: none;
}

.settings-sheet {
  width: min(100%, 560px);
  max-height: min(86vh, 720px);
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.22);
  text-align: left;
}

.settings-header {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.settings-header h2 {
  margin: 0;
  font-size: 1.25rem;
}

.close-button {
  border: 0;
  background: transparent;
  color: var(--ink);
  font-weight: 750;
  cursor: pointer;
}

.settings-section {
  padding: 13px 18px 4px;
}

.settings-section + .settings-section {
  border-top: 8px solid var(--background);
}

.settings-section-title {
  padding: 2px 0 5px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.055em;
  text-transform: uppercase;
}

.setting-row,
.select-setting,
.color-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 62px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.setting-row:last-child,
.select-setting:last-child,
.color-setting:last-child {
  border-bottom: 0;
}

.setting-row strong,
.select-setting strong,
.color-setting strong {
  display: block;
  margin-bottom: 2px;
  font-size: 0.9rem;
}

.setting-row small,
.select-setting small,
.color-setting small {
  display: block;
  max-width: 330px;
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.3;
}

.setting-row input[type="checkbox"] {
  width: 22px;
  height: 22px;
  flex: 0 0 auto;
  accent-color: var(--ink);
}

.select-setting select {
  width: 125px;
  flex: 0 0 125px;
  padding: 9px 8px;
}

.color-setting input[type="color"] {
  width: 46px;
  height: 34px;
  padding: 2px;
  flex: 0 0 46px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-raised);
}

@media (display-mode: standalone) {
  .install-hint {
    display: none;
  }
}

@media (min-width: 700px) {
  .settings-backdrop {
    align-items: center;
  }
}

@media (max-width: 380px) {
  body {
    padding-left: 8px;
    padding-right: 8px;
  }

  .setup-card {
    padding: 11px;
  }

  .primary-button {
    padding-left: 12px;
    padding-right: 12px;
  }

  .header-copy {
    padding-left: 38px;
  }
}
