@import url('https://fonts.googleapis.com/css2?family=Rajdhani:wght@400;500;600;700&family=Share+Tech+Mono&family=Barlow+Condensed:wght@300;400;500;600&display=swap');

/* ═══════════════════════════════════════════════════════════
   DESIGN TOKENS — DARK MODE (default)
═══════════════════════════════════════════════════════════ */
:root,
[data-theme="dark"] {
  --bg-primary:      #0d0f12;
  --bg-secondary:    #111418;
  --bg-panel:        #151a20;
  --bg-card:         #1a2028;
  --bg-input:        #0f1319;
  --bg-overlay:      rgba(13,15,18,0.88);
  --bg-overlay-75:   rgba(13,15,18,0.78);
  --bg-overlay-70:   rgba(13,15,18,0.72);
  --border-subtle:   #1e2530;
  --border-active:   #2a3545;
  --border-accent:   #c8850a;
  --accent-amber:    #e09020;
  --accent-amber-dim:#c8850a;
  --accent-amber-glow: rgba(224,144,32,0.15);
  --accent-cyan:     #3ab8c8;
  --accent-cyan-dim: #2a8a98;
  --text-primary:    #d8dde6;
  --text-secondary:  #8a95a3;
  --text-muted:      #5a6a7a;
  --text-label:      #7a8a9a;
  --text-accent:     #e09020;
  --text-value:      #f0f4f8;
  --success:         #3dba6a;
  --error:           #e04040;
  --error-dim:       rgba(224,64,64,0.15);
  --font-display:    'Rajdhani', sans-serif;
  --font-mono:       'Share Tech Mono', monospace;
  --font-body:       'Barlow Condensed', sans-serif;

  /* Layout tokens — overridden at breakpoints */
  --panel-width:     320px;
  --toolbar-height:  44px;
  --results-height:  90px;
  --tab-bar-height:  0px;       /* 0 on desktop, set on mobile */
  --drawer-peek:     72px;      /* how much drawer peeks above tab bar */
  --safe-bottom:     env(safe-area-inset-bottom, 0px);
  --radius:          4px;
  --radius-sm:       2px;
}

/* ═══════════════════════════════════════════════════════════
   DESIGN TOKENS — LIGHT MODE
═══════════════════════════════════════════════════════════ */
[data-theme="light"] {
  --bg-primary:      #f0f2f5;
  --bg-secondary:    #e4e8ee;
  --bg-panel:        #f8f9fb;
  --bg-card:         #ffffff;
  --bg-input:        #eef1f5;
  --bg-overlay:      rgba(240,242,245,0.90);
  --bg-overlay-75:   rgba(240,242,245,0.80);
  --bg-overlay-70:   rgba(240,242,245,0.75);
  --border-subtle:   #d4dae3;
  --border-active:   #b8c0cc;
  --border-accent:   #c8850a;
  --accent-amber:    #b06a08;
  --accent-amber-dim:#9a5c06;
  --accent-amber-glow: rgba(176,106,8,0.12);
  --accent-cyan:     #1a7a8a;
  --accent-cyan-dim: #0e5a68;
  --text-primary:    #17212b;
  --text-secondary:  #3d4a5a;
  --text-muted:      #6a7a8a;
  --text-label:      #5a6a7a;
  --text-accent:     #b06a08;
  --text-value:      #0a0e14;
  --success:         #1a7a3a;
  --error:           #b03030;
  --error-dim:       rgba(176,48,48,0.12);
}

/* ═══════════════════════════════════════════════════════════
   RESET
═══════════════════════════════════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 15px;
  letter-spacing: 0.02em;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

/* ═══════════════════════════════════════════════════════════
   DESKTOP LAYOUT  (>1024px)
═══════════════════════════════════════════════════════════ */
#app {
  display: grid;
  grid-template-columns: var(--panel-width) 1fr;
  grid-template-rows: var(--toolbar-height) 1fr var(--results-height);
  height: 100vh;
  width: 100vw;
}

/* ─── Toolbar ────────────────────────────────────────────── */
#toolbar {
  grid-column: 1 / -1;
  grid-row: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px 0 20px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 200;
  /* stay above drawer overlay */
}

.toolbar-brand { display: flex; align-items: center; gap: 10px; }

