:root{
  /* User color scheme */
  --color-primary: #303d52;
  --color-primary-text: #ffffff;
  --color-primary-light: #5a6f8f;
  --color-primary-lighter: #7f94b0;
  --color-primary-extra-light: #8da1b9;
  --color-primary-dark: #001f3f;
  
  --color-secondary: #73524C;
  --color-secondary-text: #ffffff;
  --color-secondary-light: #A9746D;
  --color-secondary-dark: #4F3732;

  --color-tertiary: #A3A96F;
  --color-tertiary-text: #202020;
  --color-tertiary-light: #BFC6A1;
  --color-tertiary-dark: #6E7343;

  --color-quaternary: #5C677B;
  --color-quaternary-text: #ffffff;
  --color-quaternary-light: #7F8A9E;
  --color-quaternary-dark: #3B4455;

  --color-quinary: #7D8465;
  --color-quinary-text: #202020;
  --color-quinary-light: #B2B99A;
  --color-quinary-dark: #505738;

  --color-grey: #f5f5f5;
  --color-grey-text: #000000;
  --color-grey-light: #ffffff;
  --color-grey-dark: #e0e0e0;

  --color-success: #4CAF50;
  --color-success-text: #ffffff;
  --color-success-light: #81c784;
  --color-success-dark: #388e3c;

  --color-warning: #FFC107;
  --color-warning-text: #ffffff;
  --color-warning-light: #ffd54f;
  --color-warning-dark: #ffa000;
  
  --color-error: #F44336;
  --color-error-text: #ffffff;
  --color-error-light: #e57373;
  --color-error-dark: #d32f2f;

  --color-info: #2196F3;
  --color-info-text: #ffffff;
  --color-info-light: #64b5f6;
  --color-info-dark: #1976d2;

  --color-white: #ffffff;
  --color-black: #000000;

  /* App theme variables mapped to user colors */
  --bg: var(--color-primary-dark);
  --bg-elev: var(--color-primary);
  --line: var(--color-quaternary-dark);
  --text: var(--color-primary-text);
  --muted: var(--color-primary-lighter);
  --accent: var(--color-info);
  --accent-2: var(--color-info-light);
  --danger: var(--color-error);
  --ok: var(--color-success);

  --tab-h: 42px;
  --header-h: 66px;
  --footer-h: 40px;
  --toolbar-h: 44px;
  --sidebar-w: 320px; /* default open width */
  --trackers-sidebar-w: 0px;
  --radius: 12px;
  --shadow: 0 18px 42px rgba(0,0,0,.35);
  --camera-max-width: 960px;
  --camera-max-height: 72vh;
  --camera-aspect: 16 / 9;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
}

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(160deg, rgba(0, 16, 32, 0.92) 0%, rgba(18, 33, 52, 0.94) 48%, rgba(4, 12, 24, 0.96) 100%), var(--bg);
  color: var(--text);
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, "Roboto", sans-serif;
  display: flex;
  -webkit-font-smoothing: antialiased;
}

body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(120% 120% at 50% 10%, rgba(64, 120, 208, 0.25) 0%, rgba(0, 0, 0, 0) 60%),
              radial-gradient(90% 90% at 10% 90%, rgba(163, 169, 111, 0.22) 0%, rgba(0, 0, 0, 0) 65%);
  mix-blend-mode: screen;
  opacity: 0.65;
}

.app {
  flex: 1 1 auto;
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  position: relative;
  isolation: isolate;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(16px + env(safe-area-inset-top, 0px)) clamp(20px, 5vw, 48px) 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  text-align: center;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.85) 0%, rgba(0, 0, 0, 0.55) 70%, rgba(0, 0, 0, 0) 100%);
  backdrop-filter: blur(18px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.app-header nav,
.app-header .header-actions {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  pointer-events: auto;
}

.app-header nav {
  left: clamp(20px, 5vw, 48px);
}

.app-header nav:empty,
.app-header .header-actions:empty {
  display: none;
}

.app-header .header-actions {
  right: clamp(20px, 5vw, 48px);
}

.title-stack {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
  text-align: center;
}

.title-stack h1 {
  margin: 0;
  font-weight: 600;
  font-size: clamp(1.75rem, 1.2vw + 1.4rem, 2.6rem);
  letter-spacing: -0.01em;
  color: var(--text);
}

.title-stack .subtitle {
  margin: 0;
  font-size: 0.95rem;
  color: var(--muted);
}

.app-main {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  justify-items: center;
  align-content: start;
  gap: clamp(20px, 4vw, 36px);
  padding: clamp(18px, 4vw, 40px) clamp(20px, 5vw, 48px) calc(clamp(32px, 6vw, 64px) + 160px);
}

.camera-view {
  position: relative;
  display: flex;
  justify-content: center;
  width: min(100%, var(--camera-max-width));
}

.video-shell {
  position: relative;
  width: min(100%, var(--camera-max-width));
  max-height: min(var(--camera-max-height), 820px);
  aspect-ratio: var(--camera-aspect);
  border-radius: clamp(18px, 2vw, 28px);
  overflow: hidden;
  background: radial-gradient(120% 120% at 50% 10%, rgba(93, 116, 148, 0.35) 0%, rgba(10, 18, 30, 0.95) 70%);
  box-shadow: var(--shadow);
  isolation: isolate;
  display: flex;
  flex-direction: column;
}

#preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: var(--color-black);
  display: block;
}

