/* ============================================================
   KiCad Mobile Lite — PWA shell styles
   Dark theme, mobile-first. All interactive targets ≥ 44px.
   Safe-area insets via env() so the UI clears notches/gestures.
   ============================================================ */

:root {
  --bg: #0b0e14;
  --surface: #141a24;
  --surface-2: #1c2431;
  --border: #2a3444;
  --text: #e6edf3;
  --muted: #8b94a3;
  --accent: #4da3ff;
  --accent-ink: #06121f;
  --good: #38d17c;
  --warn: #ff5d5d;

  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);

  --toolbar-h: 60px;
  --touch-min: 44px; /* minimum touch target */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  /* No global touch-action override here: gestures over the viewer iframe
     must flow through to it untouched. */
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

/* ---------------- Header toolbar ---------------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--toolbar-h);
  padding: calc(var(--sat) + 6px) calc(var(--sar) + 8px) 6px calc(var(--sal) + 8px);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  z-index: 30;
}

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

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  flex: 0 0 auto;
  background:
    linear-gradient(135deg, var(--accent) 0%, #7c5cff 100%);
  box-shadow: 0 0 12px rgba(77, 163, 255, .45);
}

.brand-name {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: .02em;
  white-space: nowrap;
}

.tool-group {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
  margin-inline: auto;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

/* Every button is ≥ 44px in both dimensions. */
.tool-btn {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  padding: 6px 10px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.tool-btn:active { transform: scale(.96); }

.tool-group .tool-btn.is-active {
  background: var(--accent);
  color: var(--accent-ink);
}

.tool-group .tool-btn:not(.is-active):hover { background: rgba(255,255,255,.06); }

.toolbar-actions .tool-btn {
  background: var(--surface-2);
  border-color: var(--border);
}

#btn-3d[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
}

.wasm-status {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--muted);
  border: 1px dashed var(--border);
  border-radius: 999px;
  padding: 6px 10px;
  white-space: nowrap;
}

/* ---------------- Viewport (iframe host) ---------------- */

.viewport {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  background: #05070b;
  /* Deliberately NOT touch-action:none: the iframe owns all gestures here. */
  touch-action: auto;
}

#viewer-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: #05070b;
}

.viewport-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  padding: 24px;
  color: var(--muted);
}

.viewport-fallback p { margin: 0; }
.viewport-fallback .muted,
.muted { color: var(--muted); font-size: 13px; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .92em; }

/* ---------------- Bottom-sheet drawer ---------------- */

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  z-index: 40;
  opacity: 1;
  transition: opacity .2s ease;
}

.drawer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  max-height: min(72dvh, 560px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -12px 40px rgba(0, 0, 0, .5);
  transform: translateY(102%);
  transition: transform .24s ease-out;
  will-change: transform;
  /* Vertical swipe gestures belong to the drawer; horizontal stays free. */
  touch-action: pan-x;
  padding-bottom: var(--sab);
}

.drawer.is-open { transform: translateY(0); }

.drawer-handle {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch-min); /* big grab target */
  cursor: grab;
  touch-action: none; /* the handle is the drag surface */
}

.drawer-handle span {
  width: 44px;
  height: 5px;
  border-radius: 999px;
  background: var(--border);
}

.drawer-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px 8px 16px;
  border-bottom: 1px solid var(--border);
}

.drawer-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
}

.drawer-close {
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.drawer-body {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px 16px;
}

.model-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.model-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: var(--touch-min);
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 15px;
  text-align: left;
  cursor: pointer;
}

.model-item:active { background: #232d3d; }

.model-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
}

.badge-3d {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .06em;
  color: var(--accent-ink);
  background: var(--accent);
  border-radius: 6px;
  padding: 4px 8px;
}

.library-empty, .library-error {
  text-align: center;
  color: var(--muted);
  padding: 32px 16px;
}

.library-empty p, .library-error p { margin: 6px 0; }

.noscript {
  padding: 24px;
  text-align: center;
}

/* ---------------- Small screens ---------------- */