.brand-icon {
  width: 28px; height: 28px;
  background: var(--accent-amber);
  clip-path: polygon(0 30%,30% 0,100% 0,100% 70%,70% 100%,0 100%);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.brand-icon svg { width: 16px; height: 16px; fill: #000; }

.brand-name {
  font-family: var(--font-display);
  font-size: 18px; font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--text-value);
  text-transform: uppercase;
  white-space: nowrap;
}
.brand-name span { color: var(--accent-amber); }

.brand-tag {
  font-family: var(--font-mono);
  font-size: 10px; color: var(--text-muted);
  letter-spacing: 0.15em; text-transform: uppercase;
  margin-top: 1px;
}

.toolbar-actions { display: flex; align-items: center; gap: 8px; }

.toolbar-status {
  font-family: var(--font-mono); font-size: 10px;
  color: var(--text-muted); letter-spacing: 0.08em;
  display: flex; align-items: center; gap: 6px;
}

/* Panel toggle button — hidden on desktop, shown on tablet */
#btn-panel-toggle {
  display: none;
  align-items: center; gap: 6px;
  font-family: var(--font-mono); font-size: 10px;
  letter-spacing: 0.1em; text-transform: uppercase;
  padding: 5px 10px;
  background: transparent;
  border: 1px solid var(--border-active);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
#btn-panel-toggle:hover { border-color: var(--accent-amber-dim); color: var(--accent-amber); }
#btn-panel-toggle.active { border-color: var(--accent-amber); color: var(--accent-amber); background: var(--accent-amber-glow); }
#btn-panel-toggle svg { width: 14px; height: 14px; flex-shrink: 0; }
#btn-panel-toggle span { display: none; } /* hide text, show icon only */

/* ─── Theme Toggle ────────────────────────────────────────── */
.theme-toggle {
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  background: transparent;
  border: 1px solid var(--border-active);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s ease;
  flex-shrink: 0;
  position: relative;
}
.theme-toggle:hover {
  border-color: var(--accent-amber-dim);
  color: var(--accent-amber);
  background: var(--accent-amber-glow);
}
.theme-toggle svg {
  width: 16px; height: 16px;
  position: absolute;
  transition: opacity 0.25s ease, transform 0.3s ease;
}
.theme-icon-sun { opacity: 0; transform: rotate(-90deg) scale(0.6); }
.theme-icon-moon { opacity: 1; transform: rotate(0) scale(1); }
[data-theme="light"] .theme-icon-sun { opacity: 1; transform: rotate(0) scale(1); }
[data-theme="light"] .theme-icon-moon { opacity: 0; transform: rotate(90deg) scale(0.6); }

.status-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--success); box-shadow: 0 0 6px var(--success);
  animation: pulse-dot 2s ease infinite;
}
@keyframes pulse-dot { 0%,100%{opacity:1}50%{opacity:.5} }

/* ─── Input Panel ────────────────────────────────────────── */
#input-panel {
  grid-column: 1; grid-row: 2;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-subtle);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex; flex-direction: column;
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1),
              box-shadow 0.3s ease;
  /* on tablet this collapses: */
  will-change: transform;
}

#input-panel::-webkit-scrollbar { width: 4px; }
#input-panel::-webkit-scrollbar-track { background: transparent; }
#input-panel::-webkit-scrollbar-thumb { background: var(--border-active); border-radius: 2px; }

