:root {
  --bg: #000;
  --panel: rgba(18, 20, 24, 0.92);
  --text: #f2f4f7;
  --muted: #9aa4b2;
  --accent: #1f6feb;
  --ok: #2ea043;
  --bad: #f85149;
  --warn: #d29922;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

button, select {
  font: inherit;
  color: inherit;
  background: #2a2f37;
  border: 1px solid #3b424d;
  border-radius: 10px;
  padding: 10px 14px;
}
button:active { filter: brightness(1.3); }
button.primary { background: var(--accent); border-color: var(--accent); font-weight: 700; font-size: 1.2rem; padding: 14px 40px; }
button:disabled { opacity: 0.5; }

.hidden { display: none !important; }

#stage { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; }
#preview { width: 100%; height: 100%; object-fit: contain; background: #000; }

#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 10px) 8px calc(env(safe-area-inset-left) + 10px);
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel); border-radius: 999px; padding: 6px 12px;
  font-size: 0.85rem; white-space: nowrap;
}
.pill.warn { color: var(--warn); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); display: inline-block; }
#conn[data-state="ok"] .dot { background: var(--ok); }
#conn[data-state="offline"] .dot, #conn[data-state="unauthorized"] .dot { background: var(--bad); }

.icon-btn { margin-left: auto; width: 40px; height: 40px; padding: 0; border-radius: 50%; background: var(--panel); font-size: 1.2rem; }

#controls {
  position: fixed; bottom: 0; left: 0; right: 0;
  padding: 10px calc(env(safe-area-inset-right) + 10px) calc(env(safe-area-inset-bottom) + 12px) calc(env(safe-area-inset-left) + 10px);
  display: flex; gap: 10px; justify-content: flex-end;
}
#controls button { background: var(--panel); }
#controls button.accent { background: var(--accent); border-color: var(--accent); font-weight: 700; }
#btn-auto { margin-right: auto; }
#btn-auto.on { border-color: var(--ok); color: #7ee787; }

#region-box {
  position: absolute; border: 2px dashed var(--ok); border-radius: 4px;
  pointer-events: none; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
}

#result {
  position: fixed; z-index: 10; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: rgba(14, 16, 20, 0.94); border-radius: 14px; padding: 12px 14px;
  left: calc(env(safe-area-inset-left) + 10px); right: calc(env(safe-area-inset-right) + 10px);
  bottom: calc(env(safe-area-inset-bottom) + 70px); max-height: 50vh;
}
@media (orientation: landscape) {
  #result {
    left: auto; width: min(46vw, 520px);
    top: calc(env(safe-area-inset-top) + 56px); bottom: calc(env(safe-area-inset-bottom) + 70px); max-height: none;
  }
}
.result-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }
.result-actions { display: inline-flex; gap: 4px; align-items: center; flex-shrink: 0; }
.result-head button { padding: 4px 8px; background: transparent; border: none; font-size: 0.85rem; color: var(--muted); }
#btn-result-close { font-size: 1.1rem; color: var(--text); }
#btn-speech-stop { display: none; }
#result.speaking #btn-speech-stop { display: inline-block; color: var(--warn); }
#result.speaking #result-title::after { content: " · 읽는 중"; color: var(--muted); font-weight: normal; font-size: 0.85rem; }
.section-title { color: var(--muted); font-size: 0.8rem; margin: 16px 0 4px; letter-spacing: 0.02em; }
.row input[type="range"] { flex: 1; }
.row input[type="checkbox"] { width: 20px; height: 20px; }
.q { padding: 10px 0; border-top: 1px solid #2a2f37; }
.q:first-child { border-top: none; }
.q-head { font-size: 1.1rem; }
.q-head .ans { color: #7ee787; font-weight: 700; }
.q-ans-text { color: var(--muted); font-size: 0.9rem; margin-top: 2px; }
.q-exp { margin-top: 6px; line-height: 1.55; }
.badge { display: inline-block; font-size: 0.75rem; padding: 2px 8px; border-radius: 999px; margin-left: 6px; vertical-align: middle; }
.badge.low { background: rgba(210, 153, 34, 0.2); color: var(--warn); }
.result-msg { font-size: 1.05rem; line-height: 1.6; padding: 6px 0; }
#result-meta { margin-top: 8px; }

.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.88);
  display: flex; align-items: center; justify-content: center; z-index: 20;
}
.overlay-box { max-width: 520px; padding: 24px; text-align: center; }
.overlay-box h1 { font-size: 1.5rem; margin: 0 0 12px; }
.overlay-box p { line-height: 1.6; }
.sub { color: var(--muted); font-size: 0.85rem; line-height: 1.5; }
.error { color: var(--bad); }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  max-height: 85vh; overflow-y: auto;
  background: #15181d; border-radius: 16px 16px 0 0;
  padding: 14px calc(env(safe-area-inset-right) + 16px) calc(env(safe-area-inset-bottom) + 16px) calc(env(safe-area-inset-left) + 16px);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.6);
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.row { display: flex; gap: 10px; align-items: center; margin: 10px 0; }
.row select { flex: 1; min-width: 0; }
.row.buttons { flex-wrap: wrap; }
#info-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-top: 8px; }
#info-table th { text-align: left; color: var(--muted); font-weight: normal; padding: 5px 8px 5px 0; white-space: nowrap; vertical-align: top; }
#info-table td { padding: 5px 0; word-break: break-all; }

#still-viewer { flex-direction: column; align-items: stretch; justify-content: flex-start; background: #000; }
#still-scroll { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
#still-img { display: block; max-width: none; }
#still-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px calc(env(safe-area-inset-right) + 12px) calc(env(safe-area-inset-bottom) + 10px) calc(env(safe-area-inset-left) + 12px);
  background: var(--panel); font-size: 0.85rem;
}

#toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 80px); transform: translateX(-50%);
  background: var(--panel); padding: 10px 16px; border-radius: 10px; z-index: 40; max-width: 90vw; text-align: center;
}
