/* ===== Base ===== */
:root {
  color-scheme: dark;
  --ui-padding: 12px;
  --ui-bottom: max(var(--ui-padding), env(safe-area-inset-bottom));
  --panel-radius: 10px;
  --panel-padding: 16px;
  --panel-max: 560px;
  --btn-height: 38px;
  --btn-padding-x: 12px;
  --btn-radius: 8px;
  --primary-btn-width: 140px;
  --slider-row-gap: 4px;
  --slider-pad-y: 8px;
  --panel-bg: rgba(0,0,0,.35);
  --panel-border: rgba(255,255,255,.10);
  --text: rgba(255,255,255,.90);
  --btn-bg: rgba(255,255,255,.12);
  --btn-bg-hover: rgba(255,255,255,.16);
  --rec-bg: rgba(255,80,80,.28);
  --rec-bg-hover: rgba(255,80,80,.34);
  --rec-bg-recording: rgba(255,40,40,.55);
  --save-bg: rgba(120,255,190,.28);
  --save-bg-hover: rgba(120,255,190,.36);
  --select-height: 22px;
  --mini-pill-width: 240px;
}
html, body {
  margin: 0;
  height: 100%;
  background: #0f1115;
  overflow: hidden;
  font: 13px/1.2 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--text);
}

/* ===== Canvas ===== */
#emojiCanvas {
  display: block;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: 90vw;
  max-height: 90vh;
}

/* ===== UI panel ===== */
.ui {
  position: fixed;
  left: max(var(--ui-padding), env(safe-area-inset-left));
  bottom: var(--ui-bottom);
  z-index: 10;
  user-select: none;
}
.panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--panel-radius);
  padding: var(--panel-padding);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: var(--panel-max);
  box-sizing: border-box;
}

/* ===== Sliders ===== */
.row { display: flex; align-items: center; gap: 10px; }
.row.stack {
  flex-direction: column;
  align-items: stretch;
  gap: var(--slider-row-gap);
  margin-bottom: 2px;
}
.sliderRow {
  display: grid;
  grid-template-columns: 1fr 6fr 1fr;
  align-items: center;
  gap: 8px;
  padding: var(--slider-pad-y) 0;
}
.label { opacity: .85; }
.val {
  text-align: right;
  font-variant-numeric: tabular-nums;
  opacity: .86;
}

