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

  * { 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,
  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,
  input:focus-visible,
  select:focus-visible {
    outline: 4px solid rgba(20, 155, 215, 0.35);
    outline-offset: 2px;
  }

  input,
  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 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 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;
  }

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

  .home-button:hover,
  .home-button:focus-visible {
    background: var(--blue, #149bd7);
    border-color: #8fd8f3;
    color: #06111a;
  }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;
  }

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

  .mode-controls {
    margin-left: auto;
  }

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

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

  .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:disabled {
    background: #eef3f6;
    border-color: #c9d7de;
    color: #78909c;
    cursor: not-allowed;
  }

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

  .danger {
    background: #fff2f2;
    border-color: #f2b4b4;
    color: #8e2020;
  }

  .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: min(360px, calc(100vw - 36px));
    padding: 10px;
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 20;
    display: grid;
    gap: 10px;
    border: 2px solid var(--line);
    border-radius: var(--radius);
    background: #ffffff;
    box-shadow: var(--shadow);
  }

  .menu-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
  }

  .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;
    accent-color: var(--blue);
  }

  .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.94);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    transform-origin: top center;
  }

  .stats-bar {
    display: grid;
    grid-template-columns: repeat(7, minmax(92px, 1fr));
    gap: 8px;
    margin-bottom: 14px;
  }

  .stat-card {
    min-height: 72px;
    padding: 8px 10px;
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 4px 8px;
    border: 2px solid var(--line);
    border-radius: var(--radius);
    background: #ffffff;
  }

  .stat-card strong {
    grid-column: 1 / 2;
    color: var(--muted);
    font-size: 0.82rem;
  }

  .stat-card span {
    grid-column: 1 / -1;
    font-size: clamp(1.15rem, 2.5vw, 1.75rem);
    line-height: 1;
    font-weight: 900;
  }

  .stat-toggle {
    width: 34px;
    height: 30px;
    min-height: 30px;
    padding: 0;
    border-radius: 6px;
    font-size: 0.75rem;
    line-height: 1;
  }

  .stat-toggle[aria-pressed="false"] + .stat-value {
    display: none;
  }

  .board-wrap {
    position: relative;
    min-height: 470px;
    overflow-x: hidden;
    overflow-y: auto;
    border: 2px solid #b9d2de;
    border-radius: var(--radius);
    background:
      linear-gradient(#edf5f9 1px, transparent 1px),
      linear-gradient(90deg, #edf5f9 1px, transparent 1px),
      #ffffff;
    background-size: 28px 28px;
  }

  .board {
    min-width: 0;
    width: 100%;
    height: 588px;
    position: relative;
    display: grid;
    grid-template-columns: repeat(var(--columns), minmax(0, 1fr));
    align-items: stretch;
    gap: clamp(4px, 1vw, 12px);
    padding: 22px clamp(6px, 1.5vw, 18px) 86px;
    transform: scale(var(--zoom));
    transform-origin: top left;
  }

  .column {
    min-width: 0;
    position: relative;
    display: grid;
    grid-template-rows: 1fr auto;
    border: 2px solid #d6e5ec;
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.78);
  }

  .tile-stack {
    position: relative;
    min-height: 430px;
    border-bottom: 5px solid #213344;
  }

  .stack-footer {
    min-height: 82px;
    display: grid;
    grid-template-columns: minmax(28px, 48px) minmax(28px, 48px);
    justify-content: center;
    align-content: center;
    gap: clamp(3px, 0.7vw, 8px);
    padding: 8px 4px;
  }

  .stack-step-button {
    width: 100%;
    min-width: 0;
    min-height: 38px;
    padding: 0;
    display: grid;
    place-items: center;
    font-size: 1.45rem;
    font-weight: 900;
  }

  .add-block-button {
    border-color: #9cc9df;
    background: #eaf7fc;
    color: #0c5c7e;
  }

  .remove-block-button {
    border-color: #f0b6a4;
    background: #fff2ee;
    color: #9b2c14;
  }

  .stack-step-button:hover {
    transform: none;
  }

  .stack-step-button:disabled {
    opacity: 0.38;
    cursor: not-allowed;
  }

  .stack-total {
    grid-column: 1 / -1;
    min-height: 30px;
    display: none;
    place-items: center;
    border: 2px solid var(--line);
    border-radius: var(--radius);
    background: #ffffff;
    font-size: 1.3rem;
    font-weight: 900;
  }

  .show-stack-totals .stack-total {
    display: grid;
  }

  .tile {
    width: min(64px, calc(100% - 10px));
    height: 30px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    display: grid;
    place-items: center;
    border: 3px solid rgba(16, 33, 49, 0.9);
    border-radius: 6px;
    color: #06111a;
    font-size: 1rem;
    font-weight: 900;
    box-shadow: 0 4px 8px rgba(15, 33, 49, 0.16);
    touch-action: none;
    cursor: grab;
    transition: bottom 0.18s ease, transform 0.12s ease, box-shadow 0.12s ease;
  }

  .tile:active {
    z-index: 30;
    transform: translateX(-50%) scale(1.06);
    box-shadow: 0 14px 24px rgba(15, 33, 49, 0.28);
  }

  .tile.dragging {
    opacity: 0.45;
  }

  .drag-ghost {
    width: 64px;
    height: 30px;
    position: fixed;
    z-index: 1000;
    pointer-events: none;
    border: 3px solid rgba(16, 33, 49, 0.9);
    border-radius: 6px;
    box-shadow: 0 16px 26px rgba(15, 33, 49, 0.3);
    transform: translate(-50%, -50%) scale(1.05);
  }

  .column.drop-target {
    border-color: var(--blue);
    background: #eaf7fc;
  }

  .mean-line,
  .median-line {
    display: none;
    position: absolute;
    left: 18px;
    right: 18px;
    height: 0;
    border-top: 4px solid var(--red);
    z-index: 12;
    pointer-events: none;
  }

  .median-line {
    border-top-color: var(--violet);
    border-top-style: dashed;
  }

  .mean-label,
  .median-label {
    position: absolute;
    right: 8px;
    top: -15px;
    padding: 4px 8px;
    border-radius: 999px;
    background: var(--red);
    color: #ffffff;
    font-size: 0.78rem;
    font-weight: 900;
    white-space: nowrap;
  }

  .median-label {
    background: var(--violet);
  }

  .show-mean .mean-line,
  .show-median .median-line {
    display: block;
  }

  .feedback {
    min-height: 58px;
    padding: 10px 12px;
    border: 2px solid var(--line);
    border-radius: var(--radius);
    background: #ffffff;
    color: var(--ink);
    font-weight: 800;
    line-height: 1.3;
  }

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

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

  .challenge-panel {
    display: none;
    margin-bottom: 14px;
    padding: 12px;
    border: 2px solid #9cc9df;
    border-radius: var(--radius);
    background: #eaf7fc;
    font-weight: 800;
    line-height: 1.35;
  }

  .challenge-feedback {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid rgba(12, 92, 126, 0.24);
    color: var(--muted);
  }

  .challenge-feedback.good {
    color: #22510f;
  }

  .challenge-feedback.try {
    color: #6a4300;
  }

  .challenge-panel.show {
    display: block;
  }

  .value-buttons {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: end;
  }

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

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

  @media (max-width: 980px) {
    .stats-bar {
      grid-template-columns: repeat(4, minmax(92px, 1fr));
    }
    .value-buttons {
      justify-content: start;
    }
  }

  @media (max-width: 720px) {
    .topbar {
      grid-template-columns: 1fr;
    }
    .top-actions {
      justify-content: start;
    }
    .stats-bar {
      grid-template-columns: repeat(2, minmax(92px, 1fr));
    }
  }

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

    body {
      background: #ffffff;
    }

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

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

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

    .board-wrap {
      min-height: 390px;
      overflow: visible;
      border-color: #000000;
    }

    .board {
      transform: none !important;
      min-width: 0;
      height: 430px;
    }

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