:root {
  --blue: #149bd7;
  --ink: #102131;
  --muted: #5c6b78;
  --paper: #eef4f8;
  --panel: #ffffff;
  --line: #d8e2ea;
  --dark: #071018;
  --accent: #8b4b90;
  --accent-dark: #5a115a;
  --gold: #f3c34f;
  --green: #34a853;
  --amber: #f7b733;
  --red: #ef5b5b;
  --shadow: 0 12px 32px rgba(15, 33, 49, 0.14);
  --radius: 8px;
  --touch: 48px;
}

* { box-sizing: border-box; }

html,
body {
  min-height: 100%;
  margin: 0;
  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 {
  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,
input:focus-visible {
  outline: 4px solid rgba(20, 155, 215, 0.35);
  outline-offset: 2px;
}

input {
  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 auto 1fr;
  min-height: 100vh;
  min-height: 100dvh;
}

.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;
  padding: 12px 18px;
  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(--muted);
  font-weight: 800;
}

.max-input {
  width: 88px;
  text-align: center;
}

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

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

.wide-button[aria-pressed="true"],
.wide-button.primary {
  background: var(--accent);
  border-color: var(--accent-dark);
  color: #ffffff;
}

.check-pill {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  font-weight: 800;
  white-space: nowrap;
}

.check-pill input {
  width: 20px;
  height: 20px;
  min-height: 20px;
  padding: 0;
  accent-color: var(--accent);
}

.workspace {
  min-height: 0;
  overflow: auto;
  padding: 18px;
}

.tool-surface {
  width: min(1180px, 100%);
  min-height: 640px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.95);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  transform-origin: top center;
}

.puzzle-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(230px, 300px);
  gap: 16px;
  align-items: stretch;
}

.arithmagon-wrap {
  min-height: 520px;
  display: grid;
  place-items: center;
  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;
  overflow: auto;
}

.arithmagon-board {
  --zoom: 1;
  width: 760px;
  height: 510px;
  position: relative;
  transform: scale(var(--zoom));
  transform-origin: top left;
}

.connector-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.connector-layer line {
  stroke: rgba(90, 17, 90, 0.18);
  stroke-width: 8;
  stroke-linecap: round;
}

.node {
  position: absolute;
  display: grid;
  place-items: center;
}

.node-input {
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 0 4px;
  border: 2px solid var(--accent-dark);
  background: #ffffff;
  color: var(--accent-dark);
  font-size: clamp(1.4rem, 4vw, 2.4rem);
  font-weight: 800;
  text-align: center;
  box-shadow: 8px 10px 0 rgba(90, 17, 90, 0.2);
}

.node-input::placeholder {
  color: rgba(90, 17, 90, 0.35);
}

.node-input.is-correct {
  border-color: var(--green);
  box-shadow: 8px 10px 0 rgba(52, 168, 83, 0.18);
}

.node-input.is-incorrect {
  border-color: var(--amber);
  box-shadow: 8px 10px 0 rgba(247, 183, 51, 0.22);
}

.node.circle {
  width: 112px;
  height: 112px;
}

.node.square {
  width: 112px;
  height: 112px;
}

.node.circle .node-input {
  border-radius: 50%;
}

.node.top {
  left: 50%;
  top: 20px;
  translate: -50% 0;
}

.node.left {
  left: 118px;
  bottom: 28px;
}

.node.right {
  right: 118px;
  bottom: 28px;
}

.node.top-left {
  left: 215px;
  top: 210px;
}

.node.top-right {
  right: 215px;
  top: 210px;
}

.node.bottom {
  left: 50%;
  bottom: 26px;
  translate: -50% 0;
}

.side-panel {
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 12px;
}

.feedback,
.rules-card,
.answer-card {
  padding: 12px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  line-height: 1.35;
}

.feedback {
  min-height: 64px;
  font-weight: 800;
}

.feedback.good {
  border-color: #76b947;
  background: #eef9e8;
  color: #22510f;
}

.feedback.try {
  border-color: #f2b84b;
  background: #fff8e6;
  color: #6a4300;
}

.rules-card h2,
.answer-card h2 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.rules-card p,
.answer-card p {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.answer-card {
  display: grid;
  gap: 8px;
  align-content: start;
}

.small-button {
  min-height: 42px;
  padding: 0 10px;
  border-color: var(--line);
  background: #ffffff;
  color: var(--ink);
  font-weight: 900;
}

.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: 980px) {
  .puzzle-layout {
    grid-template-columns: 1fr;
  }

  .side-panel {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: auto;
  }

  .arithmagon-wrap {
    min-height: 480px;
  }
}

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

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

  .side-panel {
    grid-template-columns: 1fr;
  }

  .arithmagon-board {
    width: 760px;
    height: 510px;
  }

  .node.circle,
  .node.square {
    width: 112px;
    height: 112px;
  }
}

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

  body {
    background: #ffffff;
  }

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

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

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

  .puzzle-layout {
    display: block;
  }

  .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;
  }
}
