:root{
    --bg:#fff6cc;
    --ink:#111;
    --muted:#666;
    --panel:#fff0b3;
    --accent:#0b57d0;
    --danger:#b42318;
    --shadow: 0 10px 25px rgba(0,0,0,.08);
    --radius: 14px;

    --line:#111;
    --tick:#111;

    --hover: rgba(11,87,208,.08);
    --press: rgba(11,87,208,.14);
  }
  html,body{height:100%}
  body{
    margin:0;
    font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
    background:var(--bg);
    color:var(--ink);
  }
  .wrap{
    max-width: 1180px;
    margin: 20px auto 40px;
    padding: 0 16px;
  }
  .header{
    display:flex;
    gap:12px;
    align-items:flex-start;
    justify-content:space-between;
    flex-wrap:wrap;
  }
  h1{font-size: 20px; margin:0 0 6px; letter-spacing:.2px;}
  .sub{margin:0; color:var(--muted); font-size: 13px; line-height:1.35;}

  .controls{
    background: var(--panel);
    border: 1px solid #e5e7eb;
    border-radius: var(--radius);
    padding: 12px;
    box-shadow: var(--shadow);
    display:flex;
    gap:10px 12px;
    align-items:flex-end;
    flex-wrap:wrap;
    min-width: min(690px, 100%);
  }
  .field{
    display:flex;
    flex-direction:column;
    gap:6px;
    min-width: 130px;
  }
  label{font-size: 12px; color: var(--muted);}
  input[type="text"], input[type="number"], select{
    border: 1px solid #d1d5db;
    border-radius: 10px;
    padding: 10px 10px;
    font-size: 14px;
    outline: none;
    background:#fff;
  }
  input:focus, select:focus{
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(11,87,208,.12);
  }

  .btn{
    border: 1px solid #d1d5db;
    background:#fff;
    border-radius: 10px;
    padding: 10px 12px;
    font-size: 14px;
    cursor:pointer;
    transition: transform .03s ease, box-shadow .12s ease, border-color .12s ease, background .12s ease;
    user-select:none;
    white-space:nowrap;
  }
  .btn:hover{border-color:#b8c2cf}
  .btn:active{transform: translateY(1px)}
  .btn.primary{background: var(--accent); color:#fff; border-color: var(--accent);}
  .btn.primary:hover{filter:brightness(.98)}
  .btn:disabled{opacity:.55; cursor:not-allowed;}
  .btn.toggle.on{background: rgba(11,87,208,.10); border-color: rgba(11,87,208,.35);}
  .btn.danger{border-color:#f1b6b3}
  .btn.danger:hover{border-color:#e89b97}

  .hint{font-size:12px; color:var(--muted); margin-top:6px; flex-basis:100%;}
  .stage{
    margin-top: 16px;
    background:#fff;
    border:1px solid #e5e7eb;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px;
  }

  .breadcrumb{
    display:flex;
    gap:8px;
    align-items:center;
    flex-wrap:wrap;
    margin-bottom: 10px;
  }
  .crumb-btn{
    font-size: 12px;
    color: var(--muted);
    background: #f3f4f6;
    border: 1px solid #e5e7eb;
    padding: 6px 10px;
    border-radius: 999px;
    cursor:pointer;
    user-select:none;
    transition: background .12s ease, border-color .12s ease, transform .03s ease;
  }
  .crumb-btn:hover{border-color:#cdd3dc; background:#eef1f6}
  .crumb-btn:active{transform: translateY(1px)}
  .crumb-btn strong{color:var(--ink)}
  .crumb-btn.current{
    background: rgba(11,87,208,.10);
    border-color: rgba(11,87,208,.25);
    color:#1f3b74;
  }

  .canvas{width:100%; overflow-x:auto;}
  .msg{margin-top:10px; font-size: 12px; color:var(--muted);}
  .error{color:var(--danger);}

  .kbd{
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 11px;
    border: 1px solid #d1d5db;
    background:#fff;
    border-radius: 6px;
    padding: 2px 6px;
    color:#111;
  }

  /* SVG label pill */
  .label-pill{pointer-events:none; font-size:16px; fill: #111;}
  .pill-bg{fill: #fff; stroke: #d1d5db; stroke-width: 1; rx: 8; ry: 8;}

  /* helper overlay */
  .toast{
    font-size: 16px;

    position: fixed;
    left: 16px;
    bottom: 16px;
    max-width: min(460px, calc(100vw - 32px));
    background: #111;
    color: #fff;
    border-radius: 14px;
    padding: 12px 12px;
    box-shadow: 0 18px 30px rgba(0,0,0,.20);
    display:none;
    z-index: 999;
  }
  .toast.show{display:block}
  .toast .t-title{font-weight: 650; font-size: 15px; margin-bottom:4px;}
  .toast .t-body{font-size: 14px; opacity:.95; line-height:1.35;}
  .toast .t-actions{display:flex; gap:10px; margin-top:10px; justify-content:flex-end;}
  .toast button{
    min-height: 44px;

    border: 1px solid rgba(255,255,255,.30);
    background: transparent;
    color:#fff;
    padding: 8px 10px;
    border-radius: 10px;
    cursor:pointer;
    font-size: 12px;
  }
  .toast button:hover{border-color: rgba(255,255,255,.55)}

  /* Zoom animation */
  svg .level.zoom-in{
    animation: zoomInDown 380ms cubic-bezier(.2,.9,.2,1) both;
    transform-box: fill-box;
    transform-origin: var(--zoom-origin, center);
  }
  @keyframes zoomInDown{
    0%   { opacity: 0; transform: translateY(-120px) scaleX(.12); }
    60%  { opacity: 1; transform: translateY(-25px) scaleX(.55); }
    100% { opacity: 1; transform: translateY(0px) scaleX(1); }
  }

  svg .subticks{
    animation: subTickIn 260ms ease-out both;
    transform-origin: center;
  }
  @keyframes subTickIn{
    0%{ opacity: 0; transform: scaleY(.4); }
    100%{ opacity: 1; transform: scaleY(1); }
  }

  svg .focus-interval{
    opacity: 1;
    animation: focusPulse 520ms ease-out 1;
  }
  @keyframes focusPulse{
    0%   { opacity: 0; }
    35%  { opacity: 1; }
    100% { opacity: 1; }
  }


  /* Curved connector lines (faint/translucent) shown during zoom animation */
  svg .connectors path{
    fill: none;
    stroke: rgba(11,87,208,.18);
    stroke-width: 2;
    stroke-linecap: round;
  }
  svg .connectors path.thin{
    stroke-width: 1.6;
    stroke: rgba(11,87,208,.14);
  }
  svg .connectors{
    animation: connFade 420ms ease-out both;
  }
  @keyframes connFade{
    0%{ opacity: 0; transform: translateY(-6px); }
    100%{ opacity: 1; transform: translateY(0px); }
  }


  /* ===== Touch / Interactive board optimisations ===== */
  body{ -webkit-text-size-adjust: 100%; }
  .wrap{ max-width: 1400px; }
  .controls{
    gap: 14px 14px;
    padding: 16px;
  }
  .field{ min-width: 160px; }
  input[type="text"], input[type="number"], select{
    padding: 14px 12px;
    font-size: 18px;
    border-radius: 14px;
  }
  label{ font-size: 14px; }
  .btn{
    padding: 14px 16px;
    font-size: 18px;
    border-radius: 14px;
    min-height: 52px;
  }
  .btn.toggle.on{ background: rgba(11,87,208,.14); }
  .stage{ padding: 16px; }
  .breadcrumb{ gap: 10px; }
  .crumb-btn{
    font-size: 14px;
    padding: 10px 14px;
    min-height: 44px;
  }
  /* prevent accidental text selection on boards */
  .controls, .btn, .crumb-btn, svg, .stage{
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
  }
  /* Improve scroll + touch handling */
  .canvas{ -webkit-overflow-scrolling: touch; }
  svg{ touch-action: manipulation; }

  /* ===== Spire Maths visual system ===== */
  :root {
    --blue: #149bd7;
    --ink: #102131;
    --muted: #5c6b78;
    --line-soft: #d8e2ea;
    --shadow: 0 12px 32px rgba(15, 33, 49, 0.12);
    --radius: 8px;
    --touch: 48px;
    --line: #102131;
    --tick: #102131;
    --hover: rgba(20,155,215,.08);
    --press: rgba(20,155,215,.18);
  }

  *{box-sizing:border-box}
  html,body{min-height:100%; height:auto;}
  body{
    margin:0;
    color:var(--ink);
    background:#eef4f8;
    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);
    background:#ffffff;
    color:var(--ink);
    cursor:pointer;
    user-select:none;
    -webkit-tap-highlight-color:transparent;
  }
  button:focus-visible,
  select:focus-visible,
  input:focus-visible{
    outline:4px solid rgba(20,155,215,.35);
    outline-offset:2px;
  }
  .app{
    display:grid;
    grid-template-rows: auto auto 1fr;
    min-width:320px;
    min-height:100vh;
    min-height:100dvh;
  }
  .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;
  }

  .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;
  }.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;
  }
  .icon{
    width:24px;
    height:24px;
    display:block;
    pointer-events:none;
  }
  .controls{
    display:grid;
    grid-template-columns:minmax(460px,1fr) minmax(430px,1fr) auto;
    gap:12px;
    align-items:stretch;
    padding:12px 18px;
    background:#ffffff;
    border:0;
    border-radius:0;
    border-bottom:1px solid var(--line-soft);
    box-shadow:0 6px 14px rgba(15,33,49,.06);
    min-width:0;
    z-index:3;
  }
  .control-group{
    display:flex;
    align-items:end;
    gap:8px;
    min-width:0;
    flex-wrap:wrap;
  }
  .field{min-width:130px}
  .field.compact{min-width:84px; width:96px}
  .field.small{min-width:120px}
  .field.medium{min-width:170px}
  .field.wide{min-width:200px}
  label,
  .control-label{
    color:var(--muted);
    font-weight:700;
    font-size:.9rem;
  }
  input[type="text"], input[type="number"], select{
    min-height:var(--touch);
    border:1px solid var(--line-soft);
    border-radius:var(--radius);
    padding:0 12px;
    font-size:1rem;
    background:#fff;
    color:var(--ink);
    font-weight:700;
  }
  input:focus, select:focus{border-color:var(--blue); box-shadow:none}
  .btn{
    min-height:var(--touch);
    padding:0 14px;
    display:inline-flex;
    align-items:center;
    justify-content:center;
    gap:8px;
    border-color:var(--line-soft);
    border-radius:var(--radius);
    font-size:1rem;
    font-weight:800;
    background:#f8fbfd;
    white-space:nowrap;
  }
  .btn:hover,
  .btn.toggle.on{
    border-color:var(--blue);
    background:#e5f6fc;
  }
  .btn.primary{
    background:var(--blue);
    color:#06111a;
    border-color:#0d75a2;
  }
  .btn:disabled{opacity:.55; cursor:not-allowed}
  .btn.danger{border-color:#f2c4c0}
  .btn.danger:hover{border-color:#d66960; background:#fff2f1}
  .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-soft);
    border-radius:var(--radius);
    background:#f8fbfd;
    color:var(--ink);
    cursor:pointer;
    font-weight:800;
    list-style:none;
    white-space:nowrap;
  }
  .tool-menu summary::-webkit-details-marker{display:none}
  .tool-menu[open] summary,
  .tool-menu summary:hover{
    border-color:var(--blue);
    background:#e5f6fc;
  }
  .menu-panel{
    width:310px;
    padding:10px;
    position:absolute;
    top:calc(100% + 6px);
    right:0;
    z-index:10;
    display:flex;
    flex-wrap:wrap;
    gap:8px;
    align-items:end;
    border:2px solid var(--line-soft);
    border-radius:var(--radius);
    background:#ffffff;
    box-shadow:var(--shadow);
  }
  .menu-panel .field{flex:1 1 132px}
  .menu-panel .btn{flex:1 1 auto}
  .workspace{
    min-height:0;
    overflow:auto;
    overscroll-behavior:contain;
    padding:18px;
    background:
      linear-gradient(#dce8f0 1px, transparent 1px),
      linear-gradient(90deg, #dce8f0 1px, transparent 1px);
    background-size:34px 34px;
  }
  .stage{
    max-width:1180px;
    margin:0 auto;
    background:rgba(255,255,255,.92);
    border:2px solid #b9d7e5;
    border-radius:var(--radius);
    box-shadow:var(--shadow);
    padding:12px;
  }
  .hint{
    display:none;
    min-height:var(--touch);
    align-items:center;
    margin-top:0;
    color:var(--muted);
    font-size:.92rem;
    font-weight:700;
    padding:0 12px;
    border:1px solid var(--line-soft);
    border-radius:var(--radius);
    background:#f8fbfd;
  }
  .breadcrumb{margin-bottom:10px}
  .crumb-btn{
    min-height:40px;
    padding:0 12px;
    border:1px solid var(--line-soft);
    border-radius:var(--radius);
    background:#f8fbfd;
    color:var(--muted);
    font-size:.88rem;
    font-weight:700;
  }
  .crumb-btn:hover{border-color:var(--blue); background:#e5f6fc}
  .crumb-btn.current{
    background:var(--blue);
    border-color:#0d75a2;
    color:#06111a;
  }
  .canvas{
    width:100%;
    overflow-x:auto;
    background:#ffffff;
    border:1px solid var(--line-soft);
    border-radius:var(--radius);
  }
  .msg{
    margin-top:10px;
    font-size:.92rem;
    color:var(--muted);
    font-weight:700;
  }
  .kbd{border-color:var(--line-soft); color:var(--ink)}
  .label-pill{fill:var(--ink); font-weight:800}
  .pill-bg{fill:#fff; stroke:#b9d7e5}
  svg .connectors path{stroke:rgba(20,155,215,.24)}
  svg .connectors path.thin{stroke:rgba(20,155,215,.18)}
  .toast{
    background:#071018;
    border:2px solid #263d51;
    border-radius:var(--radius);
  }
  .toast .t-title{font-weight:800}
  .toast button{
    background:#122436;
    border-radius:var(--radius);
    font-weight:800;
  }
  .side-panel{
    display:grid;
    grid-template-columns:minmax(280px,1fr) auto;
    align-items:center;
    gap:12px;
    padding:12px 18px;
    background:#ffffff;
    border-top:1px solid var(--line-soft);
    box-shadow:0 -6px 14px rgba(15,33,49,.06);
    z-index:4;
  }
  .teacher-note{
    min-height:48px;
    display:flex;
    align-items:center;
    color:var(--muted);
    font-size:.95rem;
    font-weight:700;
    min-width:0;
  }
  .copyright{
    white-space:nowrap;
    color:var(--muted);
    font-size:.84rem;
    font-weight:700;
  }
  .instructions-dialog{
    width:min(820px, calc(100vw - 32px));
    max-height:min(780px, 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,.32);
  }
  .instructions-dialog::backdrop{background:rgba(6,17,26,.5)}
  .instructions-header{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:12px;
    padding:14px 16px;
    background:#071018;
    color:#ffffff;
  }
  .instructions-header h2{
    margin:0;
    font-size:1.25rem;
    line-height:1.15;
  }
  .instructions-body{
    max-height:calc(min(780px, 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}
  .instructions-body strong{color:#06111a}
  @media (max-width:980px){
    .app{height:auto}
    .topbar,
    .controls,
    .side-panel{grid-template-columns:1fr}
    .top-actions,
    .control-group{justify-content:start}
    .brand img{width:116px}
.workspace{padding:12px}
    .side-panel{position:sticky; bottom:0}
    .menu-panel{
      left:0;
      right:auto;
      width:min(310px, calc(100vw - 24px));
    }
  }
  @media print{
    body{background:#ffffff}
    .topbar,
    .brand-panel,
    .controls,
    .side-panel,
    .toast,
    .breadcrumb,
    .msg{display:none}
    .app{display:block; min-height:0}
    .workspace{overflow:visible; padding:0; background:#ffffff}
    .stage{
      width:100%;
      max-width:none;
      margin:0;
      padding:0;
      box-shadow:none;
      border:0;
      background:#ffffff;
    }
    .canvas{
      overflow:auto;
      overscroll-behavior:contain;
      touch-action:none;
      border:0;
      background:#ffffff;
    }
    svg{
      width:100%;
      height:auto;
      display:block;
    }
  }

  .wrap{
    max-width:none;
    min-height:100vh;
    min-height:100dvh;
    margin:0;
    padding:0;
    display:grid;
    grid-template-rows: auto auto 1fr;
    background:
      linear-gradient(#dce8f0 1px, transparent 1px),
      linear-gradient(90deg, #dce8f0 1px, transparent 1px);
    background-size:34px 34px;
  }

  .header{display:contents}
  .header > div:not(.brand-panel):not(.controls){display:none}
  .brand-panel{
    display:grid;
    grid-template-columns:minmax(190px,1fr) auto;
    align-items:center;
    gap:16px;
    padding:12px 18px;
    background:#071018;
    color:#ffffff;
  }

  .header .controls{
    display:flex;
    align-items:end;
    justify-content:start;
    gap:12px;
    flex-wrap:wrap;
  }
  .stage{align-self:start; width:calc(100% - 36px); margin:18px auto}

  @media (max-width:980px){
    .brand-panel,
    .header .controls,
    .stage{width:calc(100% - 24px); margin:12px auto}
  }

  @media print{
    @page{size:landscape; margin:14mm}

    body,
    .wrap{
      background:#ffffff !important;
    }

    .wrap{
      display:block;
      min-height:0;
    }

    .header{
      display:block;
    }

    .brand-panel{
      display:block !important;
      padding:0 0 12px;
      background:#ffffff;
      color:var(--ink);
      box-shadow:none;
      border:0;
    }

    .brand{
      display:block;
    }

    .brand img{
      width:150px !important;
    }

    .title-block,
    .top-actions,
    .controls,
    .instructions-dialog,
    .breadcrumb,
    .msg,
    .toast,
    .teacher-note{
      display:none !important;
    }

    .stage{
      width:100%;
      max-width:none;
      margin:0;
      padding:0;
      border:0;
      box-shadow:none;
      background:#ffffff;
    }

    .canvas{
      overflow:visible;
      border:0;
      background:#ffffff;
    }

    svg{
      width:100%;
      height:auto;
      display:block;
    }

    .copyright{
      color:var(--muted);
      font-size:.84rem;
      font-weight:700;
    }
  }