/* Panel scroll area — used on mobile inside drawer */
.panel-scroll-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* ─── Sections ───────────────────────────────────────────── */
.panel-section {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.panel-section:last-child { border-bottom: none; }

.section-header {
  display: flex; align-items: center;
  justify-content: space-between; margin-bottom: 12px;
}

.section-label {
  font-family: var(--font-mono); font-size: 10px;
  letter-spacing: 0.2em; text-transform: uppercase;
  color: var(--text-muted);
  display: flex; align-items: center; gap: 8px;
}
.section-label::before {
  content: ''; display: block;
  width: 3px; height: 10px; background: var(--accent-amber);
}

.section-badge {
  font-family: var(--font-mono); font-size: 8px;
  padding: 2px 6px; border-radius: 2px;
  background: var(--bg-input); color: var(--text-muted);
  border: 1px solid var(--border-subtle);
}

/* ─── Duct Type Grid ─────────────────────────────────────── */
.duct-type-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.duct-type-btn {
  display: flex; flex-direction: column;
  align-items: center; gap: 6px;
  padding: 10px 8px;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.2s ease;
  color: var(--text-muted);
  position: relative; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.duct-type-btn::after {
  content: ''; position: absolute; inset: 0;
  background: var(--accent-amber-glow); opacity: 0;
  transition: opacity 0.2s;
}
.duct-type-btn:hover { border-color: var(--border-active); color: var(--text-secondary); }
.duct-type-btn.active { border-color: var(--accent-amber); color: var(--accent-amber); background: var(--bg-card); }
.duct-type-btn.active::after { opacity: 1; }
.duct-type-btn svg { width: 32px; height: 22px; position: relative; z-index: 1; }
.duct-type-btn .btn-label {
  font-family: var(--font-mono); font-size: 9px;
  letter-spacing: 0.1em; text-transform: uppercase;
  position: relative; z-index: 1;
  text-align: center; line-height: 1.3;
}

/* ─── Unit Selector ──────────────────────────────────────── */
.unit-selector { display: flex; gap: 4px; flex-wrap: wrap; }

.unit-btn {
  flex: 1; min-width: 0;
  padding: 7px 4px;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-family: var(--font-mono); font-size: 9px;
  letter-spacing: 0.08em; cursor: pointer;
  transition: all 0.15s; text-align: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  white-space: nowrap;
}
.unit-btn:hover { border-color: var(--border-active); color: var(--text-secondary); }
.unit-btn.active { background: var(--bg-card); border-color: var(--accent-amber-dim); color: var(--accent-amber); }

/* ─── Input Fields ───────────────────────────────────────── */
.input-group { margin-bottom: 10px; }
.input-group:last-child { margin-bottom: 0; }

.input-label {
  font-family: var(--font-mono); font-size: 10px;
  letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--text-label); margin-bottom: 5px;
  display: flex; justify-content: space-between; align-items: center;
}
.input-hint { color: var(--text-muted); font-size: 9px; }

.input-row {
  display: flex; align-items: center;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius); overflow: hidden;
  transition: border-color 0.15s; position: relative;
}
.input-row:focus-within { border-color: var(--accent-amber-dim); box-shadow: 0 0 0 1px var(--accent-amber-dim) inset; }
.input-row.error { border-color: var(--error); box-shadow: 0 0 0 1px var(--error) inset; }

.input-row input {
  flex: 1; background: transparent; border: none; outline: none;
  padding: 9px 10px;
  color: var(--text-value);
  font-family: var(--font-mono); font-size: 13px;
  letter-spacing: 0.05em; width: 100%;
  /* Larger touch target on mobile */
}
.input-row input::placeholder { color: var(--text-muted); font-size: 11px; }

.input-unit {
  padding: 0 10px; font-family: var(--font-mono); font-size: 9px;
  color: var(--text-muted); letter-spacing: 0.08em;
  border-left: 1px solid var(--border-subtle);
  height: 100%; display: flex; align-items: center;
  white-space: nowrap; min-width: 36px; justify-content: center;
}

.input-error {
  font-family: var(--font-mono); font-size: 9px;
  color: var(--error); margin-top: 3px;
  letter-spacing: 0.06em; min-height: 13px;
}

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.dimension-divider {
  display: flex; align-items: center; gap: 8px; margin: 8px 0;
}
.dimension-divider::before, .dimension-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border-subtle);
}
.dimension-divider span {
  font-family: var(--font-mono); font-size: 8px;
  color: var(--text-muted); letter-spacing: 0.15em; text-transform: uppercase; white-space: nowrap;
}

/* ─── Calculate Button ───────────────────────────────────── */
.calc-btn {
  width: 100%; padding: 12px 16px;
  background: var(--accent-amber); border: none;
  border-radius: var(--radius);
  color: #000; font-family: var(--font-display);
  font-size: 14px; font-weight: 700;
  letter-spacing: 0.2em; text-transform: uppercase;
  cursor: pointer; transition: all 0.2s ease;
  position: relative; overflow: hidden;
  clip-path: polygon(0 0,calc(100% - 10px) 0,100% 10px,100% 100%,10px 100%,0 calc(100% - 10px));
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  /* Larger on touch */
  min-height: 44px;
}
.calc-btn::before {
  content: ''; position: absolute; inset: 0;
  background: rgba(255,255,255,0.15);
  transform: translateX(-100%); transition: transform 0.3s ease;
}
.calc-btn:hover::before { transform: translateX(0); }
.calc-btn:hover { background: #f0a030; }
.calc-btn:active { transform: scale(0.98); }

/* ─── Material ───────────────────────────────────────────── */
.material-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 0; border-bottom: 1px solid var(--border-subtle);
}
.material-row:last-child { border-bottom: none; }
.material-key { font-family: var(--font-mono); font-size: 9px; color: var(--text-muted); letter-spacing: 0.1em; }
.material-val { font-family: var(--font-mono); font-size: 10px; color: var(--text-secondary); }