.hud {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: clamp(18px, 3vw, 32px);
  pointer-events: none;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.52) 0%, rgba(0, 0, 0, 0.18) 30%, rgba(0, 0, 0, 0.6) 100%);
}

.hud-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  font-size: 0.82rem;
  letter-spacing: 0.02em;
  pointer-events: auto;
  backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

.badge-rec,
.badge-live {
  background: linear-gradient(120deg, rgba(244, 67, 54, 0.85) 0%, rgba(244, 139, 54, 0.65) 100%);
  color: var(--color-error-text);
  font-weight: 600;
  text-transform: uppercase;
}

.hud-bottom {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 14px;
}

.meter {
  height: 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.14);
  overflow: hidden;
  pointer-events: auto;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.25);
}

#motionBar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent) 0%, var(--accent-2) 100%);
  transition: width 0.16s ease;
}

.control-panel {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  pointer-events: auto;
  width: min(100%, var(--camera-max-width));
  margin: 0 auto;
}

button {
  appearance: none;
  border: none;
  border-radius: clamp(14px, 1vw, 18px);
  padding: 14px 18px;
  font-size: 1rem;
  font-weight: 600;
  background: linear-gradient(120deg, var(--accent) 0%, var(--accent-2) 100%);
  color: var(--color-info-text);
  cursor: pointer;
  box-shadow: 0 14px 30px rgba(33, 150, 243, 0.35);
  transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
}

button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 18px 36px rgba(33, 150, 243, 0.45);
}

button:active:not(:disabled) {
  transform: translateY(0);
  filter: brightness(0.92);
}

button:focus-visible {
  outline: 3px solid rgba(100, 181, 246, 0.6);
  outline-offset: 2px;
}

button.secondary {
  background: rgba(24, 37, 52, 0.92);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.35);
}

button.warn {
  background: linear-gradient(120deg, var(--danger) 0%, var(--color-warning-dark) 100%);
  color: var(--color-warning-text);
  box-shadow: 0 12px 28px rgba(255, 193, 7, 0.32);
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  pointer-events: auto;
  text-align: center;
}

.app-actions {
  position: sticky;
  bottom: 0;
  width: 100%;
  margin-top: auto;
  padding: clamp(18px, 4vw, 32px) clamp(20px, 5vw, 48px) calc(24px + env(safe-area-inset-bottom, 0px));
  display: grid;
  gap: 16px;
  align-items: center;
  justify-items: center;
  background: linear-gradient(180deg, rgba(4, 8, 16, 0.78) 0%, rgba(2, 6, 14, 0.92) 100%);
  backdrop-filter: blur(18px);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  z-index: 9;
  isolation: isolate;
}

.app-actions::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.38) 0%, rgba(0, 0, 0, 0.12) 100%);
  opacity: 0.75;
  pointer-events: none;
}

.app-actions > * {
  position: relative;
  z-index: 1;
}

.event-panel {
  display: flex;
  align-items: stretch;
  justify-content: center;
}

.panel-card {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: rgba(22, 30, 44, 0.68);
  color: var(--text);
  border-radius: clamp(18px, 2vw, 28px);
  padding: clamp(18px, 3vw, 28px);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.panel-card h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--color-primary-extra-light);
}

.log {
  flex: 1 1 auto;
  font-size: 0.85rem;
  color: var(--muted);
  background: rgba(8, 14, 24, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: clamp(14px, 1vw, 18px);
  padding: 14px;
  overflow-y: auto;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.02);
}

@media (max-width: 1200px) {
  .app-main {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 1024px) {
  .app-main {
    grid-template-columns: 1fr;
  }
  .event-panel {
    order: 2;
  }
}

@media (max-width: 900px) {
  :root {
    --camera-max-width: 640px;
    --camera-max-height: 68vh;
  }

  body {
    background: var(--color-black);
  }

  body::after {
    opacity: 0.4;
  }

  .app {
    background: var(--color-black);
  }

  .app-main {
    padding: clamp(16px, 5vw, 32px) clamp(18px, 6vw, 28px) calc(clamp(28px, 6vw, 60px) + 140px);
  }

  .hud {
    padding: clamp(18px, 6vw, 32px);
    background: linear-gradient(180deg, rgba(0, 0, 0, 0.72) 0%, rgba(0, 0, 0, 0.28) 28%, rgba(0, 0, 0, 0.75) 100%);
  }

  .app-actions {
    padding: clamp(20px, 6vw, 32px) clamp(18px, 6vw, 28px) calc(26px + env(safe-area-inset-bottom, 0px));
    box-shadow: 0 -18px 42px rgba(0, 0, 0, 0.55);
  }

  .control-panel {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  }

  button {
    padding: 16px;
  }

  .event-panel {
    display: none;
  }
}

@media (orientation: portrait) {
  :root {
    --camera-max-width: min(92vw, 520px);
    --camera-max-height: min(64vh, 620px);
    --camera-aspect: 3 / 4;
  }

  .app-main {
    padding-top: clamp(18px, 6vw, 36px);
  }

  .hud {
    padding: clamp(16px, 5vw, 28px);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
