:root {
  --blue: #149bd7;
  --ink: #102131;
  --muted: #5c6b78;
  --paper: #fbfcfe;
  --panel: #ffffff;
  --line: #d8e2ea;
  --shadow: 0 12px 32px rgba(15, 33, 49, 0.12);
  --radius: 8px;
  --touch: 48px;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background: #eef4f8;
  font-family: Arial, Helvetica, sans-serif;
  letter-spacing: 0;
  touch-action: manipulation;
}

body {
  overflow: hidden;
}

button,
input,
select {
  font: inherit;
}

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

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

.app {
  display: grid;
  grid-template-rows: auto auto 1fr;
  height: 100vh;
  height: 100dvh;
  min-width: 320px;
}

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

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

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

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

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

.title-block {
  min-width: 0;
}

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

.top-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: end;
  flex-wrap: wrap;
}

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

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

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

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  padding: 10px 18px;
  background: #ffffff;
  border-bottom: 1px solid var(--line);
  box-shadow: 0 6px 14px rgba(15, 33, 49, 0.06);
  z-index: 4;
}

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

.toolbar-right {
  margin-left: auto;
  justify-content: end;
}

.control-label {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
}

.wide-button,
.canvas-tab,
.tray-button {
  min-height: var(--touch);
  padding: 0 12px;
  border-color: var(--line);
  background: #f8fbfd;
  color: var(--ink);
  font-weight: 800;
  white-space: nowrap;
}

.wide-button:hover,
.canvas-tab:hover,
.tray-button:hover {
  border-color: var(--blue);
}

.wide-button[aria-pressed="true"],
.canvas-tab[aria-pressed="true"],
.primary {
  background: var(--blue);
  border-color: #0f80b3;
  color: #06111a;
}

.wide-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tool-menu {
  position: relative;
}

