:root {
  --bg: #111418;
  --panel: #1b2027;
  --panel2: #232a33;
  --line: #2f3742;
  --text: #e9eef4;
  --muted: #94a0ae;
  --accent: #3b82f6;
  --accent2: #2563eb;
  --ok: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --notes-size: 22px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
.hidden { display: none !important; }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, "SF Mono", Menlo, monospace; }
.spacer { flex: 1; }

#app {
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* never wider than the screen, even with long names */
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  height: 100dvh;
  padding: calc(8px + var(--safe-top)) 10px calc(8px + var(--safe-bottom));
  gap: 8px;
}

/* Top bar */
#topbar { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 14px; font-size: 15px; white-space: nowrap;
}
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.warn { background: var(--warn); }
.dot.off { background: var(--bad); }
.deck {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; background: none; border: none;
  color: var(--muted); padding: 6px 4px; text-align: left; font-size: 15px;
}
.deck #deckName { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.deck .caret { color: var(--text); flex: none; }
.deck:disabled { cursor: default; }
.slidenum { font-size: 22px; font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
#timer { font-size: 18px; }
#timer.running { border-color: var(--ok); color: var(--ok); }
.clock { color: var(--muted); font-size: 18px; }
.recchip { gap: 6px; padding: 8px 11px; }
.recdot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; }
.recchip.rec { border-color: var(--bad); color: var(--bad); }
.recchip.rec .recdot { background: var(--bad); animation: recpulse 1.4s ease-in-out infinite; }
.recchip.paused { border-color: var(--warn); color: var(--warn); }
.recchip.paused .recdot { background: var(--warn); border-radius: 2px; }
.recchip.saving .recdot { background: var(--warn); }
@keyframes recpulse { 50% { opacity: .25; } }
.recstatus { font-size: 26px; font-weight: 700; margin: 10px 0; font-variant-numeric: tabular-nums; }
.recstatus.rec { color: var(--bad); }
.recstatus.paused { color: var(--warn); }
.recstart { width: 100%; color: #fff; background: var(--bad); border-color: var(--bad); }
.recrow > .big { flex: 1; }
.iconbtn {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  min-width: 44px; height: 44px; font-size: 20px;
}
.iconbtn.small { min-width: 40px; height: 34px; font-size: 15px; }

/* Main */
#main {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr);
  gap: 10px;
  min-height: 0;
}
#slides { display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.slidebox {
  position: relative; background: #000; border-radius: 10px; overflow: hidden;
  border: 1px solid var(--line);
}
.slidebox img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
.slidebox img:not([src]), .slidebox img[src=""] { display: none; }
#current { flex: 1 1 auto; min-height: 120px; touch-action: pan-y; }
.slidemsg {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 16px; color: var(--muted); font-size: 18px; pointer-events: none;
}
.slidemsg.big { font-size: 64px; font-weight: 700; color: #3a4350; }
.badge {
  position: absolute; top: 10px; left: 10px; background: var(--warn); color: #000;
  border-radius: 6px; padding: 4px 10px; font-weight: 600; font-size: 14px;
}
.tag {
  position: absolute; top: 6px; left: 6px; background: rgba(0,0,0,.65); color: #fff;
  border-radius: 5px; padding: 1px 7px; font-size: 12px;
}
#toolrow { display: flex; gap: 10px; flex: 0 0 auto; height: 30%; min-height: 110px; }
#next { aspect-ratio: 16 / 9; height: 100%; flex: none; max-width: 50%; }
#next .slidemsg.big { font-size: 36px; }
#tools { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-content: stretch; }
.tool {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  font-size: 15px; padding: 6px; min-height: 44px;
}
.tool.on { background: var(--warn); color: #000; border-color: var(--warn); }
.tool:disabled, .nav:disabled { opacity: .35; }
.tool { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Notes */
#notesPane {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
}
#notesHead { display: flex; align-items: center; gap: 6px; padding: 6px 8px 6px 14px; border-bottom: 1px solid var(--line); color: var(--muted); }
.notes {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 16px 40px;
  font-size: var(--notes-size); line-height: 1.45; white-space: pre-wrap; word-wrap: break-word;
}
.notes.empty { color: var(--muted); font-style: italic; }

/* Bottom navigation */
#navbar { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; height: 92px; }
.nav {
  border: none; border-radius: 14px; font-size: 30px; font-weight: 700;
  background: var(--panel2); transition: transform .06s, filter .06s;
}
.nav.next { background: var(--accent); color: #fff; }
.nav:active, .tool:active { transform: scale(.97); filter: brightness(1.2); }
.nav.busy { filter: brightness(.8); }

/* Portrait: stack slides above notes */
@media (orientation: portrait) {
  #main { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  #current { flex: none; aspect-ratio: 16 / 9; }
  #toolrow { height: 120px; }
  #slides { min-height: auto; }
  #tools { grid-template-columns: 1fr 1fr 1fr; }
}

@media (max-width: 700px) {
  .clock { display: none; }
  #navbar { height: 76px; }
}

/* Phones */
@media (max-width: 600px) {
  #app { padding-left: 8px; padding-right: 8px; gap: 6px; }
  #topbar { gap: 6px; }
  #connLabel { display: none; }
  .chip { padding: 8px 10px; }
  .slidenum { font-size: 18px; }
  #timer { font-size: 15px; }
  #toolrow { height: auto; min-height: 0; gap: 8px; }
  #next { width: 42%; max-width: none; height: auto; }
  #tools { grid-template-columns: 1fr 1fr !important; gap: 6px; }
  .tool { font-size: 14px; min-height: 36px; padding: 4px 6px; }
  .panel #grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
  .panel { padding: 16px; }
  .panel.wide { padding: 14px; }
}

/* Overlays */
.overlay {
  position: fixed; inset: 0; background: rgba(8, 10, 13, .92); z-index: 10;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 24px; width: min(460px, 100%); max-height: 100%; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
}
.panel.wide { width: min(1000px, 100%); height: 100%; }
.panel h1, .panel h2 { margin: 0; }
.panelhead { display: flex; align-items: center; justify-content: space-between; }
.sub { margin: 0 0 6px; color: var(--muted); }
.hint { color: var(--muted); font-size: 14px; margin: 0; }
.hint code { color: var(--text); word-break: break-all; -webkit-user-select: all; user-select: all; }
.error { color: var(--bad); margin: 0; min-height: 1em; }
.big {
  min-height: 54px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel2); font-size: 18px; font-weight: 600;
}
.big.primary { background: var(--accent); border-color: var(--accent2); color: #fff; }
.big.danger { color: var(--bad); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 40px; }
.row select { font-size: 16px; padding: 6px; border-radius: 8px; background: var(--panel2); color: var(--text); border: 1px solid var(--line); }
.row input[type=checkbox] { width: 22px; height: 22px; order: 2; }
#pinInput {
  font-size: 40px; text-align: center; letter-spacing: 16px; padding: 10px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text);
  -webkit-user-select: text; user-select: text;
}

#grid {
  flex: 1; min-height: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  grid-auto-rows: max-content; align-content: start; gap: 10px; overflow-y: auto; padding: 2px;
}
.gcell { aspect-ratio: 16 / 9; width: 100%; position: relative; padding: 0; background: #000; border: 2px solid var(--line); border-radius: 8px; overflow: hidden; }
.gsec { grid-column: 1 / -1; color: var(--muted); font-weight: 600; font-size: 14px; text-transform: uppercase; letter-spacing: .04em; padding: 8px 2px 0; }
.gcell.cur { border-color: var(--accent); }
.gcell.hid { opacity: .45; }
.gcell img { width: 100%; height: 100%; object-fit: contain; display: block; }
.gcell .num { position: absolute; bottom: 4px; right: 6px; background: rgba(0,0,0,.7); border-radius: 5px; padding: 0 6px; font-size: 14px; }
.gcell .ph { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 30px; color: #3a4350; font-weight: 700; }

/* Laser pointer, pen and media controls on the current slide */
#current.drawing { touch-action: none; cursor: crosshair; }
html.drawmode, html.drawmode * { -webkit-user-select: none !important; user-select: none !important; -webkit-touch-callout: none !important; }
#inkCanvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#spotShade { position: absolute; pointer-events: none; }
#zoomRect { position: absolute; pointer-events: none; border: 2px dashed #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.6), inset 0 0 0 1px rgba(0,0,0,.6); background: rgba(255,255,255,.08); }
.zoomlevel { min-width: 36px; text-align: center; color: var(--text); }
.shape.wide { width: auto; padding: 0 8px; }
.zoomhint { flex-basis: 100%; font-size: 12px; color: #ffd60a; text-align: right; }
#spotBtn { color: #ffe08a; }
#spotBtn.on { color: #111; background: #ffd60a; border-color: #ffd60a; }
.pencolors .plabel { color: #dfe6ee; font-size: 13px; padding: 0 6px; }
.spotsize { font-weight: 700; font-size: 13px; }
#laserDot {
  position: absolute; width: 18px; height: 18px; margin: -9px 0 0 -9px; border-radius: 50%; pointer-events: none;
  background: #ff2d2d; box-shadow: 0 0 0 8px rgba(255, 45, 45, .3), 0 0 18px 6px rgba(255, 45, 45, .6);
}
.slidetools { position: absolute; top: 8px; right: 8px; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.toolrow1 { display: flex; gap: 6px; }
.stool {
  width: 42px; height: 42px; border-radius: 50%; border: 1px solid rgba(255,255,255,.25);
  background: rgba(17, 20, 24, .72); color: #fff; font-size: 18px; padding: 0;
}
.stool.on { background: var(--accent); border-color: var(--accent); }
#laserBtn { color: #ff4d4d; }
#laserBtn.on { color: #fff; background: #e11d1d; border-color: #e11d1d; }
.pencolors { display: flex; flex-wrap: wrap; justify-content: flex-end; align-items: center; gap: 5px; background: rgba(17, 20, 24, .78); padding: 6px; border-radius: 21px; max-width: 100%; }
.pencolors .sep { width: 1px; height: 22px; background: rgba(255,255,255,.25); margin: 0 2px; }
.shape { width: 32px; height: 32px; border-radius: 50%; border: none; background: transparent; color: #fff; font-size: 16px; padding: 0; }
.shape.on { background: var(--accent); }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; padding: 0; box-shadow: 0 0 0 1px rgba(255,255,255,.4); }
.swatch.on { border-color: #fff; }
.mediabars { position: absolute; left: 8px; right: 8px; bottom: 8px; display: flex; flex-direction: column; gap: 6px; pointer-events: none; }
.mediabar {
  display: flex; align-items: center; gap: 6px; padding: 6px; border-radius: 12px; pointer-events: auto;
  background: rgba(17, 20, 24, .82); border: 1px solid rgba(255,255,255,.15); width: max-content; max-width: 100%;
}
.mbtn { min-width: 44px; height: 38px; border-radius: 9px; border: none; background: rgba(255,255,255,.12); color: #fff; font-size: 15px; font-weight: 600; }
.mbtn.play { background: var(--accent); min-width: 54px; }
.minfo { padding: 0 8px; font-variant-numeric: tabular-nums; color: #dfe6ee; font-size: 14px; white-space: nowrap; }

/* Formatted notes */
.notes .np { position: relative; margin: 0 0 .35em; white-space: pre-wrap; }
.notes .bullet { position: absolute; margin-left: -1.2em; color: var(--muted); }
#notesHead .section { color: var(--text); opacity: .75; font-size: 14px; margin-left: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#notesHead .section:not(:empty)::before { content: "· "; }

/* Presentation picker */
.preslist { display: flex; flex-direction: column; gap: 8px; }
.presitem {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; padding: 12px 14px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--panel2); font-size: 16px; word-break: break-word;
}
.presitem small { color: var(--muted); font-size: 13px; }
.presitem.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }

/* Short fade when a new slide image appears */
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
.slidebox img.fade { animation: fadein .18s ease-out; }

/* Settings extras */
.panel h3 { margin: 10px 0 0; font-size: 15px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.footrow { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 4px; }
.linkbtn { background: none; border: none; color: var(--muted); text-decoration: underline; font-size: 14px; padding: 6px 0; }
.big:disabled { opacity: .4; }

/* Activity panel */
.stats {
  display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px;
  font-size: 14px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
.stats .k { color: var(--muted); }
.stats .v { font-variant-numeric: tabular-nums; word-break: break-word; }
.actbar { display: flex; align-items: center; gap: 8px; }
.iconbtn.wide { padding: 0 12px; }
.actlog {
  flex: 1; min-height: 160px; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
  font-size: 12.5px; line-height: 1.5; -webkit-user-select: text; user-select: text;
}
.actlog .t { color: var(--muted); }
.actlog .error { color: #fca5a5; }

.toast {
  position: fixed; left: 50%; bottom: calc(110px + var(--safe-bottom)); transform: translateX(-50%);
  background: #000c; border: 1px solid var(--line); padding: 10px 18px; border-radius: 999px; z-index: 20;
  font-size: 15px; pointer-events: none;
}

/* Phones: smaller controls on the slide (after the base rules above) */
@media (max-width: 600px) {
  .slidetools { top: 5px; right: 5px; gap: 4px; }
  .stool { width: 32px; height: 32px; font-size: 14px; }
  .pencolors { padding: 4px; gap: 4px; }
  .swatch { width: 20px; height: 20px; }
  .mediabars { left: 5px; right: 5px; bottom: 5px; }
  .mediabar { padding: 4px; gap: 4px; border-radius: 9px; }
  .mbtn { min-width: 34px; height: 28px; font-size: 13px; border-radius: 7px; }
  .mbtn.play { min-width: 40px; }
  .minfo { font-size: 12px; padding: 0 4px; }
}

/* Whiteboard: a blank page instead of the slide */
#current.wb { background: #fff; }
#current.wb #currentImg, #current.wb .slidemsg { visibility: hidden; }

/* Big slide: full screen for drawing (and automatically on phones in landscape) */
.bignav, .bigclose, body:not(.bigslide) .rotatehint { display: none; }
body.bigslide #current {
  position: fixed; inset: 0; z-index: 8; border: none; border-radius: 0; min-height: 0; aspect-ratio: auto;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
}
body.bigslide #current.wb { background: #fff; }
body.bigslide .bignav {
  display: flex; align-items: center; gap: 8px; position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(10px + env(safe-area-inset-bottom, 0px)); z-index: 2;
}
body.bigslide .bigbtn {
  width: 64px; height: 52px; border-radius: 14px; border: 1px solid rgba(255,255,255,.2);
  background: rgba(17, 20, 24, .72); color: #fff; font-size: 30px; font-weight: 700; padding: 0;
}
body.bigslide .bignum { min-width: 64px; text-align: center; color: #fff; font-weight: 700; text-shadow: 0 1px 3px #000; font-variant-numeric: tabular-nums; }
body.bigslide .bigclose {
  display: block; position: absolute; top: calc(8px + env(safe-area-inset-top, 0px)); left: calc(8px + env(safe-area-inset-left, 0px));
  width: 42px; height: 42px; border-radius: 50%; border: 1px solid rgba(255,255,255,.25); background: rgba(17, 20, 24, .72); color: #fff; font-size: 18px; z-index: 2;
}
body.bigslide .slidetools { top: calc(8px + env(safe-area-inset-top, 0px)); right: calc(8px + env(safe-area-inset-right, 0px)); }
body.bigslide .mediabars { bottom: calc(72px + env(safe-area-inset-bottom, 0px)); }
body.bigslide .stool { width: 42px; height: 42px; font-size: 18px; }
body.bigslide .swatch { width: 26px; height: 26px; }
.rotatehint {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 3; pointer-events: none;
  background: rgba(0,0,0,.8); color: #fff; padding: 12px 18px; border-radius: 12px; font-size: 15px; text-align: center;
}
#fsBtn.on { background: var(--accent); border-color: var(--accent); }
#wbBtn.on { background: #fff; color: #111; }

/* Delivery alert */
.alertbar {
  position: fixed; left: 12px; right: 12px; top: calc(10px + env(safe-area-inset-top, 0px)); z-index: 25;
  background: #b91c1c; color: #fff; border-radius: 12px; padding: 14px 16px; font-weight: 600; font-size: 16px;
  box-shadow: 0 6px 24px rgba(0,0,0,.5); text-align: center; animation: shake .4s;
}
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* Soundboard */
.soundgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.soundbtn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; min-height: 92px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--panel2); font-size: 15px; font-weight: 600; padding: 8px;
}
.soundbtn .sicon { font-size: 34px; line-height: 1; }
.soundbtn:active { transform: scale(.96); }
.soundbtn.played { animation: played .5s; }
@keyframes played { 0% { background: var(--accent); } 100% { background: var(--panel2); } }
.vol { display: flex; align-items: center; gap: 8px; flex: 1; justify-content: flex-end; }
.vol input { width: 100%; max-width: 220px; }
#soundStop { min-width: 110px; }


/* Big slide on screens wider than the slide (phones sideways): tools in the black bars beside it */
@media (min-aspect-ratio: 16/10) {
  body.bigslide .slidetools { flex-direction: row-reverse; align-items: flex-start; }
  body.bigslide .toolrow1 { flex-direction: column; }
  body.bigslide .pencolors {
    position: fixed; left: calc(8px + env(safe-area-inset-left, 0px)); top: 50%; transform: translateY(-50%);
    flex-direction: column; flex-wrap: nowrap; gap: 4px; padding: 5px; border-radius: 18px;
  }
  body.bigslide .pencolors .sep { width: 22px; height: 1px; margin: 2px 0; }
  body.bigslide .pencolors { gap: 3px; }
  body.bigslide .pencolors .shape { width: 28px; height: 28px; font-size: 14px; }
  body.bigslide .pencolors .swatch { width: 22px; height: 22px; }
  body.bigslide .bigclose { top: auto; left: auto; bottom: calc(10px + env(safe-area-inset-bottom, 0px)); right: calc(8px + env(safe-area-inset-right, 0px)); }
}

/* The spotlight sizes are a short row; keep it horizontal there too */
@media (min-aspect-ratio: 16/10) {
  body.bigslide #spotSizesBox { flex-direction: row; top: auto; bottom: calc(10px + env(safe-area-inset-bottom, 0px)); transform: none; }
  body.bigslide #spotSizesBox .plabel { display: none; }
}

/* Zoom controls: a short row, like the spotlight sizes */
@media (min-aspect-ratio: 16/10) {
  body.bigslide #zoomBox { flex-direction: row; flex-wrap: wrap; top: calc(8px + env(safe-area-inset-top, 0px)); transform: none; max-width: 46vw; }
  body.bigslide #zoomBox > .plabel:first-child { display: none; }
  body.bigslide .pencolors .shape.wide { width: auto; }
}
#recInfo { white-space: pre-line; margin-top: 4px; }
#micStatus { white-space: pre-line; margin-top: 2px; }
