:root {
  --primary: #0066cc;
  --primary-dark: #004b9b;
  --accent: #03a9f4;
  --ink: #172033;
  --muted: #64748b;
  --line: #dfe7f0;
  --surface: #ffffff;
  --surface-soft: #f4f7fb;
  --success: #16a66a;
  --warning: #ef9c22;
  --danger: #df4b5f;
  --shadow: 0 14px 40px rgba(32, 65, 105, .09);
  font-family: Pretendard, "Noto Sans KR", "Apple SD Gothic Neo", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: #edf3f8;
}

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

html { min-width: 320px; min-height: 100%; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: radial-gradient(circle at 10% 0, rgba(3, 169, 244, .11), transparent 26rem), #edf3f8;
}

button, input, select { font: inherit; }
button, select, input[type="range"] { touch-action: manipulation; }

.skip-link {
  position: fixed;
  z-index: 100;
  top: 8px;
  left: 8px;
  padding: 10px 14px;
  border-radius: 9px;
  color: #fff;
  background: #111827;
  transform: translateY(-150%);
}
.skip-link:focus { transform: translateY(0); }

.site-header {
  color: #fff;
  background: linear-gradient(120deg, var(--primary-dark), var(--primary) 62%, #0789d2);
  box-shadow: 0 4px 18px rgba(0, 63, 129, .22);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  width: min(1180px, calc(100% - 32px));
  min-height: 76px;
  margin: 0 auto;
}

.brand, .home-link { color: #fff; text-decoration: none; }
.brand { display: inline-flex; align-items: center; gap: 11px; }
.brand::before {
  content: "";
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  border-radius: 11px;
  background: #0066cc url("https://nukcanon.github.io/nukcanon/image/nukcanon-brand.png?v=20260915") center / cover no-repeat;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .18);
}
.brand-copy { display: grid; gap: 1px; }
.brand-copy strong { font-size: 1.23rem; letter-spacing: -.025em; line-height: 1.2; }
.brand-copy small { color: rgba(255, 255, 255, .72); font-size: .67rem; font-weight: 700; letter-spacing: .09em; text-transform: uppercase; }

.home-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid rgba(255, 255, 255, .25);
  border-radius: 10px;
  background: rgba(255, 255, 255, .1);
  font-size: .86rem;
  font-weight: 750;
}
.home-link:hover { background: rgba(255, 255, 255, .18); }
.home-link svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.workspace { width: min(1180px, calc(100% - 32px)); margin: 0 auto; padding: 28px 0 42px; }

.app-heading { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin-bottom: 18px; }
.eyebrow, .panel-kicker { margin: 0 0 4px; color: var(--primary); font-size: .7rem; font-weight: 850; letter-spacing: .13em; }
.app-heading h1 { margin: 0; color: #101827; font-size: clamp(1.65rem, 4vw, 2.25rem); line-height: 1.2; letter-spacing: -.04em; }

.status-pill { display: inline-flex; align-items: center; gap: 8px; min-height: 38px; padding: 0 13px; border: 1px solid #d7e3ef; border-radius: 999px; color: #4c5d73; background: rgba(255,255,255,.8); font-size: .78rem; font-weight: 750; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #94a3b8; box-shadow: 0 0 0 4px rgba(148,163,184,.13); }
.status-pill[data-state="ready"] .status-dot { background: var(--success); box-shadow: 0 0 0 4px rgba(22,166,106,.13); }
.status-pill[data-state="learning"] .status-dot { background: var(--warning); box-shadow: 0 0 0 4px rgba(239,156,34,.14); }
.status-pill[data-state="detected"] .status-dot { background: var(--danger); box-shadow: 0 0 0 4px rgba(223,75,95,.14); }
.status-pill[data-state="measuring"] .status-dot { background: var(--accent); box-shadow: 0 0 0 4px rgba(3,169,244,.14); }

.app-grid { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.camera-card, .panel { border: 1px solid var(--line); border-radius: 18px; background: var(--surface); box-shadow: var(--shadow); }
.camera-card { overflow: hidden; }

.camera-stage { position: relative; min-height: 360px; aspect-ratio: 16 / 9; overflow: hidden; background: #0d1420; user-select: none; }
#cameraVideo, #roiCanvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#cameraVideo { object-fit: cover; background: #0d1420; }
#roiCanvas { z-index: 2; max-width: none; touch-action: none; cursor: crosshair; }

.camera-empty { position: absolute; z-index: 1; inset: 0; display: grid; place-content: center; justify-items: center; gap: 8px; padding: 24px; color: #dce7f3; text-align: center; background: radial-gradient(circle at 50% 45%, #233247, #0d1420 70%); }
.camera-empty[hidden] { display: none; }
.camera-empty svg { width: 44px; height: 44px; margin-bottom: 4px; fill: none; stroke: #78c9ff; stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; }
.camera-empty strong { font-size: 1.05rem; }
.camera-empty span { color: #98a9bc; font-size: .8rem; }

.timer-display { position: absolute; z-index: 3; left: 16px; bottom: 16px; min-width: 150px; padding: 8px 12px; border: 1px solid rgba(255,255,255,.16); border-radius: 11px; color: #fff; background: rgba(3, 9, 18, .72); backdrop-filter: blur(8px); font-variant-numeric: tabular-nums; font-size: clamp(1.25rem, 3vw, 1.8rem); font-weight: 820; letter-spacing: -.03em; pointer-events: none; }
.motion-meter { position: absolute; z-index: 3; right: 16px; bottom: 16px; display: grid; grid-template-columns: 44px 110px; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid rgba(255,255,255,.16); border-radius: 10px; color: #dce7f3; background: rgba(3, 9, 18, .72); backdrop-filter: blur(8px); font-size: .72rem; font-weight: 750; font-variant-numeric: tabular-nums; pointer-events: none; }
.meter-track { height: 6px; overflow: hidden; border-radius: 999px; background: rgba(255,255,255,.17); }
.meter-track > span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #30bdf2, #23d17f); transition: width .12s linear; }

.primary-controls { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 8px; padding: 14px 14px 8px; }
.button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; min-height: 46px; padding: 9px 12px; border: 0; border-radius: 11px; cursor: pointer; font-size: .86rem; font-weight: 780; transition: transform .16s ease, box-shadow .16s ease, background .16s ease; }
.button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button:disabled { cursor: not-allowed; opacity: .48; }
.button-primary { color: #fff; background: linear-gradient(135deg, #0066cc, #0288d1); box-shadow: 0 8px 18px rgba(0,102,204,.18); }
.button-primary:hover:not(:disabled) { background: linear-gradient(135deg, #0056ad, #0277bd); }
.button-accent { color: #fff; background: linear-gradient(135deg, #12a874, #0e8e65); box-shadow: 0 8px 18px rgba(18,168,116,.17); }
.button-accent.running { background: linear-gradient(135deg, #e14e62, #c43d51); box-shadow: 0 8px 18px rgba(196,61,81,.18); }
.button-secondary { color: #334155; border: 1px solid #dce5ef; background: #f4f7fb; }
.button-secondary:hover:not(:disabled) { background: #eaf1f8; }
.camera-help { margin: 0; padding: 3px 16px 15px; color: var(--muted); font-size: .78rem; line-height: 1.5; }

.side-column { display: grid; gap: 16px; }
.panel { padding: 16px; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 13px; }
.panel-heading.compact { margin-bottom: 18px; }
.panel-heading h2 { margin: 0; color: #172033; font-size: 1.08rem; letter-spacing: -.025em; }
.lap-count { padding: 5px 8px; border-radius: 8px; color: var(--primary); background: #edf6ff; font-size: .68rem; font-weight: 850; }

.lap-list { display: grid; gap: 7px; max-height: 245px; margin: 0; padding: 0; overflow: auto; list-style: none; }
.lap-row { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 12px; min-height: 47px; padding: 8px 11px; border: 1px solid #e4ebf3; border-radius: 11px; background: var(--surface-soft); }
.lap-number { display: grid; place-items: center; width: 29px; height: 29px; border-radius: 8px; color: var(--primary); background: #e6f3ff; font-size: .72rem; font-weight: 850; }
.lap-time { justify-self: end; color: #243047; font-size: 1rem; font-weight: 820; font-variant-numeric: tabular-nums; }
.empty-row { padding: 28px 8px; color: #8291a5; font-size: .78rem; text-align: center; }

.icon-button { display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border: 1px solid #dce5ef; border-radius: 10px; color: #54708f; background: #f4f7fb; cursor: pointer; }
.icon-button:hover { color: var(--primary); background: #eaf4ff; }
.icon-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.field { display: grid; gap: 8px; margin-top: 15px; color: #334155; font-size: .82rem; }
.field > span { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.field output { color: var(--primary); font-weight: 850; font-variant-numeric: tabular-nums; }
.field small { color: #7a899d; font-size: .7rem; line-height: 1.4; }
.field select { width: 100%; min-height: 42px; padding: 0 11px; border: 1px solid #d5e0eb; border-radius: 10px; color: #334155; background: #fff; }
.field input[type="range"] { width: 100%; accent-color: var(--primary); }

.debug-panel { margin-top: 16px; padding: 10px; border: 1px solid #dce5ef; border-radius: 11px; background: #101923; }
.debug-heading { display: flex; justify-content: space-between; margin-bottom: 8px; color: #dbe7f3; font-size: .7rem; }
.debug-heading span { color: #8298ae; }
#debugCanvas { display: block; width: 100%; height: auto; max-height: 150px; border-radius: 7px; background: #000; object-fit: contain; }

.toast { position: fixed; z-index: 20; left: 50%; bottom: max(22px, env(safe-area-inset-bottom)); max-width: calc(100% - 32px); padding: 11px 15px; border-radius: 10px; color: #fff; background: rgba(17,24,39,.94); box-shadow: 0 12px 30px rgba(0,0,0,.18); font-size: .82rem; opacity: 0; pointer-events: none; transform: translate(-50%, 12px); transition: opacity .18s ease, transform .18s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

button:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible { outline: 3px solid rgba(3,169,244,.35); outline-offset: 3px; }

@media (max-width: 900px) {
  .app-grid { grid-template-columns: 1fr; }
  .side-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 680px) {
  .header-inner, .workspace { width: min(100% - 20px, 1180px); }
  .header-inner { min-height: 64px; }
  .brand::before { width: 38px; height: 38px; flex-basis: 38px; border-radius: 9px; }
  .brand-copy small { display: none; }
  .home-link { width: 40px; padding: 0; gap: 0; }
  .home-link span { display: none; }
  .home-link svg { width: 19px; height: 19px; }
  .workspace { padding-top: 18px; }
  .app-heading { align-items: start; flex-direction: column; gap: 10px; }
  .status-pill { min-height: 34px; }
  .camera-stage { min-height: 240px; aspect-ratio: 4 / 3; }
  .primary-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .side-column { grid-template-columns: 1fr; }
  .motion-meter { right: 10px; bottom: 10px; grid-template-columns: 40px 66px; }
  .timer-display { left: 10px; bottom: 10px; min-width: 125px; font-size: 1.2rem; }
}

@media (orientation: landscape) and (max-height: 560px) {
  .site-header { display: none; }
  .workspace { width: calc(100% - 16px); padding: 8px 0; }
  .app-heading { display: none; }
  .app-grid { grid-template-columns: minmax(0, 1fr) 275px; gap: 8px; }
  .camera-stage { min-height: 0; height: calc(100vh - 78px); aspect-ratio: auto; }
  .camera-card, .panel { border-radius: 12px; }
  .primary-controls { padding: 7px; gap: 6px; }
  .button { min-height: 42px; padding: 7px 8px; font-size: .76rem; }
  .camera-help { display: none; }
  .side-column { display: block; }
  .settings-panel { display: none; }
  .lap-panel { height: calc(100vh - 16px); }
  .lap-list { max-height: calc(100vh - 86px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
