:root{
  /* ---- Token system ---- */
  --paper:        #F6F1E6;
  --paper-deep:   #EFE8D8;
  --ink:          #1C232E;
  --ink-soft:     #3D4654;
  --rule:         #C9C0AA;
  --rule-strong:  #1C232E;
  --teal:         #1F6F61;
  --teal-soft:    #DCEDE7;
  --amber:        #C97A2B;
  --amber-soft:   #F3E0C4;
  --rose:         #B0463E;
  --rose-soft:    #F3DCD8;
  --gold:         #A88324;

  --font-display: 'Spectral', Georgia, serif;
  --font-ui: 'Inter', -apple-system, sans-serif;
  --font-num: 'JetBrains Mono', monospace;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --shadow-card: 0 1px 2px rgba(28,35,46,0.06), 0 8px 24px -8px rgba(28,35,46,0.12);
  --shadow-pressed: inset 0 1px 3px rgba(28,35,46,0.18);
}

*{ box-sizing:border-box; -webkit-tap-highlight-color:transparent; }
html,body{ margin:0; padding:0; }

body{
  background: var(--paper);
  background-image:
    radial-gradient(circle at 20% 10%, rgba(168,131,36,0.05), transparent 40%),
    radial-gradient(circle at 85% 80%, rgba(31,111,97,0.05), transparent 45%);
  font-family: var(--font-ui);
  color: var(--ink);
  min-height: 100vh;
  overflow-x: hidden;
}

/* subtle paper grain */
body::before{
  content:"";
  position: fixed; inset:0;
  pointer-events:none;
  opacity: 0.5;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix type='matrix' values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 0.02 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 0;
}

button{ font-family: inherit; cursor:pointer; border:none; background:none; color: inherit; }
button:focus-visible, [tabindex]:focus-visible{ outline: 2px solid var(--teal); outline-offset: 2px; }

::selection{ background: var(--amber-soft); }

/* ---------------- App shell ---------------- */
#app{
  position: relative;
  z-index: 1;
  max-width: 1180px;
  margin: 0 auto;
  padding: 22px 20px 60px;
  display:flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ---------------- Top bar ---------------- */
.topbar{
  display:flex;
  align-items:center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.brand{
  display:flex;
  align-items:baseline;
  gap: 10px;
}
.brand-mark{
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 27px;
  letter-spacing: 0.2px;
  color: var(--ink);
}
.brand-mark em{
  font-style: italic;
  color: var(--teal);
  font-weight: 500;
}
.brand-sub{
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  opacity: 0.65;
  font-weight: 600;
}

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

.icon-btn{
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  display:flex; align-items:center; justify-content:center;
  background: rgba(28,35,46,0.04);
  border: 1px solid transparent;
  transition: background 120ms ease, transform 120ms ease;
  color: var(--ink-soft);
}
.icon-btn:hover{ background: rgba(28,35,46,0.08); }
.icon-btn:active{ transform: scale(0.94); }
.icon-btn svg{ width:18px; height:18px; }

/* ---------------- Layout: board + side panel ---------------- */
.layout{
  display:grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 28px;
  align-items:start;
  flex: 1;
}

@media (max-width: 900px){
  .layout{ grid-template-columns: 1fr; }
}

/* ---------------- Status row above board ---------------- */
.status-row{
  display:flex;
  align-items:center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 12px;
  flex-wrap: wrap;
}

.diff-pill{
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
}

.stat-cluster{
  display:flex;
  gap: 18px;
  align-items:center;
  font-family: var(--font-num);
}

.stat{
  display:flex;
  flex-direction: column;
  align-items:flex-start;
  line-height: 1.1;
}
.stat-label{
  font-family: var(--font-ui);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  opacity: 0.6;
  font-weight: 700;
  margin-bottom: 2px;
}
.stat-value{
  font-size: 17px;
  font-weight: 500;
  color: var(--ink);
}
.stat-value.danger{ color: var(--rose); }

.timer-btn{
  display:flex; align-items:center; gap:6px;
  padding: 0;
}
.timer-btn svg{ width:14px; height:14px; opacity:0.7; }

/* ---------------- Board ---------------- */
.board-wrap{
  position: relative;
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
  aspect-ratio: 1 / 1;
}

.board{
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--paper-deep);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  overflow: hidden;
  border: 2.5px solid var(--rule-strong);
}

