:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --ink: #1b1d22;
  --muted: #62656d;
  --line: #e3e1da;
  --accent: #0f5f55;
  --accent-ink: #ffffff;
  --accent-soft: #e3efec;
  --warn: #9a5b00;
  --warn-soft: #fff4e0;
  --danger: #a3261b;
  --radius: 14px;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --shadow: 0 1px 2px rgb(20 20 20 / 0.04), 0 4px 16px rgb(20 20 20 / 0.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121315; --surface: #1c1e21; --ink: #ecebe6; --muted: #a2a4a9; --line: #2e3034;
    --accent: #5fc2b2; --accent-ink: #0b1a18; --accent-soft: #1d2f2c; --warn: #f0b660; --warn-soft: #2e2516;
    --danger: #f08a7f; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121315; --surface: #1c1e21; --ink: #ecebe6; --muted: #a2a4a9; --line: #2e3034;
  --accent: #5fc2b2; --accent-ink: #0b1a18; --accent-soft: #1d2f2c; --warn: #f0b660; --warn-soft: #2e2516;
  --danger: #f08a7f; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
h2 { font-size: 1.1rem; margin: 0 0 12px; letter-spacing: -0.01em; }
h3 { margin: 0 0 8px; }
code { background: var(--accent-soft); padding: 1px 5px; border-radius: 5px; font-size: 0.9em; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { color: var(--ink); text-decoration: none; font-size: 1.05rem; }
.brand b { color: var(--accent); }
.pill { font-size: 0.75rem; padding: 3px 9px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.save-state { margin-left: auto; font-size: 0.8rem; color: var(--muted); }

/* layout */
.layout { max-width: 1240px; margin: 0 auto; padding: 16px; display: grid; gap: 20px; }
.editor { display: grid; gap: 16px; min-width: 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 16px; box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 4px; }
.card-head h2 { margin: 0; }

/* fields */
.field { display: grid; gap: 5px; margin: 0 0 12px; border: 0; padding: 0; min-width: 0; }
.field > span, .field legend { font-size: 0.85rem; color: var(--muted); font-weight: 500; padding: 0; }
.field small { color: var(--muted); font-size: 0.78rem; }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); width: 100%;
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 44px;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 160px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; gap: 10px; align-items: center; margin: 6px 0 12px; cursor: pointer; }
.check input { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); flex: none; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 6px 0 10px; }
.note { background: var(--accent-soft); color: var(--ink); border-radius: 10px; padding: 10px 12px; font-size: 0.9rem; margin: 4px 0 0; }
.fine { color: var(--muted); font-size: 0.8rem; margin: 10px 0 0; }

.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 2px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; min-height: 0; }
.seg span, .seg button {
  display: block; padding: 8px 16px; border-radius: 9px; font: inherit; font-size: 0.95rem; border: 0;
  background: transparent; color: var(--muted); cursor: pointer; transition: background-color 150ms ease, color 150ms ease;
}
.seg input:checked + span, .seg button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }

/* buttons */
.btn {
  font: inherit; font-weight: 600; border-radius: 11px; border: 1px solid transparent; padding: 10px 16px;
  min-height: 44px; cursor: pointer; background: var(--bg); color: var(--ink);
  transition: transform 140ms var(--ease-out), background-color 150ms ease;
}
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.secondary { background: var(--surface); border-color: var(--accent); color: var(--accent); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--line); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 0.88rem; }
.btn.big { width: 100%; min-height: 52px; font-size: 1.02rem; margin-top: 8px; }
.btn.link { background: none; color: var(--accent); padding: 8px 0; min-height: 40px; text-decoration: underline; text-underline-offset: 3px; font-weight: 500; }
.btn.add { width: 100%; border-style: dashed; margin-top: 4px; }
@media (hover: hover) and (pointer: fine) {
  .btn.primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
  .btn.ghost:hover, .btn.danger:hover { background: var(--bg); }
}

