/* ==========================================================================
   SUDOKU - by kanto
   Strict Triad Design System: Cream (#FAF7F2), Black (#121212), White (#FFFFFF)
   Fully Responsive: Adaptive Desktop Web Experience + Native Mobile Feel
   ========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@600;700;800&family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap');

:root {
  /* Strict Color Tokens */
  --color-cream-bg: #FAF7F2;
  --color-cream-peer: #F4EFE6;
  --color-cream-match: #E8DFD1;
  --color-white: #FFFFFF;
  --color-black: #121212;
  --color-charcoal: #2A2A2A;
  --color-muted: #6E6E6E;
  --color-border-grid: #121212;
  --color-border-subtle: #E2DBD0;

  /* Typography */
  --font-title: 'Cinzel', 'Playfair Display', Georgia, serif;
  --font-ui: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-grid: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, monospace;

  /* Transitions */
  --transition-fast: 0.14s ease;
  --board-radius: 12px;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  background-color: var(--color-cream-bg);
  color: var(--color-black);
  font-family: var(--font-ui);
  overflow-x: hidden;
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* App Container - Fully Adaptive */
.app-container {
  width: 100%;
  position: relative;
  background-color: var(--color-cream-bg);
}

/* Header Section */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 10px;
  border-bottom: 1.5px solid var(--color-border-grid);
}

.brand-wrapper {
  display: flex;
  flex-direction: column;
}

.brand-title {
  font-family: var(--font-title);
  font-size: clamp(24px, 4vw, 32px);
  font-weight: 800;
  letter-spacing: 0.16em;
  color: var(--color-black);
  line-height: 1;
}

.brand-subtitle {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.22em;
  text-transform: lowercase;
  color: var(--color-muted);
  margin-top: 4px;
}

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

.icon-btn {
  background: var(--color-white);
  border: 1.5px solid var(--color-border-subtle);
  border-radius: 8px;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-black);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.icon-btn:hover {
  border-color: var(--color-black);
  transform: translateY(-1px);
}

.icon-btn:active {
  transform: scale(0.94);
  background-color: var(--color-cream-peer);
}

.install-btn {
  display: none;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 8px 14px;
  background-color: var(--color-black);
  color: var(--color-white);
  border-radius: 20px;
  border: 1.5px solid var(--color-black);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.install-btn:hover {
  background-color: var(--color-charcoal);
}

/* Status & Difficulty Bar */
.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.difficulty-tabs {
  display: flex;
  background-color: var(--color-cream-peer);
  padding: 3px;
  border-radius: 8px;
  border: 1px solid var(--color-border-subtle);
}

.diff-tab {
  background: transparent;
  border: none;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--color-muted);
  cursor: pointer;
  border-radius: 6px;
  transition: all var(--transition-fast);
}

.diff-tab.active {
  background-color: var(--color-white);
  color: var(--color-black);
  font-weight: 700;
  box-shadow: 0 1px 4px rgba(18, 18, 18, 0.08);
}

/* Timer Section with 3 Hearts */
.timer-section {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 3px;
}

.hearts-container {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-right: 2px;
}

.heart {
  width: 15px;
  height: 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.heart-svg {
  width: 100%;
  height: 100%;
  fill: var(--color-black);
  stroke: var(--color-black);
  stroke-width: 1;
  transition: all 0.25s ease;
}

.heart.lost .heart-svg {
  fill: transparent;
  stroke: #B8B0A2;
  stroke-width: 2;
  opacity: 0.35;
}

.heart.lost {
  animation: heartBreak 0.35s ease;
}

@keyframes heartBreak {
  0% { transform: scale(1.35); }
  50% { transform: scale(0.8) rotate(-12deg); }
  100% { transform: scale(1) rotate(0deg); }
}

.timer-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.mobile-install-btn {
  display: none;
  align-items: center;
  gap: 4px;
  background-color: var(--color-white);
  color: var(--color-black);
  border: 1.5px solid var(--color-black);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: all var(--transition-fast);
}

.mobile-install-btn:active {
  transform: scale(0.94);
  background-color: var(--color-cream-peer);
}

.timer-box {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-charcoal);
  background: var(--color-white);
  padding: 5px 10px;
  border-radius: 8px;
  border: 1px solid var(--color-border-subtle);
  cursor: pointer;
}

