:root {
  --bg: #0d0f12;
  --panel: #171a20;
  --panel-2: #20242c;
  --border: #2c313b;
  --text: #eef0f3;
  --muted: #8b93a1;
  --green: #2ecc71;
  --yellow: #f5c518;
  --red: #ff4d4d;
  --blue: #4da3ff;
  --radius: 10px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); }
button, input, textarea, select { font: inherit; color: inherit; }

.tabular { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

.dot {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  background: var(--green); flex: none;
}
.dot.offline { background: var(--red); animation: blink 1s steps(2) infinite; }

@keyframes blink { 50% { opacity: 0.2; } }
/* Smooth, compositor-only animations (opacity) for the stage screen */
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }
@keyframes flash-overlay { 0%, 100% { opacity: 0; } 50% { opacity: 1; } }

/* ---------- Buttons & forms ---------- */
.btn {
  border: 1px solid var(--border); background: var(--panel-2);
  padding: 0.55em 0.9em; border-radius: 8px; cursor: pointer; white-space: nowrap;
  flex-shrink: 0; /* next to an input, the input gives way, not the label */
}
a.btn { color: inherit; text-decoration: none; display: inline-block; }
.btn.primary, a.btn.primary { color: #06210f; }
.btn:hover { border-color: #49505d; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--green); color: #06210f; border-color: transparent; font-weight: 600; }
.btn.warn { background: var(--yellow); color: #2a2100; border-color: transparent; font-weight: 600; }
.btn.danger { color: var(--red); }
.btn.active { background: var(--blue); color: #001a33; border-color: transparent; }
.btn.small { padding: 0.25em 0.55em; font-size: 0.85em; }
.btn.big { font-size: 1.15rem; padding: 0.8em 1.2em; white-space: normal; }

input, textarea, select {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 0.5em 0.65em; width: 100%; min-width: 0;
}
textarea { resize: vertical; }
label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 0.25em; }

/* ---------- Speaker view ---------- */
.speaker {
  height: 100vh; display: flex; flex-direction: column; overflow: hidden;
  padding: 2vh 3vw; position: relative; transition: background 0.4s;
}
.speaker header, .speaker footer {
  display: flex; justify-content: space-between; align-items: center; gap: 2vw;
  font-size: clamp(16px, 3.2vh, 40px); color: var(--muted);
}
.speaker .now { color: var(--text); font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.speaker .now small { font-weight: 400; color: var(--muted); }
.speaker main { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; min-height: 0; }
.speaker .time {
  font-size: min(36vw, 62vh); font-weight: 700; white-space: nowrap; line-height: 1; letter-spacing: -0.02em;
  transition: color 0.3s;
}
.speaker .progress { width: 100%; height: 1.2vh; background: var(--panel-2); border-radius: 99px; overflow: hidden; margin-top: 2vh; }
.speaker .progress > div { height: 100%; width: 100%; background: currentColor; transform-origin: left center; will-change: transform; }
.speaker .idle { font-size: clamp(24px, 7vh, 90px); text-align: center; }
.speaker .idle .label { color: var(--muted); font-size: 0.55em; }

.phase-normal { color: var(--green); }
.phase-yellow { color: var(--yellow); }
.phase-red { color: var(--red); }
.phase-overtime { color: var(--red); }
.speaker.overtime { background: #3a0b0b; }
.speaker .time { will-change: opacity; }
.speaker .time.paused { opacity: 0.5; }
.speaker.overtime .time { animation: pulse 1.2s ease-in-out infinite; }
.speaker::after {
  content: ''; position: absolute; inset: 0; background: #fff; opacity: 0;
  pointer-events: none; will-change: opacity; z-index: 2;
}
.speaker.flashing::after { animation: flash-overlay 0.5s ease-in-out 6; }

/* Message is shown below the time, never on top of it */
.message {
  margin-top: 3vh; padding: 1.5vh 3vw; max-width: 100%; text-align: center;
  background: rgba(0, 0, 0, 0.85); border: 0.4vh solid currentColor; border-radius: 2vh;
  font-size: clamp(24px, 8vh, 120px); font-weight: 700; line-height: 1.15; word-break: break-word;
}
.speaker.with-message .progress { margin-top: 1.5vh; }
.speaker .fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 0; }
.speaker header, .speaker main, .speaker footer { position: relative; z-index: 1; }
.speaker .time { text-shadow: 0 0 3vh rgba(0, 0, 0, 0.8); }
.speaker header, .speaker footer { text-shadow: 0 0 8px #000, 0 0 2px #000; }
.message.flash { animation: pulse 1s ease-in-out infinite; will-change: opacity; }
.color-white { color: #fff; }
.color-green { color: var(--green); }
.color-yellow { color: var(--yellow); }
.color-red { color: var(--red); }

.blackout { position: fixed; inset: 0; background: #000; z-index: 10; }

/* ---------- Agenda view ---------- */
.agenda { max-width: 1100px; margin: 0 auto; padding: 24px 16px 48px; }
.agenda h1 { margin: 0; font-size: clamp(22px, 4vw, 36px); }
.agenda .top { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.agenda .clock { font-size: clamp(28px, 5vw, 48px); font-weight: 700; }
.summary { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.pill { background: var(--panel); border: 1px solid var(--border); border-radius: 99px; padding: 6px 14px; display: flex; gap: 8px; align-items: center; }
.pill b { font-weight: 600; }

.schedule { display: flex; flex-direction: column; gap: 8px; }
.row {
  display: grid; grid-template-columns: 74px 1fr auto; gap: 14px; align-items: center;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px;
}
.row .when { text-align: right; }
.row .when .proj { font-size: 1.3em; font-weight: 700; }
.row .when .plan { font-size: 0.8em; color: var(--muted); text-decoration: line-through; }
.row .who { min-width: 0; }
.row .who .name { font-weight: 600; font-size: 1.1em; }
.row .who .title { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .meta { text-align: right; font-size: 0.9em; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.row.done { opacity: 0.45; }
.row.host { background: var(--panel-2); border-style: dashed; }
.gap { color: var(--muted); font-size: 0.8em; padding: 0 0 0 90px; margin: -2px 0; }
.gap::before { content: '↓ '; }
.row.live { border-color: var(--green); box-shadow: 0 0 0 1px var(--green) inset; }
.row.live .badge { background: var(--green); color: #06210f; }
.badge { font-size: 0.75em; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--panel-2); text-transform: uppercase; letter-spacing: 0.04em; }
.late { color: var(--yellow); }
.very-late { color: var(--red); }
.early { color: var(--blue); }
.ontime { color: var(--green); }

/* ---------- Control view ---------- */
.plan-mode .live-only { display: none; }
.plan-mode .control { grid-template-columns: minmax(0, 1fr) minmax(280px, 380px); }
.plan-mode .control > .stack:first-child { order: 2; }
.banner { background: var(--yellow); color: #2a2100; padding: 8px 16px; font-weight: 600; }
.toast.ok { background: var(--panel-2); border: 1px solid var(--border); }
.control { display: grid; grid-template-columns: minmax(320px, 420px) 1fr; gap: 16px; padding: 16px; max-width: 1500px; margin: 0 auto; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.panel h2 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 12px; font-weight: 600; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.btns { display: flex; flex-wrap: wrap; gap: 8px; }
.btns.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.grid2 > .btn { white-space: normal; } /* fixed-width cells: long labels wrap */

/* Speaker screens: connected and showing the latest change? */
.presence {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 6px 10px; text-align: left;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer;
}
.presence-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.ok > .presence-dot, .presence-dot.ok { background: var(--green); }
.lag > .presence-dot, .presence-dot.lag { background: var(--yellow); }
.bad > .presence-dot, .presence-dot.bad { background: var(--red); animation: blink 1s steps(2) infinite; }
.presence.bad { border-color: var(--red); }
.presence-list { margin: -6px 0 0; padding: 0; list-style: none; font-size: 0.9em; }
.presence-list li { display: flex; align-items: center; gap: 8px; padding: 4px 10px; }
.presence-list .nowrap { white-space: nowrap; }
.presence-list .muted { margin-left: auto; text-align: right; }
.preview { background: #000; border-radius: var(--radius); padding: 16px; text-align: center; }
.preview .time { font-size: 72px; font-weight: 700; line-height: 1; }
.preview .who { color: var(--muted); margin-top: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview.overtime { background: #3a0b0b; }

.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.topbar h1 { font-size: 1.1rem; margin: 0; flex: 1; min-width: 0; }
.topbar .back { color: var(--blue); text-decoration: none; white-space: nowrap; }
.topbar .nav { display: flex; gap: 6px; align-items: center; }
.topbar .menu-btn { display: none; }

.talk {
  display: grid; grid-template-columns: 28px 64px 1fr auto auto; gap: 10px; align-items: center;
  padding: 10px 8px; border-bottom: 1px solid var(--border);
}
.talk:last-child { border-bottom: none; }
.talk.live { background: rgba(46, 204, 113, 0.08); border-radius: 8px; }
.talk.done .who { opacity: 0.5; }
.talk.host .who .name { font-style: italic; }
.talk .idx { color: var(--muted); text-align: right; }
.talk .when { font-size: 0.9em; }
.talk .when .plan { color: var(--muted); font-size: 0.85em; }
.talk .who .name { font-weight: 600; }
.talk .who .title { color: var(--muted); font-size: 0.9em; }
.talk .dur { font-size: 0.9em; text-align: right; }
.talk .actions { display: flex; gap: 4px; }
.talk.editing { grid-template-columns: 1fr; }
.talk-edit { display: grid; grid-template-columns: 1fr 1.5fr 80px auto; gap: 8px; align-items: end; }

.links { display: flex; flex-direction: column; gap: 8px; }
.link { display: flex; gap: 8px; align-items: center; }
.link input { font-size: 0.85em; }
.swatches { display: flex; gap: 6px; }
.swatch { width: 32px; height: 32px; border-radius: 6px; border: 2px solid transparent; cursor: pointer; }
.swatch.sel { border-color: #fff; }
.swatch[data-color='white'] { background: #fff; }
.swatch[data-color='green'] { background: var(--green); }
.swatch[data-color='yellow'] { background: var(--yellow); }
.swatch[data-color='red'] { background: var(--red); }

.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--red); color: #fff; padding: 10px 16px; border-radius: 8px; z-index: 20;
}

.login { max-width: 380px; margin: 15vh auto; padding: 0 16px; }
.login.panel { padding: 20px; width: calc(100% - 32px); }

details summary { cursor: pointer; color: var(--muted); }
kbd { background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; font-size: 0.85em; }

label.check { display: flex; gap: 8px; align-items: center; margin: 0; color: var(--text); font-size: 0.9rem; }
label.check input { width: auto; }
.dock { display: none; }

@media (max-width: 900px) {
  .control, .plan-mode .control { grid-template-columns: minmax(0, 1fr); padding: 12px; }
  /* One column in a useful order: live controls first, setup last */
  .control > .stack { display: contents; }
  #timerPanel { order: 1; }
  #fxPanel { order: 2; }
  #msgPanel { order: 3; }
  #agendaPanel { order: 4; }
  #linksPanel { order: 5; }
  #settingsPanel { order: 6; }
  .control { gap: 12px; }

  /* One slim row; links fold into a menu. */
  .topbar { padding: 8px 12px; gap: 10px; flex-wrap: wrap; }
  .topbar h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar .back { font-size: 1.3rem; line-height: 1; }
  .wide-only, .topbar #clock, .control kbd, .control kbd + span, .shortcuts { display: none; }
  .topbar .menu-btn { display: inline-block; min-height: 36px; min-width: 40px; }
  .topbar .nav { display: none; flex-basis: 100%; flex-direction: column; align-items: stretch; gap: 6px; padding-top: 4px; }
  .topbar .nav.open { display: flex; }
  .topbar .nav .btn { text-align: left; font-size: 1rem; }

  /* Comfortable tap targets */
  .btn { min-height: 44px; }
  .btn.small { min-height: 40px; min-width: 40px; }
  .preview .time { font-size: 56px; }

  body:not(.plan-mode) { padding-bottom: 76px; }
  body:not(.plan-mode) .dock {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; gap: 8px; align-items: center;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--border);
  }
  .dock-time { flex: 1; font-size: 28px; font-weight: 700; }
  .dock .btn { min-width: 64px; }
  .grid2 { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  #settings .grid2 { grid-template-columns: minmax(0, 1fr); }
  .talk { grid-template-columns: 54px 1fr auto; }
  .talk .idx, .talk .dur { display: none; }
  .talk .actions { grid-column: 1 / -1; justify-content: flex-end; }
  .talk-edit { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 600px) {
  .agenda { padding: 16px 12px 32px; }
  .agenda .top { margin-bottom: 12px; }
  .agenda .clock { font-size: 32px; }
  .summary { gap: 6px; margin-bottom: 12px; }
  .pill { font-size: 0.85rem; padding: 4px 10px; }
  .schedule { gap: 6px; }
  .row { grid-template-columns: 52px 1fr; gap: 2px 12px; padding: 10px 12px; }
  .row .when .proj { font-size: 1.1em; }
  .row .who .name { font-size: 1em; }
  .row .who .title { font-size: 0.9em; }
  .row .meta { grid-column: 2; flex-direction: row; justify-content: flex-start; gap: 8px; font-size: 0.85em; }
  .gap { padding-left: 76px; }
}

/* ---------- Overview ---------- */
.overview { max-width: 960px; margin: 0 auto; padding: 24px 16px 48px; }
.overview h1 { margin: 0 0 20px; }
.overview .section { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 28px 0 10px; font-weight: 600; }
.new-session { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) auto; gap: 10px; align-items: end; }
.sessions { display: flex; flex-direction: column; gap: 10px; }
.session { display: flex; flex-direction: column; gap: 12px; }
.session-head { display: flex; justify-content: space-between; gap: 12px; }
.session-title { font-weight: 600; font-size: 1.15em; }
.session .btn { text-decoration: none; }
.badge.live { background: var(--green); color: #06210f; }

@media (max-width: 640px) {
  .new-session { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- QR dialog ---------- */
.modal {
  position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, 0.7);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-box { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; width: min(440px, 100%); max-height: 100%; overflow: auto; }
.qr { background: #fff; border-radius: 8px; padding: 8px; }
.qr svg { display: block; width: 100%; height: auto; }
.qr-url { font-size: 0.8em; color: var(--muted); word-break: break-all; }

@media print {
  body.qr-open { background: #fff; }
  body.qr-open > :not(#qrModal) { display: none !important; }
  body.qr-open .modal { position: static; background: none; padding: 0; }
  body.qr-open .modal-box { border: none; background: none; color: #000; width: 12cm; margin: 2cm auto; text-align: center; }
  body.qr-open .modal-box h2 { color: #000; font-size: 28pt; text-transform: none; letter-spacing: 0; }
  body.qr-open .modal-box .muted, body.qr-open .qr-url { color: #333; }
  body.qr-open .modal-box .btns { display: none; }
}
