/* ============================================
   Battery Lab — design tokens & base
   ============================================ */

:root {
  --font: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, "Roboto", sans-serif;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow-soft: 0 8px 32px rgba(15, 23, 42, 0.08);
  --shadow-glow: 0 0 0 1px rgba(255, 255, 255, 0.12) inset;
  --transition: 0.22s cubic-bezier(0.4, 0, 0.2, 1);
  --focus-ring: 0 0 0 3px rgba(59, 130, 246, 0.45);

  --bg0: #f1f5f9;
  --bg1: #e2e8f0;
  --surface: rgba(255, 255, 255, 0.72);
  --surface-solid: #ffffff;
  --text: #0f172a;
  --text-muted: #475569;
  --border: rgba(148, 163, 184, 0.35);
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-bg: rgba(254, 226, 226, 0.85);
  --warn: #d97706;
  --warn-bg: rgba(254, 243, 199, 0.85);
  --ok: #059669;
  --ok-bg: rgba(209, 250, 229, 0.85);
  --accent: #7c3aed;
}

html[data-theme="dark"] {
  --bg0: #020617;
  --bg1: #0f172a;
  --surface: rgba(15, 23, 42, 0.75);
  --surface-solid: #0f172a;
  --text: #f8fafc;
  --text-muted: #94a3b8;
  --border: rgba(51, 65, 85, 0.7);
  --shadow-soft: 0 12px 40px rgba(0, 0, 0, 0.45);
  --danger-bg: rgba(127, 29, 29, 0.35);
  --warn-bg: rgba(120, 53, 15, 0.35);
  --ok-bg: rgba(6, 78, 59, 0.35);
}

html.high-contrast {
  --border: rgba(0, 0, 0, 0.55);
  --text: #000000;
  --text-muted: #1a1a1a;
}

html[data-theme="dark"].high-contrast {
  --border: rgba(255, 255, 255, 0.65);
  --text: #ffffff;
  --text-muted: #e2e8f0;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body.app-body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg0);
  line-height: 1.5;
  overflow-x: hidden;
  padding-bottom: env(safe-area-inset-bottom);
}

.app-bg {
  position: fixed;
  inset: 0;
  z-index: -2;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(37, 99, 235, 0.18), transparent 55%),
    radial-gradient(900px 500px at 100% 20%, rgba(124, 58, 237, 0.14), transparent 50%),
    linear-gradient(165deg, var(--bg0), var(--bg1));
  pointer-events: none;
}

html[data-theme="dark"] .app-bg {
  background:
    radial-gradient(1000px 500px at 15% 0%, rgba(59, 130, 246, 0.2), transparent 55%),
    radial-gradient(800px 400px at 90% 10%, rgba(124, 58, 237, 0.15), transparent 45%),
    linear-gradient(165deg, var(--bg0), var(--bg1));
}

.wrapper {
  max-width: 1440px;
  margin: 0 auto;
  padding: 16px 20px 100px;
}

@media (min-width: 900px) {
  .wrapper {
    padding-bottom: 32px;
  }
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 8px;
  background: var(--primary);
  color: #fff;
  padding: 8px 14px;
  border-radius: 8px;
  z-index: 100000;
}

.skip-link:focus {
  left: 8px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Glass */
.glass {
  background: var(--surface);
  backdrop-filter: blur(18px) saturate(140%);
  -webkit-backdrop-filter: blur(18px) saturate(140%);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft), var(--shadow-glow);
  border-radius: var(--radius);
}

.glass-inner {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
}

html[data-theme="dark"] .glass-inner {
  background: rgba(255, 255, 255, 0.03);
}

/* Header */
.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 20px;
  margin-bottom: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  font-size: 1.75rem;
  line-height: 1;
}

.brand-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 1rem;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
}

html[data-theme="dark"] .icon-btn {
  background: rgba(30, 41, 59, 0.9);
}

.icon-btn:hover {
  transform: translateY(-1px);
}

.icon-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

html[data-theme="dark"] #themeToggle .theme-icon-light {
  display: none;
}

html[data-theme="light"] #themeToggle .theme-icon-dark {
  display: none;
}

#soundToggle[aria-pressed="false"] .sound-on {
  display: none;
}

#soundToggle[aria-pressed="true"] .sound-off {
  display: none;
}

#soundToggle[aria-pressed="false"] .sound-off,
#soundToggle[aria-pressed="true"] .sound-on {
  display: inline;
}

.chip-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  transition: var(--transition);
}

.secondary-btn:hover {
  border-color: var(--primary);
}

.install-btn.hidden {
  display: none;
}

/* Tips bar */
.tips-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  margin-bottom: 16px;
}