.cell{
  position: relative;
  display:flex;
  align-items:center;
  justify-content:center;
  font-family: var(--font-num);
  font-size: clamp(16px, 3.6vw, 26px);
  font-weight: 500;
  color: var(--ink-soft);
  background: var(--paper);
  border-right: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  user-select: none;
  cursor: pointer;
  transition: background 90ms ease;
}

/* thicker box borders */
.cell[data-col="2"], .cell[data-col="5"]{ border-right: 2.5px solid var(--rule-strong); }
.cell[data-row="2"], .cell[data-row="5"]{ border-bottom: 2.5px solid var(--rule-strong); }
.cell[data-col="8"]{ border-right: none; }
.cell[data-row="8"]{ border-bottom: none; }

.cell.given{ color: var(--ink); font-weight: 600; }
.cell.user-entry{ color: var(--teal); font-weight: 500; }
.cell.error{ color: var(--rose) !important; }

.cell.peer{ background: rgba(28,35,46,0.035); }
.cell.row-col-highlight{ background: rgba(31,111,97,0.045); }
.cell.same-number{ background: var(--amber-soft); }
.cell.selected{ background: var(--teal-soft); }
.cell.selected.error{ background: var(--rose-soft); }

.cell.error::after{
  content:"";
  position:absolute; inset: 3px;
  border-radius: 4px;
  background: var(--rose-soft);
  z-index: -1;
}

.cell.flash{
  animation: flashIn 320ms ease;
}
@keyframes flashIn{
  0%{ transform: scale(0.82); opacity: 0.4; }
  60%{ transform: scale(1.08); }
  100%{ transform: scale(1); opacity:1; }
}

.cell.error-flash{
  animation: errorShake 380ms ease;
}
@keyframes errorShake{
  0%{ transform: translateX(0); }
  20%{ transform: translateX(-4px); }
  40%{ transform: translateX(4px); }
  60%{ transform: translateX(-3px); }
  80%{ transform: translateX(3px); }
  100%{ transform: translateX(0); }
}

.cell.hint-glow{
  animation: hintGlow 1100ms ease;
}
@keyframes hintGlow{
  0%{ box-shadow: inset 0 0 0 3px var(--gold); background: var(--amber-soft); }
  100%{ box-shadow: inset 0 0 0 0px transparent; }
}

.cell .pencil-grid{
  position:absolute; inset:0;
  display:grid;
  grid-template-columns: repeat(3,1fr);
  grid-template-rows: repeat(3,1fr);
  pointer-events:none;
}
.cell .pencil-grid span{
  display:flex; align-items:center; justify-content:center;
  font-family: var(--font-num);
  font-size: clamp(8px, 1.5vw, 11px);
  color: var(--ink-soft);
  opacity: 0.7;
  font-weight: 500;
}

/* completed line celebration pulse */
.cell.line-complete{
  animation: lineComplete 480ms ease;
}
@keyframes lineComplete{
  0%{ background: var(--teal-soft); }
  100%{ background: var(--paper); }
}

/* win overlay shimmer cell */
@keyframes winPulse{
  0%, 100%{ background: var(--paper); }
  50%{ background: var(--teal-soft); }
}
.cell.win-pulse{ animation: winPulse 900ms ease; }

/* board frozen overlay (paused / won) */
.board-overlay{
  position:absolute; inset:0;
  background: rgba(246,241,230,0.93);
  backdrop-filter: blur(3px);
  display:flex;
  flex-direction:column;
  align-items:center;
  justify-content:center;
  gap: 14px;
  border-radius: var(--radius-md);
  z-index: 5;
  text-align:center;
  padding: 20px;
}
.board-overlay.hidden{ display:none; }
.overlay-title{
  font-family: var(--font-display);
  font-size: 30px;
  font-weight: 600;
  color: var(--ink);
}
.overlay-sub{
  font-size: 13px;
  color: var(--ink-soft);
  font-family: var(--font-num);
}
.overlay-btn{
  margin-top: 6px;
  padding: 11px 26px;
  background: var(--ink);
  color: var(--paper);
  border-radius: 999px;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  transition: transform 120ms ease, background 120ms ease;
}
.overlay-btn:hover{ background: var(--teal); }
.overlay-btn:active{ transform: scale(0.96); }

