:root {
  --blue: #149bd7;
  --ink: #102131;
  --muted: #5c6b78;
  --paper: #eef4f8;
  --line: #d8e2ea;
  --dark: #071018;
  --accent: #8b4b90;
  --accent-dark: #5a115a;
  --good: #9bf3a0;
  --shadow: 0 12px 32px rgba(15, 33, 49, 0.14);
  --radius: 8px;
  --touch: 48px;
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    linear-gradient(#dce8f0 1px, transparent 1px),
    linear-gradient(90deg, #dce8f0 1px, transparent 1px),
    var(--paper);
  background-size: 34px 34px;
  font-family: Arial, Helvetica, sans-serif;
  letter-spacing: 0;
  touch-action: manipulation;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: var(--touch);
  border: 2px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
select:focus-visible {
  outline: 4px solid rgba(20, 155, 215, 0.35);
  outline-offset: 2px;
}

select {
  min-height: var(--touch);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--ink);
  padding: 0 12px;
  font-weight: 800;
}

.app {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

#gridSumsModel {
  min-height: 0;
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  background: var(--dark);
  color: #ffffff;
}

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

.brand-logo {
  display: inline-flex;
  border-radius: 6px;
}

.brand-logo:focus-visible {
  outline: 4px solid rgba(20, 155, 215, 0.35);
  outline-offset: 3px;
}

.brand img {
  width: 142px;
  height: auto;
  display: block;
  object-fit: contain;
  flex: 0 0 auto;
}

h1 {
  margin: 0;
  font-size: clamp(1.35rem, 2vw, 2.05rem);
  line-height: 1.05;
  font-weight: 800;
}

.top-actions,
.toolbar,
.control-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.top-actions {
  justify-content: end;
}

.icon-button {
  width: var(--touch);
  padding: 0;
  display: inline-grid;
  place-items: center;
  background: #122436;
  color: #ffffff;
  border-color: #263d51;
}

.icon-button:hover,
.icon-button[aria-pressed="true"] {
  background: var(--blue);
  border-color: #8fd8f3;
  color: #06111a;
}

.icon {
  width: 24px;
  height: 24px;
  display: block;
  pointer-events: none;
}

.home-button {
  min-height: var(--touch);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  border: 2px solid #263d51;
  border-radius: var(--radius);
  background: #122436;
  color: #ffffff;
  text-decoration: none;
  font-weight: 800;
  white-space: nowrap;
}

.home-button:hover,
.home-button:focus-visible {
  background: var(--blue);
  border-color: #8fd8f3;
  color: #06111a;
}

.toolbar {
  justify-content: center;
  flex-wrap: nowrap;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 8px 12px;
  background: #ffffff;
  border-bottom: 1px solid var(--line);
  box-shadow: 0 6px 14px rgba(15, 33, 49, 0.06);
  z-index: 3;
}

.control-label {
  color: var(--ink);
  font-weight: 800;
}

.size-select {
  width: 72px;
  text-align: center;
}

.wide-button {
  min-height: var(--touch);
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-color: var(--line);
  background: #f8fbfd;
  color: var(--ink);
  font-weight: 800;
  white-space: nowrap;
}

.wide-button:hover {
  border-color: #9cc9df;
}

.wide-button.primary {
  background: var(--accent);
  border-color: var(--accent-dark);
  color: #ffffff;
}

.workspace {
  min-height: 0;
  overflow: hidden;
  padding: 10px;
}

.tool-surface {
  width: min(1180px, calc(100vw - 20px));
  height: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: 0;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.95);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
}

.puzzle-intro {
  margin: 0 auto 4px;
  max-width: 960px;
  color: var(--muted);
  text-align: center;
  font-size: clamp(1rem, 2vw, 1.18rem);
  font-weight: 800;
  line-height: 1.35;
}

.puzzle-intro.compact {
  margin-bottom: 12px;
}

.board-wrap {
  flex: 1 1 auto;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  place-items: start center;
  padding: 12px;
  border: 2px solid #d7e5ec;
  border-radius: var(--radius);
  background:
    radial-gradient(circle at 22px 22px, rgba(20, 155, 215, 0.13) 2px, transparent 2px),
    linear-gradient(#fbfdff, #f3f8fb);
  background-size: 44px 44px, auto;
}

.sums-board {
  --cell-width: 286px;
  --cell-height: 142px;
  --gap: 8px;
  --zoom: 1;
  display: grid;
  grid-template-columns: repeat(var(--cols), calc(var(--cell-width) * var(--zoom)));
  grid-template-rows: repeat(var(--rows), calc(var(--cell-height) * var(--zoom)));
  gap: calc(var(--gap) * var(--zoom));
}

.sums-cell {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 0;
  border: 2px solid #05070d;
  border-radius: var(--radius);
  background: #ffffff;
  color: #05070d;
  box-shadow: 5px 6px 0 rgba(90, 17, 90, 0.18);
  font-size: clamp(1.15rem, calc(2.2rem * var(--zoom)), 3rem);
  font-weight: 500;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.sums-cell:hover,
.sums-cell:focus-visible {
  background: #f6fbff;
  outline-offset: -6px;
}

.sums-cell.has-value {
  font-weight: 800;
}

.sums-cell.last-clicked {
  border-color: var(--accent-dark);
  background: #fff8ff;
  box-shadow: 5px 6px 0 rgba(139, 75, 144, 0.28), inset 0 0 0 4px rgba(139, 75, 144, 0.08);
}

.score-strip {
  min-height: 42px;
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  flex-wrap: wrap;
  border: 2px solid var(--accent-dark);
  border-radius: var(--radius);
  background: var(--accent);
  color: #ffffff;
  text-align: center;
  font-size: clamp(1rem, 2vw, 1.25rem);
  font-weight: 900;
  box-shadow: 0 6px 12px rgba(90, 17, 90, 0.18);
}

.score-strip span {
  white-space: nowrap;
}

.print-logo,
.print-copyright {
  display: none;
}

.instructions-dialog {
  width: min(860px, calc(100vw - 32px));
  max-height: min(800px, calc(100vh - 32px));
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  color: var(--ink);
  background: #ffffff;
  box-shadow: 0 24px 70px rgba(6, 17, 26, 0.32);
}

.instructions-dialog::backdrop {
  background: rgba(6, 17, 26, 0.5);
}

.instructions-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: var(--dark);
  color: #ffffff;
}

.instructions-header h2 {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.15;
}

.instructions-body {
  max-height: calc(min(800px, 100vh - 32px) - 76px);
  overflow: auto;
  padding: 16px 18px 20px;
  line-height: 1.45;
}

.instructions-body h3 {
  margin: 16px 0 8px;
  font-size: 1.05rem;
}

.instructions-body h3:first-child {
  margin-top: 0;
}

.instructions-body p,
.instructions-body ul {
  margin: 0 0 10px;
}

.instructions-body li {
  margin-bottom: 6px;
}

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

  .top-actions {
    justify-content: start;
  }

  .toolbar {
    justify-content: start;
  }
}

@media print {
  @page {
    size: landscape;
  }

  body {
    background: #ffffff;
  }

  .topbar,
  .toolbar,
  .instructions-dialog {
    display: none;
  }

  .workspace {
    overflow: visible;
    padding: 0;
  }

  .tool-surface,
  .board-wrap {
    width: 100%;
    min-height: 0;
    box-shadow: none;
    border: 1px solid #000000;
    background: #ffffff;
  }

  .sums-board {
    margin: 0 auto;
  }

  .print-logo {
    display: block;
    width: 150px;
    margin-bottom: 10px;
  }

  .print-copyright {
    display: block;
    margin-top: 10px;
    color: #000000;
    font-size: 11pt;
    font-weight: 700;
    text-align: right;
  }
}