.material-select {
  background: var(--bg-input); border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm); color: var(--text-secondary);
  font-family: var(--font-mono); font-size: 9px;
  padding: 3px 6px; outline: none; cursor: pointer;
}
.material-select option { background: var(--bg-panel); }

/* ═══════════════════════════════════════════════════════════
   3D VIEWER
═══════════════════════════════════════════════════════════ */
#viewer-container {
  grid-column: 2; grid-row: 2;
  position: relative; overflow: hidden;
  background: var(--bg-primary);
}

#three-canvas { display: block; width: 100%; height: 100%; }

.viewer-toolbar {
  position: absolute; top: 12px; right: 12px;
  display: flex; flex-direction: column; gap: 6px; z-index: 10;
}

.viewer-btn {
  font-family: var(--font-mono); font-size: 9px;
  letter-spacing: 0.1em; text-transform: uppercase;
  padding: 7px 10px;
  background: var(--bg-overlay);
  border: 1px solid var(--border-active);
  color: var(--text-secondary); border-radius: var(--radius-sm);
  cursor: pointer; transition: all 0.15s;
  backdrop-filter: blur(4px);
  display: flex; align-items: center; gap: 5px; white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  min-height: 34px;
}
.viewer-btn:hover { border-color: var(--accent-amber-dim); color: var(--accent-amber); }
.viewer-btn.active { border-color: var(--accent-amber); color: var(--accent-amber); background: var(--accent-amber-glow); }
.viewer-btn .vb-icon { width: 12px; height: 12px; flex-shrink: 0; }

.viewer-corner-info {
  position: absolute; bottom: 12px; left: 12px;
  font-family: var(--font-mono); font-size: 9px;
  color: var(--text-muted); letter-spacing: 0.08em; line-height: 1.8;
  background: var(--bg-overlay-75); padding: 8px 10px;
  border-radius: var(--radius); border: 1px solid var(--border-subtle);
  backdrop-filter: blur(4px);
  max-width: calc(100% - 24px);
}
.viewer-corner-info .ci-row { display: flex; gap: 8px; }
.viewer-corner-info .ci-key { color: var(--text-muted); }
.viewer-corner-info .ci-val { color: var(--text-secondary); }

.viewer-type-badge {
  position: absolute; top: 12px; left: 12px;
  font-family: var(--font-display); font-size: 12px; font-weight: 600;
  letter-spacing: 0.15em; text-transform: uppercase;
  color: var(--accent-amber); background: var(--bg-overlay);
  padding: 6px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-accent); backdrop-filter: blur(4px);
  pointer-events: none;
}

.viewer-empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px; pointer-events: none; transition: opacity 0.3s;
}
.viewer-empty.hidden { opacity: 0; }
.empty-icon { width: 64px; height: 64px; opacity: 0.12; }
.empty-text {
  font-family: var(--font-mono); font-size: 11px;
  color: var(--text-muted); letter-spacing: 0.15em;
  text-transform: uppercase; text-align: center; opacity: 0.5;
}

/* ═══════════════════════════════════════════════════════════
   RESULTS PANEL
═══════════════════════════════════════════════════════════ */
#results-panel {
  grid-column: 1 / -1; grid-row: 3;
  background: var(--bg-secondary); border-top: 1px solid var(--border-subtle);
  display: flex; align-items: stretch; overflow: hidden;
}

.result-card {
  flex: 1; display: flex; flex-direction: column;
  justify-content: center; padding: 0 24px;
  border-right: 1px solid var(--border-subtle);
  position: relative; transition: background 0.2s;
  min-width: 0; /* allow shrink */
}
.result-card:last-child { border-right: none; }

.result-card::before {
  content: ''; position: absolute; top: 0; left: 0; right: 0;
  height: 2px; background: var(--accent-amber);
  transform: scaleX(0); transition: transform 0.4s ease; transform-origin: left;
}
.result-card.has-value::before { transform: scaleX(1); }