/* items */
.items { list-style: none; margin: 8px 0; padding: 0; display: grid; gap: 10px; }
.items .empty { color: var(--muted); font-size: 0.92rem; padding: 14px; border: 1px dashed var(--line); border-radius: 12px; }
.item { border: 1px solid var(--line); border-radius: 12px; padding: 10px; transition: border-color 150ms ease; }
.item.warn { border-color: color-mix(in srgb, var(--warn) 45%, var(--line)); }
.item-main { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: end; }
/* Same-prices shortcut hides the anchor input, except on sale rows (the anchor excludes the sale price). */
.same-prices .item:not(.sale) .item-main .anchor-in, .same-prices .part .anchor-in { display: none; }
.same-prices .item:not(.sale) .item-main { grid-template-columns: 1fr auto; }
.price-in { display: grid; grid-template-columns: auto; position: relative; width: 108px; }
.price-in .lbl { font-size: 0.72rem; color: var(--muted); margin: 0 0 3px 2px; }
.price-in input { text-align: right; padding-right: 26px; font-variant-numeric: tabular-nums; }
.price-in .cur { position: absolute; right: 10px; bottom: 11px; color: var(--muted); pointer-events: none; }
.item-status { margin: 6px 2px 0; font-size: 0.82rem; color: var(--warn); }
.item-status:empty { display: none; }
.more summary { cursor: pointer; color: var(--muted); font-size: 0.85rem; margin-top: 6px; padding: 4px 2px; list-style: none; width: max-content; }
.more summary::-webkit-details-marker { display: none; }
.more summary::before { content: "▸ "; display: inline-block; transition: transform 150ms var(--ease-out); }
.more[open] summary::before { transform: rotate(90deg); }
.more-body { padding-top: 10px; }

/* components ("Razradi cijenu") */
.parts { margin-top: 10px; padding: 10px; border-radius: 10px; background: var(--bg); }
.parts-head { margin: 0 0 8px; font-size: 0.84rem; color: var(--muted); }
.parts-head b { color: var(--ink); }
.part-list { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 8px; }
.part { display: grid; grid-template-columns: 1fr 64px auto auto auto; gap: 6px; align-items: end; }
.same-prices .part { grid-template-columns: 1fr 64px auto auto; }
.part .p-del { min-width: 40px; padding: 6px 0; }
.part:not(:first-child) .lbl { display: none; }
@media (max-width: 560px) {
  .part, .same-prices .part { grid-template-columns: 1fr 1fr auto; }
  .part .p-name { grid-column: 1 / -1; }
  .part .p-unit { display: none; }
  .part .price-in { width: auto; }
}
.item-tools { display: flex; gap: 8px; flex-wrap: wrap; }
.item-tools .danger { margin-left: auto; }

.problems { margin: 0 0 4px; padding: 0 0 0 18px; color: var(--warn); font-size: 0.9rem; }
.problems:empty { display: none; }
.problems .pub-head { list-style: none; margin: 6px 0 0 -18px; color: var(--muted); }
.problems .pub-head ul { margin: 2px 0 0; padding-left: 18px; color: var(--warn); }
.actions-card .note { margin: 0 0 10px; }

