:root {
  --brand: #c8102e;
  --brand-dark: #9e0c24;
  --bg: #f3f3f4;
  --card: #ffffff;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --line: #dcdce0;
  --field: #fafafa;
  --on: #c8102e;
  --on-text: #ffffff;
  --ok: #1e7f3c;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --card: #1e1e21;
    --text: #f0f0f2;
    --muted: #a0a0a8;
    --line: #36363b;
    --field: #26262a;
    --ok: #4cc06e;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: #fff; border-bottom: 3px solid var(--brand);
}
.brand { display: flex; }
.logo { height: 34px; width: auto; display: block; }
.topbar-title { flex: 1; font-weight: 700; color: #333; font-size: 17px; }
.topbar-link { color: var(--brand); font-weight: 600; text-decoration: none; padding: 8px 4px; }

.view { max-width: 720px; margin: 0 auto; padding: 12px 16px 24px; }

/* ---------- cards & sections ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin: 0 0 12px;
}
.card h2, .sect {
  margin: 0 0 10px; font-size: 13px; letter-spacing: .06em; text-transform: uppercase; color: var(--brand);
}
.sect { margin: 20px 4px 10px; }
.sub { font-size: 13px; color: var(--muted); margin: 6px 0 6px; font-weight: 600; }
.muted { color: var(--muted); font-size: 13px; }

.doc-head { display: flex; justify-content: space-between; align-items: center; margin: 4px 4px 12px; gap: 12px; }
.num { font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; }
.status { font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--line); color: var(--text); white-space: nowrap; }
.status.ok { background: var(--ok); color: #fff; }

/* ---------- fields ---------- */
.fld { display: block; margin: 0 0 12px; }
.fld > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.fld input, .fld textarea {
  width: 100%; font: inherit; font-size: 17px; color: var(--text);
  background: var(--field); border: 1px solid var(--line); border-radius: 10px;
  padding: 11px 12px; min-height: 48px;
}
.fld textarea { resize: vertical; min-height: 96px; }
.fld input:focus, .fld textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
.fld.w-half { max-width: 50%; }
.fld.err input, .dims .err { border-color: var(--brand); background: rgba(200, 16, 46, .06); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.row2 > .fld { min-width: 0; }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  flex: 1 1 0; min-width: 64px; min-height: 46px; padding: 8px 10px;
  font: inherit; font-weight: 600; font-size: 15px;
  background: var(--field); color: var(--text); border: 1px solid var(--line); border-radius: 10px; cursor: pointer;
}
.seg button.on { background: var(--on); color: var(--on-text); border-color: var(--on); }

.chk { display: flex; align-items: center; gap: 12px; min-height: 48px; margin: 0 0 6px; font-size: 16px; cursor: pointer; }
.chk input { width: 24px; height: 24px; accent-color: var(--brand); margin: 0; }

/* ---------- colli ---------- */
.collo { border-left: 4px solid var(--brand); scroll-margin-top: 64px; }
.fld, .dims input { scroll-margin-top: 72px; scroll-margin-bottom: 90px; }
.collo-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; gap: 8px; }
.collo-head h3 { margin: 0; font-size: 18px; }
.collo-actions { display: flex; gap: 6px; }
.dims { display: grid; grid-template-columns: 1fr auto 1fr auto 1fr; align-items: start; gap: 6px; }
.dims b { padding-top: 13px; color: var(--muted); }
.dims label { display: block; min-width: 0; }
.dims input {
  width: 100%; font: inherit; font-size: 19px; font-weight: 700; text-align: center; color: var(--text);
  background: var(--field); border: 1px solid var(--line); border-radius: 10px; padding: 10px 4px; min-height: 52px;
  font-variant-numeric: tabular-nums;
}
.dims input:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
.dims small { display: block; text-align: center; color: var(--muted); font-size: 12px; margin-top: 3px; }
.info { font-size: 13px; color: var(--muted); margin: -4px 0 12px; min-height: 18px; font-variant-numeric: tabular-nums; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 50px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--line);
  font: inherit; font-weight: 700; font-size: 16px; cursor: pointer; background: var(--card); color: var(--text);
}
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn.primary:active { background: var(--brand-dark); }
.btn.block, .btn.add { width: 100%; }
.btn.add { border-style: dashed; color: var(--brand); background: transparent; margin-bottom: 12px; }
.small {
  font: inherit; font-size: 14px; font-weight: 600; min-height: 38px; padding: 0 12px;
  border-radius: 9px; border: 1px solid var(--line); background: var(--field); color: var(--text); cursor: pointer;
}
.small.danger { color: var(--brand); }

.summary {
  margin: 4px 4px 12px; font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.summary strong { color: var(--text); }

.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  display: flex; gap: 10px; justify-content: center;
  padding: 10px 16px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--card); border-top: 1px solid var(--line);
}
.actionbar .btn { flex: 1; max-width: 340px; }
.actionbar .btn.primary { flex: 1.7; }
.spacer { height: 84px; }

/* ---------- archivio ---------- */
.item { display: flex; flex-direction: column; gap: 10px; }
.item-main { cursor: pointer; }
.item-cli { font-size: 16px; margin: 2px 0; }
.item-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.empty { text-align: center; color: var(--muted); padding: 32px 8px; }
.foot { text-align: center; margin-top: 20px; }

/* ---------- modal & toast ---------- */
.modal {
  position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .45);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal[hidden] { display: none; }
.sheet {
  width: 100%; max-width: 560px; background: var(--card); color: var(--text);
  border-radius: 16px 16px 0 0; padding: 20px 16px; padding-bottom: max(20px, env(safe-area-inset-bottom));
}
.sheet h3 { margin: 0 0 6px; font-size: 19px; }
.sheet .fname { font-size: 14px; color: var(--muted); word-break: break-all; margin-bottom: 16px; }
.sheet .btn { width: 100%; margin-bottom: 10px; }
.sheet ul { margin: 0 0 16px; padding-left: 20px; font-size: 15px; }

.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 50;
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 15px;
  max-width: calc(100% - 32px); box-shadow: 0 4px 16px rgba(0, 0, 0, .25);
}
.toast[hidden] { display: none; }

@media (max-width: 380px) {
  .fld.w-half { max-width: 100%; }
  .row2 { grid-template-columns: 1fr; }
}