.result-label {
  font-family: var(--font-mono); font-size: 8px;
  letter-spacing: 0.2em; text-transform: uppercase;
  color: var(--text-muted); margin-bottom: 4px; white-space: nowrap;
}
.result-value {
  font-family: var(--font-display); font-size: 26px; font-weight: 700;
  color: var(--text-value); letter-spacing: 0.02em; line-height: 1;
  transition: color 0.3s; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis;
}
.result-card.has-value .result-value { color: var(--accent-amber); }
.result-unit { font-family: var(--font-mono); font-size: 10px; color: var(--text-muted); letter-spacing: 0.08em; margin-top: 3px; }

/* Area unit select dropdown inside result cards */
.area-unit-select {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--text-muted);
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 1px 4px;
  cursor: pointer;
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='6'%3E%3Cpath d='M0 0l4 6 4-6z' fill='%235a6a7a'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 4px center;
  padding-right: 16px;
  min-width: 48px;
}
.area-unit-select:hover {
  border-color: var(--border-active);
  color: var(--text-secondary);
}
.area-unit-select:focus {
  border-color: var(--accent-amber-dim);
  color: var(--accent-amber);
}
.area-unit-select option {
  background: var(--bg-panel);
  color: var(--text-secondary);
}
.result-sub { font-family: var(--font-mono); font-size: 8px; color: var(--text-muted); letter-spacing: 0.06em; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.wastage-badge {
  display: inline-flex; align-items: center; gap: 3px;
  font-family: var(--font-mono); font-size: 8px;
  color: var(--accent-cyan-dim); border: 1px solid var(--accent-cyan-dim);
  border-radius: 2px; padding: 1px 5px; margin-top: 4px;
}

/* ═══════════════════════════════════════════════════════════
   DIMENSION OVERLAY
═══════════════════════════════════════════════════════════ */
#dim-overlay {
  position: absolute; inset: 0;
  pointer-events: none; overflow: visible;
}
.dim-label { font-family: var(--font-mono); font-size: 10px; fill: var(--accent-amber); letter-spacing: 0.06em; }
.dim-line { stroke: var(--accent-amber); stroke-width: 1; opacity: 0.6; }
.dim-arrow { fill: var(--accent-amber); opacity: 0.6; }

/* ═══════════════════════════════════════════════════════════
   LOADING
═══════════════════════════════════════════════════════════ */
.computing-overlay {
  position: absolute; inset: 0; background: var(--bg-overlay-70);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; opacity: 0; pointer-events: none;
  transition: opacity 0.2s; backdrop-filter: blur(2px);
}
.computing-overlay.active { opacity: 1; pointer-events: all; }
.computing-spinner {
  width: 32px; height: 32px;
  border: 2px solid var(--border-active); border-top-color: var(--accent-amber);
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ═══════════════════════════════════════════════════════════
   MOBILE DRAWER (hidden on desktop)
═══════════════════════════════════════════════════════════ */
#drawer-overlay {
  display: none; /* mobile only */
  position: fixed; inset: 0; z-index: 299;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(2px);
  opacity: 0; pointer-events: none;
  transition: opacity 0.3s ease;
}
#drawer-overlay.visible { opacity: 1; pointer-events: all; }

/* Drawer handle */
.drawer-handle {
  display: none; /* mobile only */
  width: 100%; padding: 10px 0 6px;
  flex-direction: column; align-items: center; gap: 4px;
  cursor: grab; flex-shrink: 0; touch-action: none;
  user-select: none; -webkit-user-select: none;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
}
.drawer-handle-bar {
  width: 36px; height: 3px;
  background: var(--border-active); border-radius: 2px;
}
.drawer-handle-label {
  font-family: var(--font-mono); font-size: 8px;
  color: var(--text-muted); letter-spacing: 0.2em; text-transform: uppercase;
}

/* Mobile tab bar */
#mobile-tab-bar {
  display: none; /* mobile only */
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(56px + var(--safe-bottom));
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-subtle);
  z-index: 300;
  padding-bottom: var(--safe-bottom);
}
.tab-bar-inner {
  display: flex; height: 56px;
}
.tab-btn {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 3px;
  background: transparent; border: none; cursor: pointer;
  color: var(--text-muted); transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; position: relative;
}
.tab-btn::before {
  content: ''; position: absolute; top: 0; left: 20%; right: 20%;
  height: 2px; background: var(--accent-amber);
  transform: scaleX(0); transition: transform 0.25s ease;
}
.tab-btn.active { color: var(--accent-amber); }
.tab-btn.active::before { transform: scaleX(1); }
.tab-btn svg { width: 20px; height: 20px; }
.tab-btn span { font-family: var(--font-mono); font-size: 8px; letter-spacing: 0.12em; text-transform: uppercase; }

