/* Give the paper priority before the fixed desktop controls squeeze it. */
@media (min-width:1051px) {
  #draw-view .workspace { grid-template-columns:minmax(0,1fr) clamp(260px,22vw,320px); }
  #draw-view .tools-panel { grid-template-columns:clamp(64px,6vw,80px) minmax(0,1fr); }
  #draw-view .tool-options-wrap { padding:14px 10px; }
  #draw-view .color-palette { gap:6px; }
}

/* Account for height too: a short, wide window can keep its history tab beside
   the paper; a tall window benefits from reclaiming that side column. */
@media (max-width:calc(150vh + 220px)), (max-width:1050px) {
  #draw-view .drawing-area { grid-template-columns:82px minmax(0,1fr); gap:8px; }
  /* The history tab attaches above the paper, freeing its reserved side column. */
  #draw-view .canvas-stage { padding:62px 6px 6px; }
  #draw-view .history-controls {
    top:auto; bottom:calc(100% - 2.5px); left:auto; right:0;
    width:152px; flex-direction:row; gap:8px; padding:6px 10px 10px;
    border-radius:12px 12px 0 0;
  }
  #draw-view .history-controls::before { border:5px solid #111; border-bottom:0; border-radius:14.5px 14.5px 0 0; }
  #draw-view .history-button { flex:1; min-height:44px; padding:4px; gap:3px; font-size:14px; }
  #draw-view #score-timer {
    --timer-clock-height:48px;
    --timer-scale-min-height:140px;
    --timer-track-left:6px;
    --timer-track-width:23px;
    --timer-stone-height:38px;
    --timer-bulb-size:44px;
    padding:8px; border-radius:14px;
  }
  #draw-view .timer-clock { width:34px; height:38px; }
  #draw-view .timer-track { border-radius:11px 11px 0 0; }
  #draw-view .timer-track::after { border-width:7px; border-bottom:0; border-radius:15px 15px 0 0; }
  #draw-view .timer-tick { left:28px; width:11px; }
  #draw-view .time-medal { left:36px; width:30px; height:42px; }
}

/* A bottom dock gives narrow desktop/tablet windows their width back. */
@media (max-width:1050px) {
  body[data-route=canvas][data-stage=draw] main { display:grid; overflow-y:auto; overflow-x:hidden; overscroll-behavior-y:contain; }
  #draw-view { grid-template-rows:auto minmax(0,1fr); padding-bottom:4px; }
  #draw-view .workspace { grid-template-columns:minmax(0,1fr); grid-template-rows:minmax(350px,1fr) auto; gap:12px; transform:none; }
  #draw-view .drawing-area { min-height:350px; }
  #draw-view .drawing-area>#score-timer { transform:translateX(calc(var(--canvas-leading-space,0px) * .5)); }
  #draw-view .canvas-panel { align-self:stretch; grid-template-rows:minmax(0,1fr); }
  #draw-view .canvas-stage { width:100%; aspect-ratio:auto; place-items:center; }
  #draw-view .studio-sidebar { grid-template-columns:minmax(0,1fr) 180px; grid-template-rows:auto auto; gap:10px; margin:0; padding:0; overflow:visible; }
  #draw-view .tools-panel { grid-column:1; grid-row:1/3; grid-template-columns:minmax(0,1fr); }
  #draw-view .tool-rail { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:6px; padding:6px; border-right:0; border-bottom:1px solid var(--line); }
  #draw-view .rail-label,#draw-view .rail-divider,#draw-view .options-heading { display:none; }
  #draw-view .tool-button { flex-direction:row; justify-content:center; min-height:40px; padding:4px; gap:6px; font-size:16px; }
  #draw-view .tool-button svg { width:20px; height:20px; }
  #draw-view .tool-options-wrap { padding:8px 12px 10px; }
  #draw-view #tool-options { display:grid; grid-template-columns:minmax(0,1fr) minmax(0,1.2fr) minmax(0,1.8fr); align-items:start; gap:14px; }
  #draw-view .option-group,#draw-view .option-group+.option-group { width:100%; margin:0; }
  #draw-view .option-label { font-size:16px; margin-bottom:6px; }
  #draw-view .option-label>span,#draw-view .option-label>output { font-size:14px; }
  #draw-view .size-buttons { gap:4px; }
  #draw-view .size-buttons button { width:28px; height:32px; }
  #draw-view .opacity-buttons { gap:4px; }
  #draw-view .opacity-buttons button { height:32px; }
  #draw-view #color-options { grid-column:auto; }
  #draw-view .color-palette { grid-template-columns:repeat(9,minmax(0,1fr)); gap:6px; }
  #draw-view .palette-swatch { width:24px; height:24px; max-width:100%; max-height:none; justify-self:center; }
  #draw-view .drawing-actions { grid-column:2; grid-row:1; }
  #draw-view .drawing-actions .button { width:100%; min-width:0; min-height:48px; padding:8px; font-size:16px; }
  #draw-view .play-reminder { grid-column:2; grid-row:2; display:block; padding:10px; align-self:stretch; }
  #draw-view .play-reminder h2 { font-size:18px; }
  #draw-view .play-reminder p { margin-top:4px; font-size:15px; line-height:1.3; }
}

