:root {
  --bg: #f1f5f9;
  --bg-elev: #ffffff;
  --bg-sunken: #e2e8f0;
  --ink: #1e293b;
  --ink-soft: #475569;
  --ink-faint: #64748b;
  --line: #cbd5e1;
  --line-soft: #e2e8f0;
  --brand: #475569;
  --brand-strong: #334155;
  --accent: #06b6d4;
  --accent-strong: #0891b2;
  --accent-soft: #cffafe;
  --good: #0d9488;
  --shadow: 0 1px 3px rgba(15, 23, 42, .12), 0 6px 18px rgba(15, 23, 42, .06);
  --radius: 14px;
  --radius-sm: 9px;
  --focus: 0 0 0 3px rgba(6, 182, 212, .45);
  --tap: 44px;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --bg-elev: #1e293b;
  --bg-sunken: #0b1220;
  --ink: #e2e8f0;
  --ink-soft: #cbd5e1;
  --ink-faint: #94a3b8;
  --line: #334155;
  --line-soft: #1e293b;
  --brand: #64748b;
  --brand-strong: #94a3b8;
  --accent: #22d3ee;
  --accent-strong: #06b6d4;
  --accent-soft: #164e63;
  --good: #2dd4bf;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--accent);
  color: #04212a;
  padding: 10px 16px;
  border-radius: 0 0 8px 0;
  z-index: 50;
  font-weight: 600;
}
.skip-link:focus { left: 0; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px clamp(12px, 4vw, 28px);
  background: linear-gradient(135deg, var(--brand-strong), var(--brand));
  color: #f8fafc;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo { border-radius: 9px; flex: none; }
.brand-text { min-width: 0; }
.brand-text h1 { font-size: 1.15rem; margin: 0; line-height: 1.1; letter-spacing: .2px; }
.tagline { margin: 0; font-size: .78rem; opacity: .82; }
.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  min-height: var(--tap);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform .12s ease, background .15s ease, box-shadow .15s ease, border-color .15s ease;
}
.btn:focus-visible { outline: none; box-shadow: var(--focus); }
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--accent);
  color: #04212a;
  border-color: var(--accent-strong);
}
.btn-primary:hover { background: var(--accent-strong); color: #f0fdff; }

.btn-ghost {
  background: rgba(148, 163, 184, .14);
  color: var(--ink);
  border-color: var(--line);
}
.btn-ghost:hover { background: rgba(148, 163, 184, .26); }

.app-header .btn-ghost {
  background: rgba(255, 255, 255, .15);
  color: #f8fafc;
  border-color: rgba(255, 255, 255, .28);
}
.app-header .btn-ghost:hover { background: rgba(255, 255, 255, .26); }

.btn-icon {
  background: rgba(255, 255, 255, .15);
  color: #f8fafc;
  border-color: rgba(255, 255, 255, .28);
  min-width: var(--tap);
  padding: 10px;
}
.btn-icon:hover { background: rgba(255, 255, 255, .28); }
.theme-icon { font-size: 1.25rem; line-height: 1; }

.btn-sm { min-height: 38px; padding: 7px 12px; font-size: .85rem; }

/* Main layout */
.app-main {
  max-width: 760px;
  margin: 0 auto;
  padding: clamp(14px, 4vw, 26px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.panel {
  background: var(--bg-elev);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: clamp(14px, 3.5vw, 22px);
  box-shadow: var(--shadow);
}
.panel-title {
  margin: 0 0 14px;
  font-size: 1rem;
  color: var(--ink-soft);
  letter-spacing: .3px;
  text-transform: uppercase;
  font-weight: 700;
}

/* Dropzone */
.dropzone {
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  background: var(--bg-sunken);
  padding: clamp(24px, 8vw, 48px) 16px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s ease, background .15s ease;
}
.dropzone:hover, .dropzone:focus-visible { border-color: var(--accent); background: var(--accent-soft); outline: none; }
.dropzone.is-drag { border-color: var(--accent-strong); background: var(--accent-soft); }
.dropzone-icon { font-size: 2.4rem; display: block; color: var(--accent-strong); line-height: 1; }
.dropzone-text { margin: 12px 0 4px; font-size: 1.02rem; font-weight: 600; }
.dropzone-hint { margin: 0; font-size: .82rem; color: var(--ink-faint); }
.link-look { color: var(--accent-strong); text-decoration: underline; }

/* Queue bar */
.queue-bar {
  margin-top: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.queue-info { font-size: .9rem; color: var(--ink-soft); font-weight: 600; }
.queue-nav { display: flex; gap: 8px; flex-wrap: wrap; }

/* Fields */
.field-group { margin-bottom: 18px; }
.field { margin-bottom: 18px; }
.field:last-child { margin-bottom: 0; }
.field > label, .field-legend {
  display: block;
  font-size: .9rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--ink-soft);
}
.field-hint { margin: 6px 0 0; font-size: .8rem; color: var(--ink-faint); }

.segmented {
  display: flex;
  gap: 6px;
  background: var(--bg-sunken);
  padding: 5px;
  border-radius: var(--radius-sm);
  flex-wrap: wrap;
}
.seg-btn {
  flex: 1 1 auto;
  min-width: 92px;
  font: inherit;
  font-weight: 600;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  padding: 9px 10px;
  min-height: 40px;
  border-radius: 7px;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, box-shadow .15s ease;
}
.seg-btn:focus-visible { outline: none; box-shadow: var(--focus); }
.seg-btn.is-active { background: var(--accent); color: #04212a; box-shadow: var(--shadow); }

.input-row { display: flex; align-items: stretch; gap: 8px; }
input[type="number"], select {
  font: inherit;
  width: 100%;
  padding: 10px 12px;
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--ink);
}
input[type="number"]:focus-visible, select:focus-visible {
  outline: none; box-shadow: var(--focus); border-color: var(--accent);
}
.unit {
  display: inline-flex;
  align-items: center;
  padding: 0 14px;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-weight: 600;
  color: var(--ink-faint);
}

.presets { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.chip {
  font: inherit;
  font-weight: 600;
  font-size: .85rem;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink-soft);
  padding: 7px 14px;
  min-height: 38px;
  border-radius: 999px;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, color .15s ease;
}
.chip:hover { border-color: var(--accent); color: var(--accent-strong); }
.chip:focus-visible { outline: none; box-shadow: var(--focus); }
.chip.is-active { background: var(--accent); color: #04212a; border-color: var(--accent-strong); }

/* Range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 8px;
  border-radius: 999px;
  background: var(--bg-sunken);
  outline: none;
  margin: 8px 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--bg-elev);
  box-shadow: var(--shadow);
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--bg-elev);
  box-shadow: var(--shadow);
  cursor: pointer;
}
input[type="range"]:focus-visible { box-shadow: var(--focus); }

/* Preview */
.preview-wrap { margin-bottom: 16px; }
.preview-img-box {
  background:
    repeating-conic-gradient(var(--bg-sunken) 0 25%, transparent 0 50%) 0 0 / 22px 22px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 120px;
}
#previewImg {
  max-width: 100%;
  max-height: 46vh;
  height: auto;
  border-radius: 6px;
  display: block;
}

/* Stats */
.stats {
  display: flex;
  align-items: stretch;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.stat-card {
  flex: 1 1 130px;
  min-width: 0;
  background: var(--bg-sunken);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat-card--accent { border-color: var(--accent); background: var(--accent-soft); }
.stat-card--save { border-color: var(--good); }
.stat-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .5px; color: var(--ink-faint); font-weight: 700; }
.stat-value { font-size: 1.2rem; font-weight: 700; color: var(--ink); word-break: break-word; }
.stat-card--save .stat-value { color: var(--good); }
.stat-sub { font-size: .82rem; color: var(--ink-soft); word-break: break-word; }
.stat-arrow { display: flex; align-items: center; color: var(--ink-faint); font-size: 1.3rem; font-weight: 700; }

.action-row { display: flex; gap: 10px; flex-wrap: wrap; }
.action-row .btn { flex: 1 1 auto; }

/* Info panel */
.panel--info { box-shadow: none; }
.panel--info summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--ink-soft);
}
.panel--info summary:focus-visible { outline: none; box-shadow: var(--focus); border-radius: 6px; }
.panel--info p { font-size: .88rem; color: var(--ink-soft); }

/* Footer */
.app-footer {
  text-align: center;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  font-size: .8rem;
  color: var(--ink-faint);
}

/* Toasts */
.toast-host {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(92vw, 420px);
  pointer-events: none;
}
.toast {
  background: var(--brand-strong);
  color: #f8fafc;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-size: .9rem;
  font-weight: 500;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity .25s ease, transform .25s ease;
  border-left: 4px solid var(--accent);
}
.toast.is-show { opacity: 1; transform: translateY(0); }
.toast--error { border-left-color: #f87171; }
.toast--ok { border-left-color: var(--good); }

/* Busy overlay */
.busy-overlay {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(15, 23, 42, .55);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: #f8fafc;
  font-weight: 600;
}
.spinner {
  width: 46px; height: 46px;
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, .25);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}

@media (max-width: 430px) {
  .stat-arrow { display: none; }
  .stat-card { flex-basis: 100%; }
  .action-row .btn { flex-basis: 100%; }
}