/* publish dialog */
dialog.wide { width: min(640px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); overflow: auto; }
dialog h4 { margin: 18px 0 8px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.slugbox { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; padding-left: 12px; background: var(--surface); }
.slugbox span { color: var(--muted); font-size: 0.92rem; white-space: nowrap; }
.slugbox input { border: 0; box-shadow: none; padding-left: 2px; }
.slugbox:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.when { display: flex; flex-wrap: wrap; gap: 4px 18px; align-items: center; }
.when .check { margin: 4px 0; }
.when input[type="date"] { width: auto; min-height: 36px; padding: 4px 8px; }
#c-checks .check { align-items: flex-start; margin: 8px 0; font-size: 0.92rem; }
#c-checks .check input { margin-top: 1px; }
.ts { min-height: 65px; margin: 4px 0 8px; }

/* dialogs & toast */
dialog {
  border: 0; border-radius: 18px; padding: 22px 18px; width: min(560px, calc(100vw - 32px));
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
  opacity: 1; transform: scale(1); transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
}
@starting-style { dialog[open] { opacity: 0; transform: scale(0.96); } }
dialog::backdrop { background: rgb(15 15 15 / 0.4); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.linkbox { display: flex; gap: 8px; }
.linkbox input { font-size: 0.85rem; }

.toast {
  position: fixed; left: 50%; bottom: calc(88px + env(safe-area-inset-bottom)); z-index: 20;
  display: flex; gap: 12px; align-items: center; max-width: calc(100vw - 32px);
  background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 12px; font-size: 0.92rem;
  opacity: 0; transform: translate(-50%, 8px); pointer-events: none;
  transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button { background: none; border: 0; color: inherit; font: inherit; font-weight: 700; text-decoration: underline; cursor: pointer; padding: 4px; }

/* mobile bottom bar */
.mobile-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; gap: 10px; align-items: center;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line);
}
.mobile-bar .tabs { flex: 1; }
.mobile-bar .tabs button { flex: 1; }
.mobile-bar .btn { padding-inline: 22px; }
body[data-tab="edit"] .preview { display: none; }
body[data-tab="preview"] .editor { display: none; }

/* preview paper */
.preview { min-width: 0; }
.paper-wrap { background: var(--line); border-radius: var(--radius); padding: 14px; overflow: hidden; }
/* On screen the sheet is a real page width, zoomed to fit (builder.js fitSheet). */
.sheet {
  background: #fff; color: #111; margin: 0 auto; width: 210mm; min-height: 297mm;
  padding: 14mm; box-shadow: 0 2px 10px rgb(0 0 0 / 0.08); border-radius: 4px; color-scheme: light;
}
body[data-format="A5"] .sheet { width: 148mm; min-height: 210mm; padding: 10mm; }

@media (min-width: 980px) {
  body { padding-bottom: 0; }
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; padding: 24px; }
  .mobile-bar { display: none; }
  body[data-tab] .preview, body[data-tab] .editor { display: grid; }
  .preview { position: sticky; top: 72px; }
  .toast { bottom: 24px; }
}
@media (max-width: 560px) {
  /* two prices + name don't fit one row: name on top, prices side by side below */
  body:not(.same-prices) .item-main { grid-template-columns: 1fr 1fr; }
  body:not(.same-prices) .item-main .i-name { grid-column: 1 / -1; }
  body:not(.same-prices) .price-in { width: auto; }
}
@media (max-width: 420px) {
  .row2 { grid-template-columns: 1fr; gap: 0; }
  .price-in { width: 92px; }
}

/* the printed list (same markup on screen and paper) */
.sh-head h1 { font-size: 22pt; margin: 0; letter-spacing: -0.01em; line-height: 1.15; }
.sh-head p { margin: 4px 0 0; color: #444; font-size: 11pt; }
.sh-title { font-size: 13pt; margin: 18px 0 8px; text-transform: uppercase; letter-spacing: 0.08em; color: #333; }
.sh-table { width: 100%; border-collapse: collapse; font-size: 11pt; }
.sh-table th { text-align: left; font-size: 9pt; text-transform: uppercase; letter-spacing: 0.04em; color: #444; border-bottom: 1.5px solid #111; padding: 6px 4px; }
.sh-table td { border-bottom: 0.5pt solid #ccc; padding: 7px 4px; vertical-align: top; }
.sh-table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.sh-table td small { display: block; color: #555; font-size: 8.5pt; }
.sh-table td .sh-sale { color: #7a3d00; }
.sh-table tr { break-inside: avoid; }
.sh-table .ph td { color: #bbb; }
.sh-table .sh-grp td { border-bottom: 0; padding-bottom: 2px; }
.sh-table .sh-sub td:first-child { padding-left: 14px; }
.sh-n { font-weight: 600; }
.sh-foot { margin-top: 16px; font-size: 8.5pt; color: #444; }
.sh-foot p { margin: 3px 0; }
.sh-made { color: #888; }
body[data-format="A5"] .sh-head h1 { font-size: 16pt; }
body[data-format="A5"] .sh-table { font-size: 9.5pt; }

@media print {
  body { background: #fff; padding: 0; }
  .no-print, .editor { display: none !important; }
  .layout { display: block; padding: 0; max-width: none; }
  .preview { display: block !important; position: static; }
  .paper-wrap { background: none; padding: 0; }
  .sheet, body[data-format="A5"] .sheet { box-shadow: none; padding: 0; width: auto; min-height: 0; border-radius: 0; zoom: 1 !important; }
}

@media (prefers-reduced-motion: reduce) {
  .btn, dialog, .toast, .more summary::before { transition: opacity 150ms ease; }
}