.timer-box:hover {
  border-color: var(--color-black);
}

.timer-text {
  font-variant-numeric: tabular-nums;
  min-width: 42px;
  text-align: center;
}

/* The Sudoku Board Area */
.board-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
}

.sudoku-board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background-color: var(--color-white);
  border: 2.5px solid var(--color-border-grid);
  border-radius: var(--board-radius);
  overflow: hidden;
  box-shadow: 0 6px 24px rgba(18, 18, 18, 0.07);
}

/* Cell Styling */
.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--color-white);
  border-right: 1px solid var(--color-border-subtle);
  border-bottom: 1px solid var(--color-border-subtle);
  cursor: pointer;
  font-size: clamp(18px, 4.2vw, 26px);
  font-weight: 500;
  color: var(--color-charcoal);
  font-family: var(--font-grid);
  transition: background-color var(--transition-fast);
}

/* 3x3 Block Thicker Dividers */
.cell:nth-child(3n) {
  border-right: 2px solid var(--color-border-grid);
}
.cell:nth-child(9n) {
  border-right: none;
}
.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  border-bottom: 2px solid var(--color-border-grid);
}
.cell:nth-child(n + 73):nth-child(-n + 81) {
  border-bottom: none;
}

/* Initial Clues */
.cell.given {
  font-weight: 700;
  color: var(--color-black);
}

/* Selection and Highlighting Hierarchy */
.cell.peer-highlight {
  background-color: var(--color-cream-peer);
}

.cell.match-highlight {
  background-color: var(--color-cream-match);
  font-weight: 700;
}

.cell.selected {
  background-color: var(--color-black) !important;
  color: var(--color-white) !important;
  z-index: 2;
}

.cell.selected .note-num {
  color: #BDBDBD;
}

/* Wrong Entry: Prominent RED X mark over cell */
.error-x-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  font-family: Arial, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: clamp(30px, 7vw, 42px);
  font-weight: 900;
  color: #E53935 !important; /* Bright, vibrant RED */
  text-shadow: 0 0 6px rgba(255, 255, 255, 0.95), 0 0 12px rgba(255, 255, 255, 0.9);
  z-index: 25;
  pointer-events: none;
  line-height: 1;
  animation: errorPopShake 0.4s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

@keyframes errorPopShake {
  0% { transform: scale(0.4); opacity: 0; }
  30% { transform: scale(1.3) translate3d(-3px, 0, 0); opacity: 1; }
  50% { transform: scale(0.95) translate3d(3px, 0, 0); }
  70% { transform: scale(1.05) translate3d(-2px, 0, 0); }
  100% { transform: scale(1) translate3d(0, 0, 0); opacity: 1; }
}

.cell.has-error,
.cell.has-error.selected {
  background-color: #FFF0F0 !important;
}

.cell.has-error .cell-val {
  color: #E53935 !important;
  opacity: 0.75;
}

/* Conflict Indicator */
.cell.conflict {
  background-color: var(--color-cream-peer);
}

.cell.conflict::after {
  content: '';
  position: absolute;
  bottom: 4px;
  width: 50%;
  height: 2px;
  background-color: var(--color-black);
  border-radius: 2px;
}

/* Pencil Notes Micro-Grid */
.notes-grid {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 2px;
  pointer-events: none;
}

.note-num {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(8px, 1.8vw, 11px);
  font-weight: 600;
  color: var(--color-muted);
  line-height: 1;
}

/* Veil Overlay when Paused */
.pause-veil {
  position: absolute;
  inset: 0;
  background-color: rgba(250, 247, 242, 0.95);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-radius: var(--board-radius);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 10;
  cursor: pointer;
  border: 2px solid var(--color-black);
}

.pause-veil.active {
  display: flex;
}

.pause-title {
  font-family: var(--font-title);
  font-size: 26px;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: var(--color-black);
  margin-bottom: 6px;
}