/* Mobile results strip (compact) */
#mobile-results-strip {
  display: none; /* mobile only */
  position: fixed;
  z-index: 298;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-subtle);
}

/* ═══════════════════════════════════════════════════════════
   MISC
═══════════════════════════════════════════════════════════ */
.tooltip { position: relative; }
.tooltip::after {
  content: attr(data-tip); position: absolute;
  bottom: calc(100% + 6px); left: 50%; transform: translateX(-50%);
  background: var(--bg-card); color: var(--text-secondary);
  font-family: var(--font-mono); font-size: 9px; letter-spacing: 0.06em;
  white-space: nowrap; padding: 4px 8px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-active); opacity: 0;
  pointer-events: none; transition: opacity 0.15s;
}
.tooltip:hover::after { opacity: 1; }

::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-active); border-radius: 2px; }

input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; }
input[type=number] { -moz-appearance: textfield; }

@keyframes value-pop {
  0% { transform: scale(0.92); opacity: 0.5; }
  60% { transform: scale(1.04); }
  100% { transform: scale(1); opacity: 1; }
}
.result-value.animate { animation: value-pop 0.35s ease; }

@keyframes slide-up { from { opacity:0; transform:translateY(16px); } to { opacity:1; transform:none; } }

/* ═══════════════════════════════════════════════════════════
   TABLET  641px – 1024px
═══════════════════════════════════════════════════════════ */
@media (max-width: 1024px) and (min-width: 641px) {
  :root {
    --panel-width: 280px;
    --results-height: 80px;
  }

  /* Show panel toggle button in toolbar */
  #btn-panel-toggle { display: flex; }

  /* Panel collapses off-screen to the left when .collapsed */
  #input-panel {
    position: fixed;
    top: var(--toolbar-height);
    left: 0;
    bottom: var(--results-height);
    width: var(--panel-width);
    z-index: 150;
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
    transform: translateX(0);
  }

  #input-panel.collapsed {
    transform: translateX(calc(-1 * var(--panel-width)));
    box-shadow: none;
  }

  /* Viewer takes full width when panel is collapsed */
  #app {
    grid-template-columns: 0 1fr;
  }

  #viewer-container {
    grid-column: 1 / -1;
  }

  #results-panel {
    grid-column: 1 / -1;
  }

  /* Overlay dims viewer when panel is open on tablet */
  #drawer-overlay {
    display: block;
  }

  .result-card { padding: 0 16px; }
  .result-value { font-size: 22px; }
  .result-sub { display: none; }

  /* Compact quick-stats card on tablet */
  #results-panel > div:last-child { display: none; }
}