/* ---------------- Number pad ---------------- */
.numpad{
  display:grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 8px;
  max-width: 620px;
  margin: 18px auto 0;
}
@media (max-width: 520px){
  .numpad{ grid-template-columns: repeat(5, 1fr); }
}

.num-btn{
  position: relative;
  aspect-ratio: 1/1;
  background: var(--paper-deep);
  border: 1.5px solid var(--rule);
  border-radius: var(--radius-sm);
  font-family: var(--font-num);
  font-size: clamp(16px,3vw,22px);
  font-weight: 600;
  color: var(--ink);
  display:flex; align-items:center; justify-content:center;
  transition: background 100ms ease, transform 100ms ease, border-color 100ms ease;
}
.num-btn:hover{ background: var(--teal-soft); border-color: var(--teal); }
.num-btn:active{ transform: scale(0.93); }
.num-btn .remaining{
  position:absolute;
  bottom: 3px; right: 5px;
  font-size: 9px;
  font-family: var(--font-ui);
  font-weight: 700;
  color: var(--ink-soft);
  opacity: 0.5;
}
.num-btn.depleted{ opacity: 0.32; pointer-events:none; }

/* ---------------- Toolbar (undo/erase/pencil/hint) ---------------- */
.toolbar{
  display:grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  max-width: 620px;
  margin: 12px auto 0;
}

.tool-btn{
  display:flex;
  flex-direction:column;
  align-items:center;
  gap: 5px;
  padding: 10px 6px 8px;
  border-radius: var(--radius-sm);
  background: transparent;
  transition: background 100ms ease;
  position: relative;
}
.tool-btn:hover{ background: rgba(28,35,46,0.05); }
.tool-btn:active{ transform: scale(0.96); }
.tool-btn svg{ width: 20px; height:20px; color: var(--ink-soft); }
.tool-btn span{
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.tool-btn.active{ background: var(--teal-soft); }
.tool-btn.active svg, .tool-btn.active span{ color: var(--teal); }
.tool-btn .badge{
  position:absolute;
  top: 3px; right: 14px;
  min-width: 15px; height:15px;
  border-radius: 999px;
  background: var(--amber);
  color: #fff;
  font-size: 9px;
  font-weight:800;
  display:flex; align-items:center; justify-content:center;
  padding: 0 3px;
}
.tool-btn:disabled{ opacity: 0.35; pointer-events:none; }

/* ---------------- Side panel ---------------- */
.panel{
  display:flex;
  flex-direction:column;
  gap: 16px;
}

.panel-card{
  background: var(--paper-deep);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  padding: 16px;
  box-shadow: var(--shadow-card);
}

.panel-title{
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--ink-soft);
  opacity: 0.65;
  margin: 0 0 12px;
}

.mode-grid{
  display:grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.mode-btn{
  text-align:left;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--rule);
  background: var(--paper);
  transition: border-color 120ms ease, background 120ms ease;
}
.mode-btn:hover{ border-color: var(--teal); }
.mode-btn.active{ border-color: var(--teal); background: var(--teal-soft); }
.mode-name{
  display:block;
  font-weight: 700;
  font-size: 13px;
  color: var(--ink);
}
.mode-meta{
  display:block;
  font-size: 10.5px;
  color: var(--ink-soft);
  opacity: 0.7;
  font-family: var(--font-num);
  margin-top: 2px;
}

.new-game-btn{
  width: 100%;
  padding: 12px;
  margin-top: 10px;
  border-radius: var(--radius-sm);
  background: var(--ink);
  color: var(--paper);
  font-weight: 700;
  font-size: 12.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  transition: background 120ms ease, transform 120ms ease;
}
.new-game-btn:hover{ background: var(--teal); }
.new-game-btn:active{ transform: scale(0.98); }

.toggle-row{
  display:flex;
  align-items:center;
  justify-content: space-between;
  padding: 9px 0;
  border-bottom: 1px solid var(--rule);
  font-size: 13px;
  font-weight: 500;
}
.toggle-row:last-child{ border-bottom:none; }

.switch{
  position: relative;
  width: 38px; height: 22px;
  border-radius: 999px;
  background: var(--rule);
  transition: background 150ms ease;
  flex-shrink:0;
}
.switch::after{
  content:"";
  position:absolute;
  top:2px; left:2px;
  width: 18px; height:18px;
  border-radius:50%;
  background: var(--paper);
  box-shadow: 0 1px 2px rgba(0,0,0,0.25);
  transition: transform 150ms ease;
}
.switch.on{ background: var(--teal); }
.switch.on::after{ transform: translateX(16px); }