.pause-desc {
  font-size: 13px;
  color: var(--color-muted);
  letter-spacing: 0.05em;
}

/* Action Controls (Undo, Erase, Notes, New) */
.action-bar {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.action-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: var(--color-white);
  border: 1.5px solid var(--color-border-subtle);
  border-radius: 10px;
  padding: 8px 4px 6px;
  cursor: pointer;
  color: var(--color-black);
  transition: all var(--transition-fast);
  position: relative;
}

.action-btn:hover {
  border-color: var(--color-black);
}

.action-btn:active {
  transform: scale(0.95);
  background-color: var(--color-cream-peer);
}

.action-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  border-color: var(--color-border-subtle);
  transform: none;
}

.action-icon {
  width: 20px;
  height: 20px;
  margin-bottom: 3px;
}

.action-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

/* Notes Mode Active Indicator */
.action-btn.notes-active {
  background-color: var(--color-black);
  color: var(--color-white);
  border-color: var(--color-black);
}

.action-btn.notes-active svg {
  stroke: var(--color-white);
}

.notes-badge {
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  background-color: var(--color-black);
  color: var(--color-white);
  padding: 1px 4px;
  border-radius: 4px;
}

.action-btn.notes-active .notes-badge {
  background-color: var(--color-white);
  color: var(--color-black);
}

/* Number Pad (1-9) */
.numpad {
  display: grid;
  gap: 6px;
}

.num-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: var(--color-white);
  border: 1.5px solid var(--color-border-subtle);
  border-radius: 8px;
  cursor: pointer;
  transition: all var(--transition-fast);
  padding: 4px;
}

.num-btn:hover {
  border-color: var(--color-black);
}

.num-btn:active {
  transform: scale(0.93);
  background-color: var(--color-cream-peer);
}

.num-btn.active {
  background-color: var(--color-black);
  border-color: var(--color-black);
  color: var(--color-white);
}

.num-btn.active .num-count {
  color: #BDBDBD;
}

.num-btn.completed {
  opacity: 0.25;
  pointer-events: none;
  border-color: transparent;
  background-color: transparent;
}

.num-val {
  font-family: var(--font-grid);
  font-size: clamp(18px, 3.8vw, 22px);
  font-weight: 700;
  line-height: 1;
}

