:root {
  --bg: #090b10;
  --panel: rgba(15, 23, 42, 0.74);
  --panel-strong: rgba(15, 23, 42, 0.92);
  --line: rgba(255, 255, 255, 0.14);
  --text: #f8fafc;
  --muted: #94a3b8;
  --soft: rgba(255, 255, 255, 0.09);
  --soft-hover: rgba(255, 255, 255, 0.15);
  --accent: #38bdf8;
  --accent-strong: #0ea5e9;
  --danger: #fb7185;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.42);
  --radius-xl: 28px;
  --radius-lg: 20px;
  --radius-md: 14px;
  color-scheme: dark;
}

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

html,
body {
  margin: 0;
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  touch-action: none;
  user-select: none;
}

button,
label,
input {
  font: inherit;
}

button,
label.secondaryBtn,
label.primaryBtn {
  cursor: pointer;
}

input[type="file"] {
  display: none;
}

#appShell {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 20% 8%, rgba(56, 189, 248, 0.2), transparent 28%),
    radial-gradient(circle at 80% 12%, rgba(168, 85, 247, 0.18), transparent 32%),
    var(--bg);
}

.topbar {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  left: max(14px, env(safe-area-inset-left));
  right: max(14px, env(safe-area-inset-right));
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: rgba(2, 6, 23, 0.58);
  box-shadow: 0 16px 45px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(18px);
}

body.compact .topbar {
  display: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.brandMark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: linear-gradient(135deg, #38bdf8, #818cf8);
  color: white;
  font-weight: 900;
  box-shadow: 0 10px 24px rgba(56, 189, 248, 0.25);
}

.brand strong {
  display: block;
  font-size: 15px;
  letter-spacing: 0.02em;
}

.brand span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 1px;
}

.topActions {
  display: flex;
  gap: 8px;
}

.stageWrap {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 0;
}

.stage {
  position: relative;
  width: 100vw;
  height: 100dvh;
  overflow: hidden;
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.035) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.035) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255, 255, 255, 0.035) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255, 255, 255, 0.035) 75%);
  background-size: 30px 30px;
  background-position: 0 0, 0 15px, 15px -15px, -15px 0;
}

#cameraVideo,
#backgroundImage,
#traceImage,
#drawCanvas,
.gridLayer,
.crosshair {
  position: absolute;
  inset: 0;
}

#cameraVideo,
#backgroundImage {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
  pointer-events: none;
}

#cameraVideo.active,
#backgroundImage.active {
  display: block;
}

#traceImage {
  inset: auto;
  top: 50%;
  left: 50%;
  max-width: min(72vw, 760px);
  max-height: min(72dvh, 760px);
  object-fit: contain;
  transform-origin: center center;
  display: none;
  pointer-events: auto;
  will-change: transform, opacity, filter;
  filter: drop-shadow(0 14px 34px rgba(0, 0, 0, 0.35));
}

#traceImage.visible {
  display: block;
}

#drawCanvas {
  width: 100%;
  height: 100%;
  z-index: 14;
  pointer-events: none;
}

body.drawMode #drawCanvas {
  pointer-events: auto;
}

#traceImage {
  z-index: 12;
}

#cameraVideo,
#backgroundImage {
  z-index: 1;
}

.gridLayer {
  z-index: 4;
  display: none;
  background-image:
    linear-gradient(rgba(56, 189, 248, 0.14) 1px, transparent 1px),
    linear-gradient(90deg, rgba(56, 189, 248, 0.14) 1px, transparent 1px);
  background-size: 42px 42px;
  mask-image: radial-gradient(circle at center, black 58%, transparent 86%);
  pointer-events: none;
}

.gridLayer.on {
  display: block;
}

.crosshair {
  z-index: 5;
  display: none;
  pointer-events: none;
}

.crosshair.on {
  display: block;
}

.crosshair::before,
.crosshair::after,
.crosshair span::before,
.crosshair span::after {
  content: "";
  position: absolute;
  background: rgba(255, 255, 255, 0.45);
  box-shadow: 0 0 18px rgba(56, 189, 248, 0.42);
}

.crosshair::before {
  left: 50%;
  top: 0;
  width: 1px;
  height: 100%;
}

.crosshair::after {
  top: 50%;
  left: 0;
  width: 100%;
  height: 1px;
}

.crosshair span::before {
  width: 54px;
  height: 54px;
  border: 1px solid rgba(255, 255, 255, 0.45);
  border-radius: 999px;
  background: transparent;
  left: calc(50% - 27px);
  top: calc(50% - 27px);
}

.crosshair span::after {
  width: 8px;
  height: 8px;
  border-radius: 99px;
  left: calc(50% - 4px);
  top: calc(50% - 4px);
  background: rgba(56, 189, 248, 0.9);
}

.emptyState {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  pointer-events: none;
}

.emptyState.hidden {
  display: none;
}

.emptyCard {
  width: min(460px, 92vw);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: var(--radius-xl);
  background: rgba(15, 23, 42, 0.72);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
  pointer-events: auto;
  text-align: center;
}

.statusPill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  padding: 0 11px;
  margin-bottom: 14px;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.12);
  color: #bae6fd;
  font-size: 12px;
  border: 1px solid rgba(56, 189, 248, 0.24);
}