/* On smaller tablets, keep the controls roomy and let the page scroll vertically. */
@media (max-width:850px) {
  body[data-route=canvas][data-stage=draw] main { display:block; }
  #draw-view { grid-template-rows:auto auto; padding-bottom:max(16px,env(safe-area-inset-bottom)); }
  #draw-view .workspace { grid-template-rows:auto auto; }
  #draw-view .canvas-panel { container-type:inline-size; }
  #draw-view .canvas-stage { height:calc((100cqw - 12px) * 2 / 3 + 68px); }
  #draw-view .studio-sidebar { grid-template-columns:repeat(2,minmax(0,1fr)); }
  #draw-view .tools-panel { grid-column:1/-1; grid-row:1; }
  #draw-view .drawing-actions { grid-column:1; grid-row:2; }
  #draw-view .play-reminder { grid-column:2; }
  #draw-view .tool-button { min-height:44px; }
}

/* A horizontal timer leaves the entire phone width available for drawing. */
@media (max-width:640px) {
  #draw-view .drawing-area { grid-template-columns:minmax(0,1fr); grid-template-rows:86px auto; min-height:0; gap:8px; }
  #draw-view #score-timer { grid-template-columns:32px minmax(0,1fr) 40px; grid-template-rows:minmax(0,1fr); gap:8px; min-height:0; padding:8px 12px; }
  #draw-view .drawing-area>#score-timer { transform:none; }
  #draw-view .timer-clock { width:30px; height:36px; align-self:start; }
  #draw-view .timer-scale { min-height:0; }
  #draw-view .timer-track { top:12px; bottom:auto; left:0; right:0; width:auto; height:18px; border-radius:11px 0 0 11px; }
  #draw-view .timer-track::after { inset:-4px 0 -4px -4px; border:7px solid var(--timer-ink); border-right:0; border-radius:15px 0 0 15px; }
  #draw-view .timer-fill { background:linear-gradient(to right,#81c86c,#ffda59); clip-path:inset(0 0 0 var(--elapsed,0%)); }
  #draw-view .timer-tick { left:var(--at); top:27px; width:3px; height:8px; transform:translateX(-50%) rotate(-3deg); }
  #draw-view .time-medal { left:var(--at); top:49px; width:26px; height:34px; transform:translate(-50%,-50%) rotate(5deg); }
  #draw-view .time-medal.is-current { transform:translate(-50%,-50%) scale(1.15); }
  #draw-view .timer-stone .time-medal { left:20px; top:49px; }
  #draw-view .timer-reservoir { width:40px; height:72px; top:-15px; left:-16px; transform:rotate(-90deg); }
  #draw-view .timer-infinity { transform:rotate(90deg); transform-origin:27px 77px; }
  #draw-view .studio-sidebar { grid-template-columns:minmax(0,1fr); grid-template-rows:auto auto auto; }
  #draw-view .drawing-actions { grid-column:1; grid-row:2; }
  #draw-view .play-reminder { grid-column:1; grid-row:3; }
  #draw-view #tool-options { grid-template-columns:repeat(2,minmax(0,1fr)); gap:16px; }
  #draw-view #color-options { grid-column:1/-1; }
  #draw-view .color-palette { grid-template-columns:repeat(9,minmax(0,1fr)); }
  #draw-view .palette-swatch { width:28px; height:28px; }
  #draw-view .size-buttons button { width:32px; height:40px; }
  #draw-view .opacity-buttons button { height:40px; }
}