.tool-menu summary {
  min-height: var(--touch);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #f8fbfd;
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
  list-style: none;
  white-space: nowrap;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.tool-menu summary::-webkit-details-marker {
  display: none;
}

.menu-panel {
  width: max-content;
  max-width: min(520px, calc(100vw - 36px));
  padding: 10px;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 25;
  display: grid;
  gap: 8px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: var(--shadow);
}

.questions-panel {
  width: min(520px, calc(100vw - 36px));
}

.question-list {
  display: grid;
  gap: 8px;
}

.question-button {
  min-height: 60px;
  padding: 8px 10px;
  text-align: left;
  border-color: var(--line);
  background: #fbfdff;
  font-weight: 800;
  line-height: 1.25;
}

.question-button small {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.workspace {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 16px;
  background:
    linear-gradient(#dce8f0 1px, transparent 1px),
    linear-gradient(90deg, #dce8f0 1px, transparent 1px);
  background-size: 34px 34px;
}

.stage {
  min-width: 1220px;
  width: 1220px;
  margin: 0 auto;
}

.tile-surface {
  display: grid;
  grid-template-columns: 214px minmax(0, 1fr);
  gap: 12px;
  width: 1220px;
  height: 716px;
  min-height: 716px;
  zoom: var(--tile-zoom, 1);
}

.tray-panel,
.mat-panel {
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
  min-width: 0;
  min-height: 0;
  height: 100%;
}

.tray-panel {
  display: grid;
  grid-template-rows: auto auto 1fr;
  overflow: hidden;
}

.panel-title,
.mat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: #fbfdff;
  font-weight: 900;
}

.tray-hint {
  padding: 10px 12px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 800;
  line-height: 1.3;
  border-bottom: 1px solid var(--line);
}

.tray-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 7px;
  align-content: start;
  padding: 10px;
  overflow: auto;
  min-height: 0;
}

.tray-button {
  position: relative;
  overflow: hidden;
  justify-content: start;
  display: grid;
  grid-template-columns: 64px;
  align-items: center;
  padding: 5px 8px;
}

.shared-tray-tile {
  height: 36px;
  width: 64px;
  display: block;
  position: relative;
}

.shared-tray-tile .fraction-tile-model,
.fraction-tile .fraction-tile-model {
  position: absolute;
  inset: 0;
  width: auto;
  height: auto;
  margin: 0;
}

.shared-tray-tile .fraction-tile-model > figcaption,
.fraction-tile .fraction-tile-model > figcaption {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.shared-tray-tile .fraction-tile-visual,
.fraction-tile .fraction-tile-visual {
  display: block;
  width: 100%;
  height: 100%;
}

.mat-panel {
  display: grid;
  grid-template-rows: auto auto 1fr;
  overflow: hidden;
}

.feedback {
  min-height: 52px;
  padding: 9px 12px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: #f8fbfd;
  color: var(--ink);
  font-weight: 800;
  line-height: 1.25;
}

.feedback.good {
  background: #eef9e8;
  color: #22510f;
}

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

.canvas-mat {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background:
    linear-gradient(rgba(20, 155, 215, 0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(20, 155, 215, 0.1) 1px, transparent 1px),
    linear-gradient(#ffffff, #eef7fb);
  background-size: 24px 24px, 24px 24px, auto;
  touch-action: none;
}

.canvas-mat.empty::after {
  content: "Tap a fraction tile to add it here";
  position: absolute;
  left: 50%;
  top: 50%;
  translate: -50% -50%;
  padding: 14px 18px;
  border: 2px dashed #a7bfcd;
  border-radius: var(--radius);
  color: #557080;
  background: rgba(255, 255, 255, 0.84);
  font-weight: 900;
  text-align: center;
  pointer-events: none;
}

.fraction-tile {
  position: absolute;
  height: 52px;
  min-width: 34px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: #06111a;
  box-shadow: 0 8px 11px rgba(15, 33, 49, 0.14);
  cursor: grab;
  user-select: none;
  touch-action: none;
  overflow: hidden;
}

.fraction-tile.selected {
  z-index: 50;
  box-shadow: 0 0 0 4px rgba(20, 155, 215, 0.42), 0 9px 12px rgba(15, 33, 49, 0.16);
}

.lasso-box {
  position: absolute;
  z-index: 100;
  border: 2px dashed #071018;
  background: rgba(20, 155, 215, 0.12);
  pointer-events: none;
}

.delete-zone {
  position: absolute;
  right: 14px;
  bottom: 14px;
  z-index: 80;
  width: 76px;
  height: 76px;
  display: grid;
  place-items: center;
  border: 2px dashed rgba(85, 112, 128, 0.55);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.38);
  opacity: 0.74;
}

.delete-zone:hover,
.delete-zone.active {
  opacity: 1;
  background: rgba(255, 255, 255, 0.76);
  border-color: #d25a4b;
}

.bin-icon {
  position: relative;
  width: 30px;
  height: 34px;
  border: 4px solid #557080;
  border-top: 0;
  border-radius: 0 0 5px 5px;
}

.bin-icon::before,
.bin-icon::after {
  content: "";
  position: absolute;
  left: 50%;
  translate: -50% 0;
  background: #557080;
}

.bin-icon::before {
  top: -10px;
  width: 38px;
  height: 5px;
  border-radius: 4px;
}

.bin-icon::after {
  top: -16px;
  width: 16px;
  height: 5px;
  border-radius: 4px 4px 0 0;
}

.delete-zone:hover .bin-icon,
.delete-zone.active .bin-icon {
  border-color: #d25a4b;
}

.delete-zone:hover .bin-icon::before,
.delete-zone:hover .bin-icon::after,
.delete-zone.active .bin-icon::before,
.delete-zone.active .bin-icon::after {
  background: #d25a4b;
}

.dialog {
  width: min(460px, calc(100vw - 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 {
  width: min(820px, calc(100vw - 32px));
  max-height: min(780px, calc(100vh - 32px));
}

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

.dialog-header,
.instructions-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: #071018;
  color: #ffffff;
}

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

.dialog-body,
.instructions-body {
  padding: 16px 18px 20px;
  line-height: 1.45;
}

.instructions-body {
  max-height: calc(min(780px, 100vh - 32px) - 76px);
  overflow: auto;
}

.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,
.instructions-body ol {
  margin: 0 0 10px;
}

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

.form-grid {
  display: grid;
  gap: 12px;
}

.number-row {
  display: grid;
  grid-template-columns: 1fr 112px;
  gap: 12px;
  align-items: end;
}

.number-row label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 800;
}

.number-row input {
  width: 112px;
  min-height: var(--touch);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 0 10px;
  font-weight: 900;
}

.dialog-actions {
  display: flex;
  justify-content: end;
  gap: 8px;
  margin-top: 14px;
}

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

@media (max-width: 920px) {
  body {
    overflow: auto;
  }

  .app {
    min-height: 100vh;
    height: auto;
  }

  .topbar {
    grid-template-columns: 1fr;
  }

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

  .brand img {
    width: 116px;
  }

  .workspace {
    padding: 10px;
  }
}

@media print {
  body {
    overflow: visible;
    background: #ffffff;
  }

  .topbar,
  .toolbar,
  .tray-panel,
  .feedback,
  .delete-zone,
  .dialog,
  .instructions-dialog {
    display: none;
  }

  .app {
    display: block;
    width: 100%;
    height: 100% !important;
    min-height: 0 !important;
  }

  .workspace {
    box-sizing: border-box;
    width: 100% !important;
    height: 100% !important;
    min-height: 0 !important;
    max-height: 100% !important;
    overflow: hidden;
    padding: 0;
    background: #ffffff;
  }

  .stage,
  .tile-surface {
    box-sizing: border-box;
    width: 100% !important;
    height: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    max-width: 100% !important;
    max-height: 100% !important;
    transform: none !important;
    zoom: 1;
    display: block;
  }

  .mat-panel {
    width: 100%;
    height: 100% !important;
    min-height: 0 !important;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: hidden;
    border: 1px solid #000000;
    box-shadow: none;
  }

  .canvas-mat {
    min-height: 0;
    overflow: visible;
    background: #ffffff;
  }

  .print-logo {
    display: block;
    position: absolute;
    top: -42px;
    left: 50%;
    z-index: 200;
    width: 100px;
    margin: 0;
    translate: -50% 0;
  }

  .print-copyright {
    display: block;
    position: absolute;
    right: 10px;
    bottom: 8px;
    z-index: 200;
    margin: 0;
    padding: 2px 4px;
    color: #000000;
    background: rgba(255, 255, 255, 0.92);
    font-size: 11pt;
    font-weight: 700;
    text-align: right;
  }
}
