/* Editor chrome. The invoice itself is styled in invoice.css. */

:root {
  --desk: #e9e9e6;
  --panel: #ffffff;
  --panel-2: #f6f6f4;
  --line: #e1e1dd;
  --line-strong: #cfcfca;
  --text: #151515;
  --muted: #6d6d68;
  --ink: #111111;
  --on-ink: #ffffff;
  --accent: #ee0000;
  --accent-soft: rgba(238, 0, 0, 0.08);
  --focus: #111111;
  --shadow-paper: 0 1px 2px rgba(0, 0, 0, 0.08), 0 12px 32px rgba(0, 0, 0, 0.12);
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --desk: #121212;
    --panel: #1b1b1b;
    --panel-2: #232322;
    --line: #2e2e2c;
    --line-strong: #3d3d3a;
    --text: #efefec;
    --muted: #a3a39d;
    --ink: #f2f2ef;
    --on-ink: #111111;
    --accent: #ff4a4a;
    --accent-soft: rgba(255, 74, 74, 0.12);
    --focus: #f2f2ef;
    --shadow-paper: 0 1px 2px rgba(0, 0, 0, 0.5), 0 12px 32px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --desk: #121212;
  --panel: #1b1b1b;
  --panel-2: #232322;
  --line: #2e2e2c;
  --line-strong: #3d3d3a;
  --text: #efefec;
  --muted: #a3a39d;
  --ink: #f2f2ef;
  --on-ink: #111111;
  --accent: #ff4a4a;
  --accent-soft: rgba(255, 74, 74, 0.12);
  --focus: #f2f2ef;
  --shadow-paper: 0 1px 2px rgba(0, 0, 0, 0.5), 0 12px 32px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--desk);
  color: var(--text);
  font-family: 'Rubik', 'Segoe UI', Tahoma, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.4;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.app { height: 100%; display: grid; grid-template-rows: auto 1fr; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; margin-inline-end: auto; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 8px; background: #fff; border: 1px solid var(--line);
  display: grid; place-items: center; overflow: hidden; flex: none;
}
.brand-mark img { width: 28px; height: auto; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-name { font-weight: 600; font-size: 15px; }
.brand-doc { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 42ch; }
.save-state { font-size: 12px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.save-state::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: #3a9a5b; }
.save-state.is-dirty::before { background: #c9a227; }
.bar-group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 36px; padding: 0 14px;
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--panel); color: var(--text);
  font-weight: 500; font-size: 13.5px; cursor: pointer; white-space: nowrap;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover { background: var(--panel-2); border-color: var(--text); }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn-primary { background: var(--ink); color: var(--on-ink); border-color: var(--ink); }
.btn-primary:hover { background: var(--ink); opacity: 0.88; border-color: var(--ink); }
.btn-quiet { border-color: transparent; background: transparent; }
.btn-quiet:hover { background: var(--panel-2); border-color: var(--line); }
.btn-sm { height: 30px; padding: 0 10px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn-danger:hover { color: var(--accent); border-color: var(--accent); }
.btn:disabled { opacity: 0.4; cursor: default; }
.icon-btn {
  width: 30px; height: 30px; display: inline-grid; place-items: center;
  border: 1px solid transparent; border-radius: 6px; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); border-color: var(--line); }
.icon-btn.danger:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.icon-btn svg { width: 16px; height: 16px; }
.icon-btn:disabled { opacity: 0.3; cursor: default; }
.icon-btn.is-confirm { width: auto; padding: 0 8px; color: var(--accent); border-color: var(--accent); font-size: 12px; font-weight: 600; }

/* Menus */
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); inset-inline-end: 0; z-index: 30;
  min-width: 260px; padding: 6px;
  background: var(--panel); border: 1px solid var(--line-strong); border-radius: 10px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.16);
}
.menu button {
  display: flex; flex-direction: column; align-items: flex-start; gap: 1px; width: 100%;
  padding: 8px 10px; border: 0; border-radius: 6px; background: transparent; cursor: pointer; text-align: start;
}
.menu button:hover, .menu button:focus-visible { background: var(--panel-2); }
.menu button small { color: var(--muted); font-size: 12px; }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; }