.stats-grid{
  display:grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.stats-box{
  background: var(--paper);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  border: 1px solid var(--rule);
}
.stats-box .num{
  font-family: var(--font-num);
  font-size: 20px;
  font-weight: 600;
  color: var(--ink);
  display:block;
}
.stats-box .lbl{
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  opacity: 0.65;
  font-weight: 700;
}

.howto-list{
  margin: 0; padding: 0;
  list-style: none;
  font-size: 12.5px;
  color: var(--ink-soft);
  line-height: 1.6;
}
.howto-list li{
  padding: 6px 0;
  border-bottom: 1px dashed var(--rule);
  display:flex; gap:8px;
}
.howto-list li:last-child{ border-bottom:none; }
.howto-list kbd{
  font-family: var(--font-num);
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 11px;
  flex-shrink:0;
  height: fit-content;
}

/* ---------------- Modal ---------------- */
.modal-backdrop{
  position: fixed; inset:0;
  background: rgba(28,35,46,0.45);
  backdrop-filter: blur(2px);
  display:flex; align-items:center; justify-content:center;
  z-index: 50;
  padding: 20px;
  opacity:0;
  pointer-events:none;
  transition: opacity 160ms ease;
}
.modal-backdrop.open{ opacity:1; pointer-events:all; }

.modal{
  background: var(--paper);
  border-radius: var(--radius-lg);
  padding: 26px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 20px 60px -10px rgba(28,35,46,0.4);
  transform: translateY(8px) scale(0.98);
  transition: transform 160ms ease;
  max-height: 86vh;
  overflow-y:auto;
}
.modal-backdrop.open .modal{ transform: translateY(0) scale(1); }

.modal-title{
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 600;
  margin: 0 0 4px;
}
.modal-desc{
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0 0 18px;
}

.modal-diff-list{
  display:flex;
  flex-direction:column;
  gap: 8px;
}
.modal-diff-btn{
  display:flex;
  align-items:center;
  justify-content: space-between;
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--rule);
  background: var(--paper-deep);
  transition: border-color 120ms ease, background 120ms ease;
}
.modal-diff-btn:hover{ border-color: var(--teal); background: var(--teal-soft); }
.modal-diff-btn .name{ font-weight: 700; font-size: 14px; }
.modal-diff-btn .desc{ font-size: 11px; color: var(--ink-soft); opacity:0.75; margin-top:2px; }
.modal-diff-btn .dots{ display:flex; gap:3px; }
.modal-diff-btn .dot{ width:6px; height:6px; border-radius:50%; background: var(--rule); }
.modal-diff-btn .dot.fill{ background: var(--amber); }

.modal-close-x{
  position:absolute;
  top: 16px; right:16px;
  width: 30px; height:30px;
  border-radius:50%;
  display:flex; align-items:center; justify-content:center;
  background: rgba(28,35,46,0.06);
}
.modal-close-x:hover{ background: rgba(28,35,46,0.12); }
.modal{ position:relative; }

.confirm-actions{
  display:flex; gap: 10px;
  margin-top: 18px;
}
.confirm-actions button{
  flex:1;
  padding: 11px;
  border-radius: var(--radius-sm);
  font-weight:700;
  font-size: 13px;
}
.confirm-cancel{ background: var(--paper-deep); border: 1.5px solid var(--rule); color: var(--ink); }
.confirm-ok{ background: var(--rose); color:#fff; }
.confirm-cancel:hover{ background: rgba(28,35,46,0.08); }
.confirm-ok:hover{ background: #963b34; }

/* toast */
.toast{
  position: fixed;
  bottom: 26px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: var(--paper);
  padding: 11px 20px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  opacity: 0;
  pointer-events:none;
  transition: opacity 200ms ease, transform 200ms ease;
  z-index: 60;
  white-space: nowrap;
}
.toast.show{
  opacity:1;
  transform: translateX(-50%) translateY(0);
}

@media (prefers-reduced-motion: reduce){
  *{ animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

@media (max-width: 600px){
  .panel-card .mode-grid{ grid-template-columns: 1fr 1fr; }
  .topbar-actions .icon-btn-label{ display:none; }
}