.emptyCard h1 {
  margin: 0;
  font-size: clamp(24px, 5vw, 38px);
  line-height: 1.12;
  letter-spacing: -0.04em;
}

.emptyCard p {
  margin: 14px 0 22px;
  color: #cbd5e1;
  line-height: 1.65;
  font-size: 14px;
}

.emptyActions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.panel {
  position: fixed;
  left: max(14px, env(safe-area-inset-left));
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  z-index: 30;
  display: grid;
  grid-template-columns: 1fr 1.2fr 1fr;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 26px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
  transition: transform 0.2s ease, opacity 0.2s ease;
  touch-action: auto;
}

body.compact .panel {
  transform: translateY(calc(100% - 58px));
}

.panelSection {
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.055);
  padding: 8px;
}

.mainControls,
.toolTabs,
.colorRow,
.actionGrid {
  display: flex;
  gap: 7px;
  align-items: center;
  flex-wrap: wrap;
}

.sliders {
  display: grid;
  grid-template-columns: repeat(2, minmax(130px, 1fr));
  gap: 8px 12px;
}

.sliders label {
  display: grid;
  gap: 4px;
}

.sliders span {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.sliders b {
  color: #e2e8f0;
  font-weight: 700;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

input[type="color"] {
  appearance: none;
  width: 38px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: transparent;
  overflow: hidden;
}

input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

input[type="color"]::-webkit-color-swatch {
  border: 0;
}

button,
.secondaryBtn,
.primaryBtn {
  border: 0;
  border-radius: 14px;
  color: var(--text);
  min-height: 38px;
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
}

.primaryBtn {
  background: linear-gradient(135deg, var(--accent), #818cf8);
  box-shadow: 0 12px 24px rgba(14, 165, 233, 0.22);
  font-weight: 800;
}

.secondaryBtn,
.ghostBtn,
.smallBtn,
.tabBtn {
  background: var(--soft);
  color: var(--text);
}

.ghostBtn {
  min-height: 34px;
  padding: 0 11px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.smallBtn,
.tabBtn {
  min-height: 36px;
  padding: 0 11px;
  font-size: 13px;
}

button:hover,
.secondaryBtn:hover {
  background: var(--soft-hover);
}

.primaryBtn:hover {
  filter: brightness(1.06);
}

button:active,
.secondaryBtn:active,
.primaryBtn:active {
  transform: translateY(1px);
}

.active,
[aria-pressed="true"].tabBtn,
[aria-pressed="true"].smallBtn {
  background: rgba(56, 189, 248, 0.2);
  color: #e0f2fe;
  border-color: rgba(56, 189, 248, 0.35);
}

.danger {
  color: #ffe4e6;
  background: rgba(244, 63, 94, 0.16);
}

.colorChip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  padding: 0 6px 0 3px;
}

.statusBox {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.statusBox strong {
  display: block;
  color: #e2e8f0;
  margin-bottom: 3px;
}

.statusBox p {
  margin: 0;
}

#toast {
  position: absolute;
  left: 50%;
  bottom: 155px;
  z-index: 40;
  transform: translateX(-50%) translateY(12px);
  max-width: min(520px, 88vw);
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.78);
  color: #e2e8f0;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.32);
  backdrop-filter: blur(14px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  font-size: 13px;
  text-align: center;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.helpDialog {
  width: min(520px, 92vw);
  border: 1px solid var(--line);
  border-radius: 26px;
  padding: 0;
  color: var(--text);
  background: var(--panel-strong);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
}

.helpDialog::backdrop {
  background: rgba(2, 6, 23, 0.62);
  backdrop-filter: blur(4px);
}

.helpDialog form {
  padding: 24px;
}

.helpDialog h2 {
  margin: 0 0 10px;
}

.helpDialog p,
.helpDialog li {
  color: #cbd5e1;
  line-height: 1.65;
}

.shortcutText {
  font-size: 13px;
}

kbd {
  display: inline-flex;
  min-width: 24px;
  height: 22px;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.11);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.14);
}

body.locked #traceImage {
  pointer-events: none;
}

@media (max-width: 1040px) {
  .panel {
    grid-template-columns: 1fr 1fr;
  }
  .sliders {
    grid-column: 1 / -1;
  }
}

@media (max-width: 720px) {
  .topbar {
    top: max(8px, env(safe-area-inset-top));
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
  }

  .brand span {
    display: none;
  }

  .panel {
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    grid-template-columns: 1fr;
    max-height: 50dvh;
    overflow: auto;
    border-radius: 24px;
  }

  body.compact .panel {
    transform: translateY(calc(100% - 55px));
  }

  .sliders {
    grid-template-columns: 1fr;
  }

  .statusBox {
    display: none;
  }

  .emptyCard {
    padding: 22px;
  }

  #toast {
    bottom: 170px;
  }
}

@media (max-width: 430px) {
  .topActions .ghostBtn:first-child {
    display: none;
  }

  .smallBtn,
  .tabBtn,
  .secondaryBtn,
  .primaryBtn {
    flex: 1 1 auto;
  }
}