.tips-text {
  flex: 1;
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.tips-nav {
  border: none;
  background: transparent;
  font-size: 1.25rem;
  cursor: pointer;
  color: var(--text);
  padding: 4px 8px;
  border-radius: 8px;
}

.tips-nav:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

/* Main grid */
.main-grid {
  display: grid;
  gap: 16px;
}

@media (min-width: 1100px) {
  .main-grid {
    grid-template-columns: 320px 1fr 340px;
    align-items: start;
  }
}

.panel {
  padding: 18px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.preset-row {
  margin-bottom: 14px;
}

.field-hint {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.chip {
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

html[data-theme="dark"] .chip,
html[data-theme="dark"] .chip-btn {
  background: rgba(30, 41, 59, 0.85);
}

.chip:hover,
.chip:focus-visible {
  border-color: var(--primary);
  transform: translateY(-1px);
}

.chip:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.form-grid {
  display: grid;
  gap: 12px;
}

@media (min-width: 500px) {
  .form-grid {
    grid-template-columns: 1fr 1fr;
  }
}

.form-grid.compact {
  grid-template-columns: 1fr;
}

.field label {
  display: block;
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 4px;
}

.field input,
.field select,
.ai-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-solid);
  color: var(--text);
  font-size: 0.95rem;
  min-height: 44px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

html[data-theme="dark"] .field input,
html[data-theme="dark"] .field select,
html[data-theme="dark"] .ai-input {
  background: rgba(15, 23, 42, 0.9);
}

.field input:focus-visible,
.field select:focus-visible,
.ai-input:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: var(--focus-ring);
}

.switch-label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  cursor: pointer;
}

.compare-grid {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px dashed var(--border);
}

.compare-heading {
  margin: 0 0 10px;
  font-size: 0.95rem;
}

.hidden {
  display: none !important;
}

[hidden] {
  display: none !important;
}

.actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}

.desktop-actions {
  display: none;
}

@media (min-width: 900px) {
  .desktop-actions {
    display: flex;
  }
}

/* Buttons */
.btn {
  border: none;
  border-radius: 12px;
  padding: 10px 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
  font-size: 0.9rem;
}

.btn:active {
  transform: scale(0.98);
}

.btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.btn.primary {
  background: linear-gradient(135deg, var(--primary), #4f46e5);
  color: #fff;
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.35);
}

.btn.primary:hover {
  background: linear-gradient(135deg, var(--primary-hover), #4338ca);
}

.btn.secondary {
  background: rgba(37, 99, 235, 0.12);
  color: var(--primary);
  border: 1px solid rgba(37, 99, 235, 0.25);
}

.btn.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn.tiny {
  padding: 6px 12px;
  font-size: 1rem;
  line-height: 1;
}

.btn.block {
  width: 100%;
}

/* Visualization */
.viz-head {
  align-items: flex-start;
}

.viz-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.viz-hint {
  font-size: 0.72rem;
  color: var(--text-muted);
  max-width: 220px;
  text-align: right;
}

.canvas-wrap {
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(248, 250, 252, 0.9), rgba(241, 245, 249, 0.95));
  min-height: 280px;
}

html[data-theme="dark"] .canvas-wrap {
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.95), rgba(2, 6, 23, 0.98));
}

.battery-canvas {
  display: block;
  width: 100%;
  height: auto;
  min-height: 320px;
  touch-action: none;
  cursor: grab;
}

.battery-canvas:active {
  cursor: grabbing;
}

.battery-canvas:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.canvas-skeleton {
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, rgba(148, 163, 184, 0.12), rgba(148, 163, 184, 0.22), rgba(148, 163, 184, 0.12));
  background-size: 200% 100%;
  animation: shimmer 1.2s ease-in-out infinite;
  z-index: 1;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.4s ease;
}

.canvas-skeleton.done {
  opacity: 0;
  pointer-events: none;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.stats-canvas {
  width: 100%;
  height: 180px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-top: 12px;
  background: var(--surface-solid);
  display: block;
}

html[data-theme="dark"] .stats-canvas {
  background: rgba(15, 23, 42, 0.6);
}

.viz-footnote {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 8px 0 0;
}

.tooltip {
  position: fixed;
  z-index: 5000;
  max-width: min(280px, 90vw);
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.92);
  color: #f8fafc;
  font-size: 0.8rem;
  line-height: 1.4;
  pointer-events: none;
  box-shadow: var(--shadow-soft);
  border: 1px solid rgba(255, 255, 255, 0.08);
  white-space: pre-line;
}

html[data-theme="light"] .tooltip {
  background: rgba(15, 23, 42, 0.92);
  color: #f8fafc;
}

/* Metrics */
.results-grid {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(2, 1fr);
}

.metric-card {
  padding: 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-solid);
  transition: transform var(--transition), box-shadow var(--transition);
}

html[data-theme="dark"] .metric-card {
  background: rgba(30, 41, 59, 0.55);
}

.metric-card[data-animate].pulse {
  animation: metricPulse 0.55s ease;
}

@keyframes metricPulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.02); box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.2); }
  100% { transform: scale(1); }
}

.metric-card.wide {
  grid-column: 1 / -1;
}

