:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --bg-app: #f8fafc;
  --bg-surface: #ffffff;
  --bg-surface-subtle: #f1f5f9;
  --border-color: #cbd5e1;
  --text-main: #0f172a;
  --text-muted: #475569;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --primary-text: #ffffff;
  --primary-focus-ring: rgba(29, 78, 216, 0.25);
  --success: #15803d;
  --success-bg: #dcfce7;
  --warning: #a16207;
  --warning-bg: #fef9c3;
  --danger: #b91c1c;
  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 6px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -2px rgba(0,0,0,0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -4px rgba(0,0,0,0.04);
}

[data-theme="dark"] {
  --bg-app: #090d16;
  --bg-surface: #111827;
  --bg-surface-subtle: #1f2937;
  --border-color: #374151;
  --text-main: #f9fafb;
  --text-muted: #9ca3af;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-text: #ffffff;
  --primary-focus-ring: rgba(59, 130, 246, 0.3);
  --success: #22c55e;
  --success-bg: rgba(22, 163, 74, 0.2);
  --warning: #eab308;
  --warning-bg: rgba(202, 138, 4, 0.2);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-app);
  color: var(--text-main);
  line-height: 1.5;
  transition: background-color 0.2s ease, color 0.2s ease;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 2rem;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
}

.brand-container {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-logo {
  width: 42px;
  height: 42px;
  background: linear-gradient(135deg, #1d4ed8, #0284c7);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
}

.brand-logo svg {
  width: 24px;
  height: 24px;
}

.brand-title {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-subtitle {
  font-size: 0.825rem;
  color: var(--text-muted);
}

/* Theme Switcher */
.theme-switch-group {
  display: flex;
  background-color: var(--bg-surface-subtle);
  padding: 3px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}

.theme-btn {
  background: transparent;
  border: none;
  font-size: 0.775rem;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.2s;
}

.theme-btn.active {
  background-color: var(--bg-surface);
  color: var(--text-main);
  box-shadow: var(--shadow-sm);
}

/* App Layout */
.app-layout {
  display: grid;
  grid-template-columns: 1fr 440px;
  gap: 2rem;
  padding: 2rem;
  max-width: 1380px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
}

/* Cards & Panel Styling */
.card {
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: var(--shadow-sm);
}

.card-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.75rem;
}

/* URL Input Card & Analyze Button High Contrast */
.input-wrapper {
  display: flex;
  gap: 0.75rem;
}

.input-wrapper input {
  flex: 1;
  padding: 0.85rem 1.15rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  background: var(--bg-surface-subtle);
  color: var(--text-main);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrapper input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-focus-ring);
  background: var(--bg-surface);
}

.primary-btn {
  background-color: var(--primary);
  color: var(--primary-text);
  border: 1px solid transparent;
  padding: 0.85rem 1.6rem;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  border-radius: var(--radius-md);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

.primary-btn:hover {
  background-color: var(--primary-hover);
  box-shadow: var(--shadow-md);
  color: #ffffff;
}

.primary-btn:active {
  transform: translateY(1px);
}

/* Detection Card */
.detection-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.badge-confidence {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 9999px;
  background-color: var(--bg-surface-subtle);
  color: var(--text-muted);
  border: 1px solid var(--border-color);
}

.badge-confidence.active {
  background-color: var(--success-bg);
  color: var(--success);
  border-color: transparent;
}

.detection-platform {
  font-size: 0.825rem;
  font-weight: 600;
  color: var(--text-muted);
}

.detection-body {
  display: flex;
  gap: 1rem;
  align-items: center;
  margin-bottom: 1rem;
}

.detected-avatar {
  width: 54px;
  height: 54px;
  border-radius: var(--radius-md);
  background: var(--bg-surface-subtle);
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-weight: 700;
  color: var(--primary);
  flex-shrink: 0;
}

.detected-avatar img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.detected-info h3 {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 0.15rem;
}

.detected-info p {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.detected-url-crumb {
  font-size: 0.75rem;
  color: var(--primary);
  font-family: monospace;
}

.smart-explanation {
  background: var(--bg-surface-subtle);
  border-left: 3px solid var(--primary);
  padding: 0.85rem 1rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 0.85rem;
  color: var(--text-main);
  margin-bottom: 1.25rem;
}

.detection-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.action-btn-highlight, .action-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0.65rem 1.15rem;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn-highlight {
  background-color: var(--primary);
  color: var(--primary-text);
  border: 1px solid transparent;
}

.action-btn-highlight:hover {
  background-color: var(--primary-hover);
}

.action-btn {
  background-color: var(--bg-surface-subtle);
  border: 1px solid var(--border-color);
  color: var(--text-main);
}

.action-btn:hover {
  background-color: var(--border-color);
}

/* Presets Grid */
.presets-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 0.75rem;
}

.preset-chip {
  padding: 0.75rem 0.5rem;
  text-align: center;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-surface-subtle);
  cursor: pointer;
  transition: all 0.15s ease-in-out;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.preset-chip:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
}

.preset-chip.active {
  border-color: var(--primary);
  background: var(--primary-focus-ring);
  color: var(--primary);
  font-weight: 700;
}

.preset-badge-icon {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-block;
}

.preset-chip span {
  font-size: 0.8rem;
}

/* Custom Controls Body */
.collapsible-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.toggle-indicator {
  font-size: 1rem;
  transition: transform 0.2s;
}

.custom-controls-body {
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.15rem;
}

.control-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.control-group label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

.control-group select,
.control-group input[type="text"],
.control-group input[type="file"] {
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: var(--bg-surface-subtle);
  color: var(--text-main);
  font-size: 0.875rem;
  outline: none;
}

.control-group select:focus,
.control-group input[type="text"]:focus {
  border-color: var(--primary);
}

.color-picker-wrapper {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.color-picker-wrapper input[type="color"] {
  border: none;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: none;
}

.color-picker-wrapper input[type="text"] {
  flex: 1;
  text-transform: uppercase;
  font-family: monospace;
}

.radio-pill-group {
  display: flex;
  gap: 0.5rem;
  background: var(--bg-surface-subtle);
  padding: 4px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}

.radio-pill-group label {
  flex: 1;
  font-size: 0.775rem;
  text-align: center;
  padding: 6px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 600;
}

.radio-pill-group input[type="radio"] {
  display: none;
}

.radio-pill-group label:has(input:checked) {
  background: var(--bg-surface);
  color: var(--text-main);
  box-shadow: var(--shadow-sm);
}

.checkbox-group {
  justify-content: center;
}

/* QR Text Feature Section */
.qr-text-box {
  background: var(--bg-surface-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.qr-text-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.qr-text-input {
  flex: 1;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.apply-text-btn {
  background-color: var(--primary);
  color: var(--primary-text);
  border: 1px solid transparent;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 0.62rem 1.15rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.apply-text-btn:hover {
  background-color: var(--primary-hover);
}

.clear-btn {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.55rem 0.85rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
}

.clear-btn:hover {
  color: var(--danger);
  border-color: var(--danger);
}

/* Preview Panel */
.sticky-preview {
  position: sticky;
  top: 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.preview-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.25rem;
}

.preview-heading {
  font-weight: 700;
  font-size: 1rem;
}

.scan-status-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--bg-surface-subtle);
  color: var(--text-muted);
  border: 1px solid var(--border-color);
}

.status-indicator-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.qr-render-stage {
  width: 340px;
  height: 340px;
  background: #ffffff;
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border-color);
  padding: 15px;
  position: relative;
  overflow: hidden;
}

#qrCanvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
}

/* Telemetry stats */
.telemetry-bar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  margin-top: 1.25rem;
  padding: 0.85rem;
  background: var(--bg-surface-subtle);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
}

.telemetry-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.telemetry-label {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.telemetry-val {
  font-size: 0.85rem;
  font-weight: 700;
}

.repair-alert {
  margin-top: 0.85rem;
  width: 100%;
  padding: 0.75rem;
  border-radius: var(--radius-sm);
  background: var(--warning-bg);
  color: var(--warning);
  font-size: 0.775rem;
  font-weight: 600;
  text-align: center;
}

.export-actions {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 1.25rem;
}

.download-btn {
  width: 100%;
  padding: 0.85rem;
  border-radius: var(--radius-md);
  font-size: 0.925rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  transition: all 0.2s;
}

.primary-download {
  background: var(--primary);
  color: var(--primary-text);
}

.primary-download:hover {
  background: var(--primary-hover);
}

.secondary-download {
  background: var(--bg-surface-subtle);
  border: 1px solid var(--border-color);
  color: var(--text-main);
}

.secondary-download:hover {
  background: var(--border-color);
}

.hidden-field {
  display: none !important;
}

/* Footer */
.app-footer {
  text-align: center;
  padding: 1.5rem;
  font-size: 0.825rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border-color);
  background: var(--bg-surface);
}

/* Responsive */
@media (max-width: 1024px) {
  .app-layout {
    grid-template-columns: 1fr;
  }
  .sticky-preview {
    position: static;
  }
}

@media (max-width: 640px) {
  .app-header {
    padding: 1rem;
  }
  .app-layout {
    padding: 1rem;
    gap: 1rem;
  }
  .control-row {
    grid-template-columns: 1fr;
  }
  .qr-render-stage {
    width: 280px;
    height: 280px;
  }
}
