:root {
  --green-900: #064e3b;
  --green-800: #065f46;
  --green-700: #047857;
  --green-600: #059669;
  --green-500: #10b981;
  --green-100: #d1fae5;
  --green-50: #ecfdf5;

  --bg: #f4f8f6;
  --surface: #ffffff;
  --surface-2: #f0f6f3;
  --border: #d4e4dc;
  --text: #0f2a22;
  --text-soft: #4a6359;
  --shadow: 0 6px 22px rgba(6, 95, 70, 0.10);
  --shadow-sm: 0 2px 8px rgba(6, 95, 70, 0.08);
  --accent: var(--green-800);
  --accent-strong: var(--green-700);
  --on-accent: #ffffff;
  --danger: #b91c1c;
  --radius: 16px;
  --radius-sm: 10px;
  --focus: #0ea371;
}

[data-theme="dark"] {
  --bg: #07140f;
  --surface: #0e2019;
  --surface-2: #122a21;
  --border: #1d4034;
  --text: #e6f4ee;
  --text-soft: #9bc4b3;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.4);
  --accent: var(--green-600);
  --accent-strong: var(--green-500);
  --on-accent: #04130d;
  --danger: #f87171;
  --focus: #34d399;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
  overflow-x: hidden;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1, h2, h3 { line-height: 1.25; margin: 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;
}

.hidden { display: none !important; }

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--accent);
  color: var(--on-accent);
  padding: 10px 14px;
  border-radius: 0 0 var(--radius-sm) 0;
  z-index: 50;
}
.skip-link:focus { left: 0; }

a { color: var(--accent-strong); }

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px clamp(14px, 4vw, 28px);
  background: linear-gradient(135deg, var(--green-900), var(--green-700));
  color: #fff;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-shield { display: inline-flex; color: var(--green-100); flex: none; }
.brand-text { min-width: 0; }
.brand-text h1 { font-size: clamp(1.05rem, 4vw, 1.4rem); }
.brand-sub { margin: 2px 0 0; font-size: 0.78rem; opacity: 0.9; overflow-wrap: anywhere; }

.theme-toggle {
  flex: none;
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.3);
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s ease, transform 0.15s ease;
}
.theme-toggle:hover { background: rgba(255,255,255,0.22); }
.theme-toggle:active { transform: scale(0.94); }

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

.privacy-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--green-50);
  color: var(--green-900);
  border: 1px solid var(--green-100);
  border-left: 5px solid var(--green-700);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
}
[data-theme="dark"] .privacy-banner {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
  border-left-color: var(--green-500);
}
.privacy-banner p { margin: 0; font-size: 0.92rem; overflow-wrap: anywhere; }
.lock-badge { font-size: 1.4rem; flex: none; }

/* Cards */
.upload-card, .results, .info-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: clamp(16px, 4vw, 22px);
  box-shadow: var(--shadow-sm);
}
.upload-card h2, .results h2, .info-card h2 {
  font-size: 1.1rem;
  margin-bottom: 14px;
  color: var(--accent-strong);
}

/* Drop zone */
.drop-zone {
  border: 2px dashed var(--green-600);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  padding: clamp(22px, 6vw, 38px) 16px;
  text-align: center;
  cursor: pointer;
  color: var(--accent-strong);
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.drop-zone:hover { background: var(--green-50); }
[data-theme="dark"] .drop-zone:hover { background: var(--surface); }
.drop-zone.dragover {
  border-color: var(--green-500);
  background: var(--green-100);
  transform: scale(1.005);
}
[data-theme="dark"] .drop-zone.dragover { background: var(--surface); }
.drop-zone:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}
.drop-illu { color: var(--green-600); margin-bottom: 6px; }
.drop-title { margin: 6px 0 2px; font-weight: 600; color: var(--text); }
.drop-hint { margin: 0; font-size: 0.82rem; color: var(--text-soft); }

/* Options */
.options {
  margin-top: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.option-row {
  flex: 1 1 200px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.option-row label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-soft);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.option-row label output { color: var(--accent-strong); font-variant-numeric: tabular-nums; }
select, input[type="range"] {
  width: 100%;
  font: inherit;
}
select {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
input[type="range"] { accent-color: var(--green-600); height: 28px; }
select:focus-visible, input[type="range"]:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* Results */
.results-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.results-bar h2 { margin-bottom: 0; }
.results-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.image-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}
@media (min-width: 560px) {
  .image-list { grid-template-columns: 1fr 1fr; }
}

.image-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
  display: flex;
  flex-direction: column;
}
.image-thumb {
  width: 100%;
  aspect-ratio: 16 / 10;
  object-fit: cover;
  background: #0001;
  display: block;
}
.image-body { padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.image-name {
  font-weight: 600;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.size-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
  font-size: 0.78rem;
}
.size-cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  text-align: center;
}
.size-cell .label { display: block; color: var(--text-soft); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.03em; }
.size-cell .value { font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.size-cell.delta .value { color: var(--green-600); }
.size-cell.delta.up .value { color: var(--danger); }

.image-meta { font-size: 0.78rem; color: var(--text-soft); }
.image-meta .clean-flag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--green-700);
  font-weight: 600;
}
[data-theme="dark"] .image-meta .clean-flag { color: var(--green-500); }
.image-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.image-actions .btn { flex: 1 1 auto; }

.card-status {
  font-size: 0.8rem;
  color: var(--text-soft);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--green-100);
  border-top-color: var(--green-600);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Info */
.info-list { margin: 0; padding-left: 1.1em; display: flex; flex-direction: column; gap: 10px; }
.info-list li { font-size: 0.9rem; overflow-wrap: anywhere; }
.info-list strong { color: var(--accent-strong); }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  font-size: 0.88rem;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease, border-color 0.15s ease;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-strong); }
.btn-ghost {
  background: transparent;
  color: var(--accent-strong);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--border); }
.btn-danger:hover { background: var(--surface-2); }

/* Footer */
.app-footer {
  text-align: center;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.footer-note { margin: 0; font-size: 0.78rem; color: var(--text-soft); }

/* Toasts */
.toast-wrap {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 60;
  width: min(92vw, 420px);
  pointer-events: none;
}
.toast {
  background: var(--green-900);
  color: #fff;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  overflow-wrap: anywhere;
  animation: toastIn 0.2s ease;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--green-700); }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* Modal */
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: 18px;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 30, 22, 0.55);
  backdrop-filter: blur(2px);
}
.modal-box {
  position: relative;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  width: min(92vw, 420px);
  box-shadow: var(--shadow);
  animation: toastIn 0.2s ease;
}
.modal-title { font-size: 1.1rem; color: var(--accent-strong); margin-bottom: 10px; }
.modal-body { margin: 0 0 18px; font-size: 0.92rem; overflow-wrap: anywhere; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