/* ===== Buttons ===== */
.btn {
  appearance: none;
  border: 0;
  background: var(--btn-bg);
  color: rgba(255,255,255,.92);
  height: var(--btn-height);
  padding: 0 var(--btn-padding-x);
  border-radius: var(--btn-radius);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  box-sizing: border-box;
  transition: background .15s ease, transform .1s ease;
}
.btn:hover  { background: var(--btn-bg-hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; pointer-events: none; }
.gridBtn { width: var(--primary-btn-width); }
.btn.rec       { background: var(--rec-bg); }
.btn.rec:hover { background: var(--rec-bg-hover); }
.btn.recording { background: var(--rec-bg-recording); }
.saveLink      { background: var(--save-bg); }
.saveLink:hover{ background: var(--save-bg-hover); }

/* ===== Controls grid ===== */
.controlsGrid {
  display: grid !important;
  grid-template-columns: var(--primary-btn-width) minmax(0,1fr) var(--primary-btn-width) !important;
  grid-template-rows: auto auto auto auto !important;
  row-gap: 4px !important;
  column-gap: 10px !important;
  align-items: center !important;
}
#pickBtn   { grid-column: 1 !important; grid-row: 1 !important; align-self: stretch !important; }
.toggleFit { grid-column: 2 !important; grid-row: 1 !important; }
#recBtn    { grid-column: 3 !important; grid-row: 1 !important; align-self: stretch !important; }
#camBtn    { grid-column: 1 !important; grid-row: 2 !important; align-self: stretch !important; }
.toggleCam { grid-column: 2 !important; grid-row: 2 !important; }
#snapBtn   { grid-column: 3 !important; grid-row: 2 !important; align-self: stretch !important; }
#tc        { grid-column: 3 !important; grid-row: 3 !important; text-align: center !important; }
.slot      { grid-column: 3 !important; grid-row: 4 !important; }

.timecode {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  opacity: .9;
}
.slot {
  width: var(--primary-btn-width);
  height: var(--select-height);
  position: relative;
}
.slot > * {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}
select {
  background: rgba(255,255,255,.06);
  color: rgba(255,255,255,.92);
  border: 1px solid rgba(255,255,255,.12);
  outline: none;
  border-radius: 999px;
  font-size: 12px;
  padding: 0 10px;
}

/* ===== Toggle switches ===== */
.toggleGrid {
  display: grid;
  grid-template-columns: 30px auto 16px;
  column-gap: 4px;
  align-items: center;
  justify-self: start;
}
.toggleGrid .label { white-space: nowrap; font-size: 10px; }
.toggleGrid .label.l { text-align: right; }
.switch {
  width: 30px;
  height: 18px;
  border-radius: 999px;
  background: rgba(255,255,255,.18);
  position: relative;
  cursor: pointer;
  transition: background .18s ease;
  outline: none;
}
.switch .knob {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: rgba(255,255,255,.95);
  position: absolute;
  top: 2px; left: 2px;
  transition: transform .18s ease;
}
.switch.on { background: rgba(255,255,255,.26); }
.switch.on .knob { transform: translateX(12px); }

/* ===== Topbar ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: -6px -6px 2px -6px;
}
#minBtn  { margin-right: auto; }
#recDot  { margin-left: auto; }
.miniBtn {
  appearance: none;
  border: 0;
  background: rgba(255,255,255,.10);
  color: rgba(255,255,255,.85);
  width: 30px; height: 26px;
  border-radius: 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}
.miniBtn:hover  { background: rgba(255,255,255,.14); }
.miniBtn:active { transform: translateY(1px); }
.miniBtn .chev::before { content: "▾"; display: block; }
body.uiMin .miniBtn .chev::before { content: "▴"; }

.recDot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: rgba(255,255,255,.18);
  box-shadow: 0 0 0 2px rgba(255,255,255,.08) inset;
}
body.isRecording .recDot {
  background: rgba(255,60,60,.95);
  box-shadow: 0 0 0 2px rgba(255,60,60,.20) inset, 0 0 12px rgba(255,60,60,.45);
  animation: dotPulse 1s ease-in-out infinite;
}
@keyframes dotPulse {
  0%, 100% { filter: brightness(1); }
  50%      { filter: brightness(1.25); }
}

/* ===== Minimized state ===== */
body.uiMin .panel > :not(.topbar):not(.controlsGrid) { display: none !important; }
body.uiMin .panel {
  width: var(--mini-pill-width) !important;
  max-width: none !important;
  padding: 10px !important;
  gap: 10px !important;
}
body.uiMin .controlsGrid {
  display: flex !important;
  flex-direction: column !important;
  align-items: stretch !important;
  gap: 10px !important;
}
body.uiMin .controlsGrid .toggleGrid { display: none !important; }
body.uiMin .controlsGrid > div:empty { display: none !important; }
body.uiMin .controlsGrid .gridBtn,
body.uiMin .controlsGrid .slot,
body.uiMin .controlsGrid #tc { width: 100% !important; }
body.uiMin #pickBtn, body.uiMin #camBtn { display: none !important; }

/* ===== Emoji selectors ===== */
.emojiSection { display: flex; flex-direction: column; gap: 6px; }
.palRow {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.palBtns { display: flex; gap: 4px; }
.palBtn {
  appearance: none;
  border: 0;
  background: rgba(255,255,255,.08);
  color: rgba(255,255,255,.75);
  height: 24px;
  width: 34px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  transition: background .12s;
}
.palBtn:hover  { background: rgba(255,255,255,.14); }
.palBtn.active { background: rgba(255,255,255,.26); color: rgba(255,255,255,.95); }
.palActions {
  margin-left: auto;
  display: flex;
  gap: 4px;
}
.paletteToggle {
  height: 24px;
  width: 34px;
  padding: 0;
  border-radius: 6px;
  font-size: 12px;
}
.paletteToggle[disabled] {
  opacity: .45;
  cursor: default;
}
.emojiSection.collapsed .emojiScrollBox { display: none; }
.emojiScrollBox {
  max-height: 148px;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,.15) transparent;
}
.emojiGrid4 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}
.colorSelect {
  background: rgba(255,255,255,.08);
  color: rgba(255,255,255,.92);
  border: 1px solid rgba(255,255,255,.10);
  border-radius: 6px;
  padding: 3px 4px;
  font-size: 13px;
  width: 100%;
  box-sizing: border-box;
  cursor: pointer;
}