@media (max-width: 420px) {
  .brand-name { display: none; }          /* icon mark is enough on tiny screens */
  .tool-btn .tool-label { display: none; } /* icons only, keeps targets ≥44px */
  .tool-btn { padding: 6px 0; }
  .tool-group .tool-btn { min-width: 52px; }
  .wasm-status { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .drawer, .scrim { transition: none; }
}

/* Editor mode: the KiCad WASM core sizes its top-level frame as
   innerWidth x innerHeight (pcbjam's full-viewport assumption). The shell
   toolbar is hidden so #wasm-view truly fills the viewport — otherwise the
   bottom ~67px of the editor (the KiCad status bar) would be clipped.
   Navigation lives on in the floating #editor-nav pill. */
body.editor-active .toolbar {
  display: none;
}

#editor-nav {
  position: fixed;
  left: 50%;
  bottom: calc(var(--sab, 0px) + 12px);
  transform: translateX(-50%);
  z-index: 34; /* above the editor, below its dialogs (#window-container: 35) */
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: rgba(13, 17, 23, 0.88);
  border: 1px solid var(--border);
  border-radius: 999px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
}

#editor-nav[hidden] {
  display: none;
}

.editor-nav-status {
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
  padding: 0 4px;
}

/* ---------------- WASM editor viewport (Phase 2) ---------------- */

.wasm-view {
  position: absolute;
  inset: 0;
  background: #05070b;
  overflow: hidden;
  /* The editor canvas owns all gestures here, like the viewer iframe does. */
  touch-action: none;
}

.wasm-view[hidden],
#viewer-frame[hidden] { display: none; }

/* wx.js DOM contract (see web/wasm-bridge.js ensureWxDom): #main-window is
   the top-level (id=0) window owning #canvas; #window-container parents all
   child windows (dialogs, popups). Both are BODY-LEVEL siblings (mirroring
   pcbjam's harness) positioned over #wasm-view's box by syncWxBox() — the
   wasm computes its frame as innerHeight - mainWindow.offsetTop, so the
   div must sit at the real viewport offset (below the shell toolbar).
   #window-container z-index sits above the shell toolbar (30) but below the
   scrim (40)/drawer (50); !important beats wx.js's own inline zIndex='1'. */
#main-window {
  position: absolute;
  overflow: hidden;
  background: #05070b;
  /* The editor canvas owns all gestures here, like the viewer iframe does. */
  touch-action: none;
}

#main-window canvas#canvas {
  position: absolute;
  inset: 0;
  display: block;
}

#window-container {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 35 !important;
  pointer-events: none;
}

/* Child windows inside the container must still receive events. */
#window-container > * {
  pointer-events: auto;
}

.wasm-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
  background: rgba(5, 7, 11, 0.94);
  color: var(--text, #e8ecf4);
}

.wasm-overlay.is-done { display: none; }

.wasm-overlay-status {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.wasm-overlay-sub {
  margin: 0;
  min-height: 1.2em;
}

.wasm-progress {
  width: min(320px, 80%);
  height: 8px;
  border-radius: 4px;
  background: #1c2330;
  overflow: hidden;
}

.wasm-progress[hidden] { display: none; }

.wasm-progress-bar {
  height: 100%;
  width: 0;
  background: #4da3ff;
  border-radius: 4px;
  transition: width 0.2s ease;
}

.wasm-overlay-links { margin: 0; font-size: 13px; }
.wasm-overlay-links a { color: #4da3ff; }

#btn-unlock[hidden] { display: none; }

/* ---------------- Capability check page ---------------- */

.cap-page {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(20px + env(safe-area-inset-top)) 20px 40px;
  color: var(--text, #e8ecf4);
}

.cap-page h1 { font-size: 22px; margin: 0 0 6px; }
.cap-page .lede { color: var(--muted, #9aa4b5); margin: 0 0 20px; }

.cap-list { list-style: none; margin: 0 0 20px; padding: 0; display: grid; gap: 10px; }

.cap-item {
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 10px;
  align-items: start;
  padding: 12px 14px;
  border: 1px solid #232c3d;
  border-radius: 10px;
  background: #0d1119;
}

.cap-item .cap-mark { font-size: 18px; line-height: 1.3; }
.cap-item.pass .cap-mark { color: #38d17c; }
.cap-item.fail .cap-mark { color: #ff5d5d; }

.cap-item .cap-name { font-weight: 600; margin-bottom: 2px; }
.cap-item .cap-detail { font-size: 13px; color: var(--muted, #9aa4b5); }

.cap-verdict {
  padding: 14px;
  border-radius: 10px;
  font-weight: 600;
  margin-bottom: 20px;
}
.cap-verdict.pass { background: rgba(56, 209, 124, 0.12); border: 1px solid #38d17c; }
.cap-verdict.fail { background: rgba(255, 93, 93, 0.10); border: 1px solid #ff5d5d; }

.cap-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.cap-actions .tool-btn { text-decoration: none; padding: 10px 16px; }
