:root {
  --bg: #EDF0F3;
  --surface: #FFFFFF;
  --ink: #16202E;
  --muted: #5B6675;
  --line: #D6DCE3;
  --accent: #0E7C86;
  --accent-ink: #FFFFFF;
  --danger: #B42318;
  --check-a: #E4E8EC;
  --check-b: #F7F9FA;
  --radius-lg: 14px;
  --radius-sm: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161A1F;
    --surface: #1F252C;
    --ink: #E8EDF2;
    --muted: #9AA6B2;
    --line: #2E3740;
    --accent: #45B5BE;
    --accent-ink: #0B1F21;
    --danger: #FF8A80;
    --check-a: #2A3139;
    --check-b: #353D46;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  max-width: 1080px;
  margin: 0 auto;
  padding: 16px 20px;
}
.brand { font-weight: 700; font-size: 17px; letter-spacing: -0.01em; }
.who { display: flex; align-items: center; gap: 14px; color: var(--muted); font-size: 15px; }

main { max-width: 1080px; margin: 0 auto; padding: 0 20px 64px; }

h1 { font-size: 28px; line-height: 1.2; letter-spacing: -0.02em; margin: 24px 0 20px; }
.section-title { font-size: 18px; margin: 40px 0 12px; }

label { display: block; font-weight: 600; font-size: 14px; margin: 16px 0 6px; }

input {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

.code-input { font-size: 28px; letter-spacing: 0.4em; font-variant-numeric: tabular-nums; }

.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius-sm);
  padding: 11px 18px;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.big { font-size: 18px; padding: 16px 28px; border-radius: 12px; }
.btn.small { padding: 7px 12px; font-size: 14px; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.done { border-color: var(--accent); color: var(--accent); }

.link {
  font: inherit;
  background: none;
  border: 0;
  padding: 4px 0;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

button:focus-visible, .drop:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.row { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 16px; margin-top: 16px; }

.hint { color: var(--muted); font-size: 14px; margin: 12px 0 0; max-width: 60ch; }
.err { color: var(--danger); font-size: 14px; min-height: 1.2em; margin: 10px 0 0; white-space: pre-line; }

/* вход */
.login { max-width: 400px; margin: 8vh auto 0; }
.login .btn.primary { width: 100%; margin-top: 16px; }

/* зона загрузки */
.drop {
  margin-top: 8px;
  border: 2px dashed var(--line);
  border-radius: var(--radius-lg);
  padding: 40px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  transition: border-color .15s, background-color .15s;
}
.drop.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.drop .hint { text-align: center; }

/* подготовка пакета */
.stage {
  margin-top: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.stage-count { margin: 0 0 12px; font-weight: 600; }
.stage-thumbs {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  gap: 8px;
  overflow-x: auto;
}
.stage-thumbs li { flex: none; width: 64px; height: 64px; border-radius: 6px; overflow: hidden; }

/* шахматка — видно прозрачность */
.checker, .frame, .stage-thumbs li {
  background-color: var(--check-b);
  background-image: conic-gradient(var(--check-a) 25%, transparent 0 50%, var(--check-a) 0 75%, transparent 0);
  background-size: 16px 16px;
}
.stage-thumbs img { width: 100%; height: 100%; object-fit: contain; }

/* пакет */
.batch { margin-top: 28px; }
.batch-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  flex-wrap: wrap;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}
.batch-title { margin: 0; font-size: 18px; }
.batch-meta { margin: 2px 0 0; color: var(--muted); font-size: 14px; }

.sheet {
  list-style: none;
  margin: 16px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}

.tile {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.tile.error { border-color: var(--danger); }

.frame { position: relative; aspect-ratio: 4 / 3; display: flex; align-items: center; justify-content: center; }
.frame img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.num {
  position: absolute;
  top: 8px;
  left: 8px;
  min-width: 28px;
  height: 28px;
  padding: 0 8px;
  border-radius: 14px;
  background: var(--ink);
  color: var(--bg);
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-variant-numeric: tabular-nums;
}

.tile-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.name { margin: 0; font-weight: 600; font-size: 15px; overflow-wrap: anywhere; }
.status { margin: 0; color: var(--muted); font-size: 13px; }
.tile.error .status { color: var(--danger); }

.badge { margin: 0; align-self: flex-start; font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 6px; }
.badge.dup { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }

.bar { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }

.link-row { display: flex; gap: 8px; }
.url { font-size: 13px; padding: 7px 10px; min-width: 0; color: var(--muted); }

.retry { align-self: flex-start; font-size: 14px; }

/* уведомление */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom, 0));
  transform: translate(-50%, 16px);
  max-width: min(92vw, 520px);
  background: var(--ink);
  color: var(--bg);
  padding: 12px 18px;
  border-radius: 10px;
  font-size: 15px;
  white-space: pre-line;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 560px) {
  .top { padding: 12px 16px; }
  main { padding: 0 16px 48px; }
  .drop { padding: 28px 16px; }
  .btn.big { width: 100%; }
  .sheet { grid-template-columns: 1fr; gap: 12px; }
  .tile { flex-direction: row; }
  .frame { flex: none; width: 104px; min-height: 104px; aspect-ratio: auto; }
  .tile-body { flex: 1; }
  .num { top: 6px; left: 6px; min-width: 24px; height: 24px; font-size: 13px; }
  .batch-head .copy-all { width: 100%; }
  .stage .row .btn { flex: 1 1 100%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