.num-count {
  font-size: 9px;
  font-weight: 600;
  color: var(--color-muted);
  margin-top: 2px;
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background-color: rgba(18, 18, 18, 0.55);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal-overlay.open {
  display: flex;
}

.modal-card {
  width: 100%;
  max-width: 400px;
  background-color: var(--color-cream-bg);
  border: 2px solid var(--color-black);
  border-radius: 16px;
  padding: 28px 24px 22px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
  text-align: center;
  animation: modalPop 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalPop {
  from {
    opacity: 0;
    transform: scale(0.92);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal-title {
  font-family: var(--font-title);
  font-size: 24px;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--color-black);
  margin-bottom: 4px;
}

.modal-subtitle {
  font-size: 11px;
  letter-spacing: 0.18em;
  color: var(--color-muted);
  margin-bottom: 16px;
  text-transform: uppercase;
}

.modal-desc {
  font-size: 13px;
  line-height: 1.5;
  color: var(--color-charcoal);
  margin-bottom: 20px;
}

.modal-stats {
  background-color: var(--color-white);
  border: 1px solid var(--color-border-subtle);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-item {
  display: flex;
  flex-direction: column;
}

.stat-val {
  font-size: 20px;
  font-weight: 800;
  color: var(--color-black);
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 11px;
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 2px;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.btn-primary {
  width: 100%;
  background-color: var(--color-black);
  color: var(--color-white);
  border: 1.5px solid var(--color-black);
  padding: 12px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.08em;
  border-radius: 10px;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-primary:hover {
  background-color: var(--color-charcoal);
}

.btn-primary:active {
  transform: scale(0.97);
}

.btn-secondary {
  width: 100%;
  background-color: var(--color-white);
  color: var(--color-black);
  border: 1.5px solid var(--color-border-subtle);
  padding: 11px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  border-radius: 10px;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-secondary:hover {
  border-color: var(--color-black);
}

.btn-secondary:active {
  transform: scale(0.97);
}

/* Settings toggles list */
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: left;
  margin-bottom: 20px;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.setting-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-black);
}

.setting-sub {
  font-size: 11px;
  color: var(--color-muted);
  margin-top: 2px;
}

.toggle-switch {
  position: relative;
  width: 44px;
  height: 24px;
  cursor: pointer;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background-color: var(--color-cream-peer);
  border: 1.5px solid var(--color-border-subtle);
  border-radius: 24px;
  transition: 0.2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  height: 16px;
  width: 16px;
  left: 3px;
  bottom: 2.5px;
  background-color: var(--color-muted);
  border-radius: 50%;
  transition: 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
  background-color: var(--color-black);
  border-color: var(--color-black);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(18px);
  background-color: var(--color-white);
}

/* Victory particle canvas */
#confettiCanvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 99;
}

/* ==========================================================================
   RESPONSIVE BREAKPOINTS: Mobile vs Desktop
   ========================================================================== */

/* MOBILE LAYOUT (< 820px) */
@media (max-width: 819px) {
  .app-container {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 8px 14px 12px;
  }

  .main-content-layout {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: calc(100% - 50px);
    gap: 8px;
  }

  .controls-panel {
    display: contents; /* Unwraps children so order works directly in main-content-layout */
  }

  .status-bar {
    order: 1;
    margin: 4px 0 2px;
  }

  .board-wrapper {
    order: 2;
    margin: 0 auto;
    max-height: calc(100dvh - 240px);
    max-width: calc(100dvh - 240px);
  }

  .action-bar {
    order: 3;
    margin-top: 4px;
  }

  .numpad {
    order: 4;
    grid-template-columns: repeat(9, 1fr);
    margin-bottom: 2px;
  }

  .num-btn {
    aspect-ratio: 1 / 1.35;
  }

  .desktop-hints {
    display: none;
  }
}

/* DESKTOP & TABLET LAYOUT (>= 820px) */
@media (min-width: 820px) {
  .app-container {
    width: min(94%, 1060px);
    height: auto;
    min-height: 85vh;
    margin: auto;
    padding: 28px 36px 32px;
    border-radius: 20px;
    box-shadow: 0 12px 48px rgba(18, 18, 18, 0.08);
    border: 1px solid var(--color-border-subtle);
  }

  .main-content-layout {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: 36px;
    align-items: center;
    margin-top: 24px;
  }

  .board-wrapper {
    width: 100%;
    max-width: 530px;
    margin: 0 auto;
  }

  .controls-panel {
    display: flex;
    flex-direction: column;
    gap: 16px;
    background-color: var(--color-cream-peer);
    padding: 20px;
    border-radius: 16px;
    border: 1px solid var(--color-border-subtle);
  }

  .numpad {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .num-btn {
    height: 52px;
    flex-direction: row;
    gap: 8px;
    padding: 6px 12px;
  }

  .num-val {
    font-size: 22px;
  }

  .num-count {
    font-size: 11px;
    margin-top: 0;
  }

  /* Desktop Keyboard Hints Card */
  .desktop-hints {
    display: block;
    background-color: var(--color-white);
    border: 1px solid var(--color-border-subtle);
    border-radius: 10px;
    padding: 10px 12px;
    margin-top: 4px;
  }

  .hints-title {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.1em;
    color: var(--color-muted);
    text-transform: uppercase;
    margin-bottom: 6px;
  }

  .hints-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 4px 10px;
    font-size: 11px;
    color: var(--color-charcoal);
  }

  .hint-item {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .kbd {
    display: inline-block;
    padding: 2px 6px;
    font-size: 10px;
    font-weight: 700;
    font-family: var(--font-grid);
    background-color: var(--color-cream-peer);
    border: 1px solid var(--color-border-subtle);
    border-radius: 4px;
    color: var(--color-black);
  }

  .mobile-install-btn {
    display: none !important;
  }
}