.metric-label {
  display: block;
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.metric-value {
  font-size: 1.35rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.metric-unit {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-left: 4px;
}

.warning {
  margin-top: 14px;
  padding: 14px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  line-height: 1.55;
  border: 1px solid var(--border);
}

.warning.safe {
  background: var(--ok-bg);
  color: var(--text);
  border-color: rgba(5, 150, 105, 0.35);
}

.warning.warn {
  background: var(--warn-bg);
  border-color: rgba(217, 119, 6, 0.4);
}

.warning.danger {
  background: var(--danger-bg);
  border: 2px solid var(--danger);
  color: var(--text);
}

.warning ul {
  margin: 8px 0 0 1.1rem;
  padding: 0;
}

.compare-summary {
  margin-top: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 0.88rem;
  background: var(--surface-solid);
}

html[data-theme="dark"] .compare-summary {
  background: rgba(30, 41, 59, 0.5);
}

.edu-cards {
  display: grid;
  gap: 10px;
  margin-top: 16px;
}

.edu-card {
  padding: 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.35);
}

html[data-theme="dark"] .edu-card {
  background: rgba(255, 255, 255, 0.03);
}

.edu-card h3 {
  margin: 0 0 6px;
  font-size: 0.9rem;
}

.edu-card p {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.ai-panel {
  margin-top: 16px;
}

.ai-panel h3 {
  margin: 0 0 8px;
  font-size: 0.95rem;
}

.ai-output {
  margin-top: 10px;
  font-size: 0.88rem;
  color: var(--text-muted);
  min-height: 1.2em;
}

.tool-extra-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 18px;
}

.tool-toggle-btn {
  width: 100%;
  text-align: left;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: linear-gradient(135deg, var(--surface-solid), rgba(248, 250, 252, 0.8));
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  box-shadow: 0 6px 18px rgba(15, 23, 42, 0.06);
  transition: var(--transition);
}

html[data-theme="dark"] .tool-toggle-btn {
  background: linear-gradient(135deg, rgba(30, 41, 59, 0.95), rgba(15, 23, 42, 0.9));
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.35);
}

.tool-toggle-btn:hover {
  transform: translateY(-2px);
  border-color: rgba(37, 99, 235, 0.35);
}

.tool-toggle-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

/* Mobile sticky */
.mobile-sticky {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  z-index: 9000;
  border-radius: 16px 16px 0 0;
}

@media (min-width: 900px) {
  .mobile-sticky {
    display: none;
  }
}

/* Onboarding */
.onboarding-overlay {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.55);
  backdrop-filter: blur(6px);
  z-index: 100001;
  display: grid;
  place-items: center;
  padding: 20px;
}

.onboarding-card {
  max-width: 440px;
  padding: 22px;
}

.onboarding-list {
  margin: 0 0 14px;
  padding-left: 1.2rem;
  color: var(--text-muted);
  font-size: 0.92rem;
}

/* Legal & popups */
.legal-popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99998;
  padding: 20px;
  animation: fadeIn 0.3s ease;
}

.legal-popup-box {
  width: 100%;
  max-width: 520px;
  padding: 26px;
  text-align: center;
}

.legal-popup-icon {
  font-size: 2.5rem;
  margin-bottom: 8px;
}

.legal-popup-box h3 {
  margin: 0 0 12px;
}

.legal-popup-box p {
  color: var(--text-muted);
  font-size: 0.92rem;
  margin: 0 0 12px;
}

html[data-theme="dark"] .legal-popup-box {
  background: rgba(15, 23, 42, 0.96);
  color: var(--text);
}

html[data-theme="dark"] .legal-popup-box h3 {
  color: var(--text);
}

.popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 99999;
  padding: 20px;
  min-height: 100vh;
}

.popup-overlay.active {
  display: flex;
  animation: fadeIn 0.25s ease;
}

.popup-box {
  width: 100%;
  max-width: 720px;
  padding: 26px;
  position: relative;
  max-height: calc(100vh - 40px);
  overflow: auto;
}

.popup-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 1rem;
}

.popup-close:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

textarea#embedCode {
  width: 100%;
  min-height: 140px;
  margin-top: 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 12px;
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  background: var(--surface-solid);
  color: var(--text);
}

.copy-btn {
  margin-top: 12px;
  padding: 12px 18px;
  border-radius: 12px;
  border: none;
  background: #10b981;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.similar-tools h3 {
  margin-top: 0;
}

#similarToolsList {
  list-style: none;
  padding: 0;
  margin: 0;
}

#similarToolsList li {
  margin-bottom: 8px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
}

#similarToolsList a {
  display: block;
  padding: 12px 14px;
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
}

#similarToolsList a:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: 12px;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Skeleton */
.skeleton-line {
  display: inline-block;
  height: 10px;
  border-radius: 6px;
  background: linear-gradient(90deg, rgba(148, 163, 184, 0.2), rgba(148, 163, 184, 0.35), rgba(148, 163, 184, 0.2));
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

.skeleton-sm {
  width: 80px;
}

#configSkeleton {
  opacity: 0.6;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