/* ===== Drop overlay ===== */
.drop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity .12s ease;
  z-index: 9;
}
.drop.on { opacity: 1; }
.drop .box {
  border: 2px dashed rgba(255,255,255,.35);
  background: rgba(0,0,0,.35);
  border-radius: 22px;
  padding: 22px 26px;
  text-align: center;
  backdrop-filter: blur(10px);
}
.drop .title { font-size: 16px; margin-bottom: 6px; }
.drop .sub   { opacity: .75; }

/* ===== Modal ===== */
#shotModal {
  position: fixed;
  inset: 0;
  z-index: 999;
  display: none;
}
.shotBackdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.55);
  backdrop-filter: blur(6px);
}
.shotCard {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%,-50%);
  width: min(80vw, 920px);
  max-height: calc(100dvh - 24px);
  background: rgba(0,0,0,.35);
  border: 1px solid rgba(255,255,255,.10);
  border-radius: 18px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 18px 40px rgba(0,0,0,.35);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  box-sizing: border-box;
}
#shotModal img,
#shotModal video {
  width: 100%;
  max-height: 60dvh;
  object-fit: contain;
  border-radius: 14px;
  background: rgba(0,0,0,.25);
}
.shotActions {
  display: flex;
  gap: 10px;
  justify-content: space-between;
  position: sticky;
  bottom: 0;
  background: rgba(0,0,0,.22);
  backdrop-filter: blur(6px);
  padding-top: 8px;
}
.shotActions .gridBtn { width: 100%; }

/* ===== Mobile ===== */
@media (pointer:coarse) {
  .sliderRow input[type="range"] { height: 34px; }
  #recBtn { display: none !important; }
  #tc     { display: none !important; }
  #snapBtn { grid-column: 3 !important; grid-row: 1 !important; }
  .slot    { grid-column: 3 !important; grid-row: 2 !important; }
  :root {
    --slider-pad-y: 5px;
    --slider-row-gap: 2px;
  }
  .sliderRow .label, .sliderRow .val { font-size: 12px; }
}
@media (max-width: 720px) {
  .ui {
    left: 0; right: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 0 var(--ui-padding);
    box-sizing: border-box;
    --primary-btn-width: 96px;
    --btn-height: 34px;
    --btn-padding-x: 8px;
  }
  .panel { max-width: 92vw; }
}
@media (max-width: 920px) and (orientation: landscape) {
  :root { --ui-bottom: 8px; --slider-pad-y: 3px; }
  .ui {
    left: max(10px, env(safe-area-inset-left));
    right: auto; width: auto; padding: 0; display: block;
  }
  .panel {
    max-width: min(460px, 92vw);
    max-height: calc(100dvh - max(10px, env(safe-area-inset-top)) - max(10px, env(safe-area-inset-bottom)) - 10px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px; gap: 6px;
  }
  .row.stack { gap: 2px; }
}

input#filePicker { position: fixed; top: -200px; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
#liveVideo       { display: none; }