/* ═══════════════════════════════════════════════════════════
   MOBILE  ≤ 640px
═══════════════════════════════════════════════════════════ */
@media (max-width: 640px) {
  :root {
    --toolbar-height:  48px;
    --tab-bar-height:  calc(56px + env(safe-area-inset-bottom, 0px));
    --results-height:  0px;   /* results go into strip above tab bar */
    --panel-width:     100%;
  }

  html, body { overflow: hidden; }

  /* Flatten grid — viewer fills everything */
  #app {
    display: block;
    height: 100vh;
    position: relative;
  }

  /* Toolbar shrinks */
  #toolbar {
    position: fixed; top: 0; left: 0; right: 0;
    height: var(--toolbar-height); z-index: 301;
    padding: 0 12px;
  }
  .brand-tag { display: none; }
  .brand-name { font-size: 15px; }
  .toolbar-status span { display: none; } /* just show dot */

  /* Viewer full screen below toolbar, above tab bar + strip */
  #viewer-container {
    position: fixed;
    top: var(--toolbar-height);
    left: 0; right: 0;
    bottom: calc(var(--tab-bar-height) + 58px); /* 58px = results strip */
    z-index: 1;
  }

  /* Desktop results panel — hidden on mobile */
  #results-panel { display: none; }

  /* Mobile results strip — shown above tab bar */
  #mobile-results-strip {
    display: flex;
    left: 0; right: 0;
    bottom: var(--tab-bar-height);
    height: 58px;
    overflow: hidden;
  }

  #mobile-results-strip .result-card {
    flex: 1; display: flex; flex-direction: column;
    justify-content: center; align-items: flex-start;
    padding: 0 12px; border-right: 1px solid var(--border-subtle);
    min-width: 0;
  }
  #mobile-results-strip .result-card:last-child { border-right: none; }
  #mobile-results-strip .result-label { font-size: 7px; letter-spacing: 0.15em; margin-bottom: 2px; }
  #mobile-results-strip .result-value { font-size: 18px; }
  #mobile-results-strip .result-unit { font-size: 8px; margin-top: 1px; }
  #mobile-results-strip .result-sub,
  #mobile-results-strip .result-card::before { display: none; }

  /* Mobile area unit select — larger touch target */
  #mobile-results-strip .area-unit-select {
    font-size: 10px;
    padding: 4px 20px 4px 6px;
    min-width: 56px;
    min-height: 28px;
    background-position: right 6px center;
  }

  /* Tab bar shown */
  #mobile-tab-bar { display: block; }

  /* Drawer — full-width bottom sheet */
  #input-panel {
    position: fixed;
    left: 0; right: 0;
    bottom: var(--tab-bar-height);
    /* collapsed = only handle visible */
    height: calc(100vh - var(--toolbar-height) - var(--tab-bar-height));
    transform: translateY(calc(100% - var(--drawer-peek)));
    z-index: 290;
    border-right: none;
    border-top: 1px solid var(--border-accent);
    border-radius: 12px 12px 0 0;
    box-shadow: 0 -8px 40px rgba(0,0,0,0.6);
    overflow: hidden;
    display: flex; flex-direction: column;
    transition: transform 0.35s cubic-bezier(0.4,0,0.2,1);
    will-change: transform;
    /* Ensure drawer never goes above toolbar */
    top: var(--toolbar-height);
  }

  #input-panel.drawer-open {
    transform: translateY(0);
  }

  /* Add top padding to scroll area so content doesn't get clipped by border-radius */
  .panel-scroll-area {
    padding-top: 4px;
  }

  /* Show drawer handle on mobile */
  .drawer-handle { display: flex; }


  /* Drawer overlay */
  #drawer-overlay { display: block; z-index: 289; }

  /* Viewer toolbar — move up slightly, smaller on mobile */
  .viewer-toolbar { top: 8px; right: 8px; }
  .viewer-btn { padding: 6px 8px; font-size: 8px; min-height: 32px; }
  .viewer-btn span { display: none; } /* icon only on mobile */
  .viewer-btn .vb-icon { width: 14px; height: 14px; }

  /* Badge & corner info — compact */
  .viewer-type-badge { font-size: 10px; padding: 4px 8px; top: 8px; left: 8px; }
  .viewer-corner-info { display: none; } /* hidden on mobile — space too tight */

  /* Bigger touch inputs */
  .input-row input { padding: 11px 10px; font-size: 15px; }
  .duct-type-btn { padding: 12px 6px; }
  .unit-btn { padding: 9px 4px; }

  /* Quick stats hidden on mobile */
  #results-panel > div:last-child { display: none; }

  /* Sections get more breathing room */
  .panel-section { padding: 14px 16px; }
}

/* ═══════════════════════════════════════════════════════════
   VERY SMALL PHONES  ≤ 375px
═══════════════════════════════════════════════════════════ */
@media (max-width: 375px) {
  .brand-name { font-size: 13px; letter-spacing: 0.08em; }
  .result-value { font-size: 16px; }
  .duct-type-btn .btn-label { font-size: 8px; }
  .calc-btn { font-size: 12px; letter-spacing: 0.12em; }
  .viewer-btn { padding: 5px 7px; }
}

/* ═══════════════════════════════════════════════════════════
   LANDSCAPE MOBILE  (height < 500px)
═══════════════════════════════════════════════════════════ */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
  :root { --tab-bar-height: 44px; --toolbar-height: 40px; }

  #viewer-container {
    bottom: calc(44px + 48px); /* tab bar + results strip */
  }

  #mobile-results-strip { height: 48px; }
  #mobile-results-strip .result-value { font-size: 16px; }

  /* Drawer half-height in landscape */
  #input-panel {
    height: calc(100vh - var(--toolbar-height) - var(--tab-bar-height));
  }

  .viewer-corner-info { display: none; }
  .tab-btn span { display: none; }
  #mobile-tab-bar { height: 44px; }
  .tab-bar-inner { height: 44px; }
}