/* ---------- Main layout ---------- */
.main { min-height: 0; display: grid; grid-template-columns: minmax(360px, 460px) 1fr; }
.editor {
  min-height: 0; overflow-y: auto;
  background: var(--panel);
  border-inline-end: 1px solid var(--line);
  padding: 8px 16px 48px;
}
.preview {
  min-height: 0; overflow: auto;
  padding: 28px 24px 64px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.preview-bar { width: 100%; display: flex; justify-content: center; gap: 8px; align-items: center; color: var(--muted); font-size: 12px; }
.preview-bar .pages { font-variant-numeric: tabular-nums; }
.sheet-frame { position: relative; flex: none; }
.sheet-frame .sheet { transform-origin: top left; box-shadow: var(--shadow-paper); position: absolute; top: 0; left: 0; }

.view-switch { display: none; }

/* ---------- Editor sections ---------- */
.section { border-bottom: 1px solid var(--line); }
.section:last-child { border-bottom: 0; }
.section-head {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 16px 0 12px; border: 0; background: none; cursor: pointer; text-align: start;
}
.section-head h2 { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.section-head .count { font-size: 12px; color: var(--muted); }
.section-head .chev { margin-inline-start: auto; color: var(--muted); transition: transform 0.15s; }
.section.is-collapsed .chev { transform: rotate(-90deg); }
.section.is-collapsed .section-body { display: none; }
.section-body { padding-bottom: 18px; display: flex; flex-direction: column; gap: 12px; }

/* Fields */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span, .field-label { font-size: 12px; font-weight: 500; color: var(--muted); }
.field input[type="text"], .field input[type="number"], .field input[type="date"], .field input[type="email"], .field input[type="tel"],
.field select, .field textarea, .rep input {
  width: 100%; min-width: 0; height: 36px; padding: 0 10px;
  border: 1px solid var(--line-strong); border-radius: 6px; background: var(--panel);
}
.field textarea { height: auto; min-height: 84px; padding: 8px 10px; resize: vertical; line-height: 1.5; }
.field input:focus, .field select:focus, .field textarea:focus, .rep input:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 1px var(--focus); }
.field .hint { font-size: 11.5px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--ink); margin: 0; }
.checks { display: flex; flex-wrap: wrap; gap: 8px 18px; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 0 12px; height: 32px; cursor: pointer; font-size: 13px; }
.seg button + button { border-inline-start: 1px solid var(--line-strong); }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--on-ink); }
.bi { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.bi .tag { font-size: 10.5px; color: var(--muted); letter-spacing: 0.08em; }
.img-pick { display: flex; align-items: center; gap: 10px; }
.img-pick .thumb { width: 64px; height: 44px; border: 1px solid var(--line); border-radius: 6px; background: #fff; display: grid; place-items: center; overflow: hidden; }
.img-pick .thumb img { max-width: 56px; max-height: 38px; }

/* Repeater rows (items, custom fields) */
.rep { display: flex; flex-direction: column; gap: 8px; }
.rep-row { display: grid; gap: 6px; align-items: start; padding: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.rep-row .rep-main { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.rep-row .rep-nums { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.rep-row .rep-nums.two { grid-template-columns: 1fr 1fr; }
.rep-row label { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--muted); min-width: 0; }
.rep-row .rep-tools { display: flex; gap: 2px; justify-content: flex-end; }
.rep-row .line-total { font-size: 12px; color: var(--muted); align-self: center; font-variant-numeric: tabular-nums; }
.rep-kv { grid-template-columns: 1fr 1fr auto; align-items: center; }

/* ---------- Block cards ---------- */
.blocks { display: flex; flex-direction: column; gap: 8px; }
.card {
  border: 1px solid var(--line-strong); border-radius: 10px; background: var(--panel);
  transition: border-color 0.12s, box-shadow 0.12s;
}
.card.is-open { border-color: var(--text); }
.card.is-active { box-shadow: 0 0 0 2px var(--accent-soft), 0 0 0 1px var(--accent); border-color: var(--accent); }
.card.is-dragging { opacity: 0.4; }
.card.drop-before { box-shadow: 0 -3px 0 0 var(--accent); }
.card.drop-after { box-shadow: 0 3px 0 0 var(--accent); }
.card-head { display: flex; align-items: center; gap: 6px; padding: 6px 6px 6px 4px; }
.handle { cursor: grab; color: var(--muted); width: 22px; height: 30px; display: grid; place-items: center; border-radius: 4px; flex: none; }
.handle:active { cursor: grabbing; }
.handle svg { width: 14px; height: 14px; }
.card-title { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 4px 2px; border: 0; background: none; text-align: start; cursor: pointer; }
.card-title strong { font-size: 13.5px; font-weight: 600; }
.card-title small { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-tools { display: flex; gap: 1px; flex: none; }
.card-body { padding: 4px 12px 14px; display: flex; flex-direction: column; gap: 12px; border-top: 1px solid var(--line); padding-top: 12px; }
.pill { font-size: 10.5px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); border: 1px solid var(--line-strong); border-radius: 999px; padding: 1px 7px; margin-inline-start: 6px; vertical-align: 1px; }

.add-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 10px; border: 1px dashed var(--line-strong); border-radius: 10px; }
.add-grid button { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); cursor: pointer; text-align: start; }
.add-grid button:hover { border-color: var(--text); background: var(--panel); }
.add-grid button strong { font-size: 13px; font-weight: 600; }
.add-grid button small { font-size: 11.5px; color: var(--muted); line-height: 1.3; }
.add-head { display: flex; align-items: center; justify-content: space-between; }

/* ---------- Drawer ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.32); z-index: 40; }
.drawer {
  position: fixed; top: 0; bottom: 0; inset-inline-end: 0; z-index: 41;
  width: min(440px, 100%); background: var(--panel); border-inline-start: 1px solid var(--line);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px);
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.drawer-body { overflow-y: auto; padding: 12px 16px 24px; display: flex; flex-direction: column; gap: 8px; }
.drawer-search { width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--panel); margin-bottom: 4px; }
.inv-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; }
.inv-row.is-current { border-color: var(--text); }
.inv-row .inv-open { border: 0; background: none; padding: 0; text-align: start; cursor: pointer; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.inv-row .inv-open strong { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.inv-row .inv-open small { color: var(--muted); font-size: 12px; }
.inv-row .inv-amount { font-weight: 600; font-variant-numeric: tabular-nums; text-align: end; }
.inv-row .inv-tools { grid-column: 1 / -1; display: flex; gap: 4px; justify-content: flex-end; }
.empty { color: var(--muted); font-size: 13px; padding: 20px 0; text-align: center; }

/* Toast */
.toast {
  position: fixed; bottom: calc(20px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--on-ink); padding: 10px 16px; border-radius: 8px; font-size: 13px; z-index: 60;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

/* ---------- Narrow screens ---------- */
@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; }
  .view-switch { display: flex; justify-content: center; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
  .app { grid-template-rows: auto auto 1fr; }
  .app[data-view="edit"] .preview { display: none; }
  .app[data-view="preview"] .editor { display: none; }
  .editor { border-inline-end: 0; }
  .preview { padding: 16px 16px 48px; }
  .brand-doc { max-width: 24ch; }
  .hide-narrow { display: none; }
}
@media (max-width: 480px) {
  .grid3 { grid-template-columns: 1fr 1fr; }
  .add-grid { grid-template-columns: 1fr; }
  .topbar { gap: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
