:root{
  --ink:        #0F2038;
  --ink-2:      #16304F;
  --ink-3:      #1D3E63;
  --line:       #3E6690;
  --line-dim:   #274B70;
  --paper:      #F7F4EC;
  --paper-dim:  #EDE8DA;
  --accent:     #E1590C;
  --accent-hi:  #FF7A2E;
  --ok:         #4C8C68;
  --muted:      #8FA6BE;
  --text-on-ink:#DCE7F2;
  --text-dim:   #9FB4C9;

  --f-display: "Space Grotesk", sans-serif;
  --f-body: "IBM Plex Sans", sans-serif;
  --f-mono: "IBM Plex Mono", monospace;
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0;
  font-family:var(--f-body);
  background:var(--ink);
  color:var(--text-on-ink);
}
.mono{ font-family:var(--f-mono); }

.visually-hidden{
  position:absolute; width:1px; height:1px; padding:0; margin:-1px;
  overflow:hidden; clip:rect(0,0,0,0); white-space:nowrap; border:0;
}

/* ============ TOP BAR ============ */
.topbar{
  height:56px; box-sizing:border-box;
  display:flex; align-items:center; justify-content:space-between;
  gap:12px;
  padding:0 20px;
  background:var(--ink);
  border-bottom:1px solid var(--line-dim);
}
.topbar-brand{ display:flex; align-items:center; gap:10px; min-width:0; }
.topbar-brand-mark{ width:32px; height:32px; flex:none; display:flex; }
.topbar-brand-mark svg{ display:block; }
.topbar-brand-title{ font-family:var(--f-display); font-weight:700; font-size:16px; letter-spacing:.01em; }
.topbar-brand-sub{ font-size:10.5px; color:var(--text-dim); text-transform:uppercase; letter-spacing:.08em; }
.topbar-lang select{
  background:var(--ink-2);
  color:var(--text-on-ink);
  border:1px solid var(--line-dim);
  border-radius:2px;
  padding:7px 10px;
  font-family:var(--f-body);
  font-size:12.5px;
  cursor:pointer;
}
.topbar-lang select:focus{ outline:none; border-color:var(--accent); }

.app{
  display:grid;
  grid-template-columns:340px 1fr;
  height:calc(100vh - 56px);
}

/* ============ RAIL ============ */
.rail{
  background:var(--ink);
  border-right:1px solid var(--line-dim);
  display:flex;
  flex-direction:column;
  min-height:0;
}

.rail-actions{
  display:grid; grid-template-columns:1fr 1fr; gap:8px;
  padding:14px 16px;
  border-bottom:1px solid var(--line-dim);
}
.rail-actions .btn-accent{ grid-column:1 / -1; }

.rail-status{
  min-height:18px;
  padding:0 16px;
  font-size:12px;
  color:var(--ok);
  font-family:var(--f-mono);
}

.rail-scroll{
  flex:1; overflow-y:auto;
  padding:8px 16px 40px;
}

/* ============ FORM ELEMENTS ============ */
.rail-supergroup-label{
  font-family:var(--f-display);
  font-weight:700;
  font-size:11px;
  text-transform:uppercase;
  letter-spacing:.12em;
  color:var(--accent);
  padding:16px 0 6px;
  margin-top:4px;
  border-top:1px solid var(--line-dim);
}
.rail-supergroup-label:first-child{
  margin-top:0;
  border-top:none;
  padding-top:2px;
}

.field-group{
  border-bottom:1px solid var(--line-dim);
  padding:12px 0;
}
.field-group > summary{
  cursor:pointer;
  font-family:var(--f-display);
  font-weight:600;
  font-size:13px;
  text-transform:uppercase;
  letter-spacing:.08em;
  color:var(--text-on-ink);
  padding:4px 0;
  list-style:none;
  display:flex; align-items:center; gap:8px;
}
.field-group > summary::before{
  content:"▸";
  color:var(--accent);
  transition:transform .15s ease;
  font-size:11px;
}
.field-group[open] > summary::before{ transform:rotate(90deg); }
.field-group > summary::-webkit-details-marker{ display:none; }

.lbl{
  display:block;
  font-size:11px;
  color:var(--text-dim);
  text-transform:uppercase;
  letter-spacing:.06em;
  margin:10px 0 4px;
}
.inp{
  width:100%;
  background:var(--ink-2);
  border:1px solid var(--line-dim);
  color:var(--text-on-ink);
  font-family:var(--f-body);
  font-size:13px;
  padding:8px 10px;
  border-radius:2px;
  resize:vertical;
  overflow:hidden;
}
.inp:focus{
  outline:none;
  border-color:var(--accent);
}
.hint{
  font-size:11.5px;
  color:var(--text-dim);
  line-height:1.5;
  margin:6px 0;
}

/* ============ BUTTONS ============ */
.btn{
  font-family:var(--f-body);
  font-weight:600;
  font-size:12.5px;
  border:1px solid var(--line);
  background:transparent;
  color:var(--text-on-ink);
  padding:9px 12px;
  border-radius:2px;
  cursor:pointer;
  text-align:center;
  display:inline-flex; align-items:center; justify-content:center;
  gap:6px;
  transition:background .12s ease, border-color .12s ease, color .12s ease;
}
.btn-ghost:hover{ border-color:var(--accent); color:var(--accent-hi); }
.btn-accent{
  background:var(--accent);
  border-color:var(--accent);
  color:#1a0e05;
}
.btn-accent:hover{ background:var(--accent-hi); border-color:var(--accent-hi); }
.btn-tiny{ padding:6px 10px; font-size:11.5px; margin-top:6px; }
.btn-sm{ padding:7px 14px; font-size:12px; }

.icon-btn{
  border:none; background:transparent; color:var(--muted);
  cursor:pointer; font-size:13px; padding:2px 6px; line-height:1;
}
.icon-btn:hover{ color:var(--accent); }

/* ============ LOGO DROP ============ */
.logo-drop{
  border:1px dashed var(--line);
  border-radius:2px;
  min-height:70px;
  display:flex; align-items:center; justify-content:center;
  cursor:pointer;
  padding:8px;
  margin-top:6px;
  background:var(--ink-2);
}
.logo-drop:hover{ border-color:var(--accent); }
.logo-placeholder{ font-size:11.5px; color:var(--text-dim); }
.logo-preview{ max-width:100%; max-height:80px; object-fit:contain; }

/* ============ TAX LIST ============ */
.tax-list{ display:flex; flex-direction:column; gap:6px; margin-top:6px; }
.tax-row{ display:flex; align-items:center; gap:6px; }
.tax-label{
  flex:1; background:var(--ink-2); border:1px solid var(--line-dim);
  color:var(--text-on-ink); font-size:12.5px; padding:6px 8px; border-radius:2px;
}
.tax-rate{
  width:56px; background:var(--ink-2); border:1px solid var(--line-dim);
  color:var(--text-on-ink); font-size:12.5px; padding:6px 8px; border-radius:2px;
  font-family:var(--f-mono);
}
.tax-pct{ font-size:12px; color:var(--text-dim); }

/* ============ CANVAS / DRAFTING TABLE ============ */
.canvas-wrap{
  overflow-y:auto;
  background:
    repeating-linear-gradient(0deg, transparent, transparent 27px, rgba(62,102,144,.10) 27px, rgba(62,102,144,.10) 28px),
    repeating-linear-gradient(90deg, transparent, transparent 27px, rgba(62,102,144,.10) 27px, rgba(62,102,144,.10) 28px),
    var(--ink-3);
  padding:32px;
}
.canvas-toolbar{
  max-width:820px; margin:0 auto 16px;
  display:flex; align-items:center; gap:14px;
}
.canvas-toolbar-hint{ font-size:12px; color:var(--text-dim); }

.sheet{
  max-width:820px;
  margin:0 auto 60px;
  background:var(--paper);
  color:#1B2430;
  padding:40px 44px 28px;
  position:relative;
  box-shadow:0 12px 40px rgba(0,0,0,.35);
  font-family:var(--f-body);
}
.sheet::before{
  content:"";
  position:absolute; inset:14px;
  border:1px solid #C9BFA6;
  pointer-events:none;
}

.sheet-head{
  padding-bottom:14px;
  border-bottom:2px solid var(--accent);
  margin-bottom:18px;
}
.sheet-head-logo{
  text-align:center;
  margin-bottom:16px;
}
.sheet-head-logo img{
  display:inline-block;
  max-width:100%;
  max-height:180px;
  width:auto;
  height:auto;
  object-fit:contain;
}
.sheet-title-fallback{
  font-family:var(--f-display);
  font-weight:700;
  font-size:42px;
  line-height:1.15;
  letter-spacing:.01em;
  text-align:center;
  color:#1B2430;
  text-shadow:0 1px 0 rgba(255,255,255,.5), 0 3px 5px rgba(27,36,48,.28);
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.sheet-head-row{
  display:grid;
  grid-template-columns:1fr 1fr;
  gap:16px;
}
.sheet-head-company-wrap{
  display:flex;
  align-items:flex-start;
  gap:28px;
  min-width:0;
}
.sheet-head-company{ align-self:start; min-width:0; flex:1; }
.sheet-company-name{ font-family:var(--f-display); font-weight:700; font-size:19px; letter-spacing:.01em; }
.sheet-company-address{ font-size:11.5px; color:#4B5A6B; white-space:pre-line; margin-top:2px; }
.sheet-company-contact{ font-size:11.5px; color:#4B5A6B; white-space:pre-line; margin-top:8px; }

.sheet-head-qr{
  flex:none;
  width:auto;
  max-width:130px;
  text-align:center;
}
.sheet-head-qr img{
  width:64px;
  height:64px;
  display:block;
  margin:0 auto;
  image-rendering:pixelated;
  border:1px solid #2B3A4A;
  padding:3px;
  background:#fff;
}
.sheet-qr-caption{
  font-size:8px;
  color:#5A6779;
  margin-top:3px;
  white-space:nowrap;
  line-height:1.3;
}

.sheet-head-client{
  border:1px solid #2B3A4A;
  padding:12px 14px;
  align-self:start;
  justify-self:end;
  width:100%;
}
.sheet-client-name{ font-family:var(--f-display); font-weight:700; font-size:14px; }
.sheet-client-address{ font-size:12px; white-space:pre-line; margin-top:4px; color:#3A4756; }

.sheet-title{
  font-family:var(--f-display);
  font-style:italic;
  font-weight:600;
  font-size:14px;
  text-align:center;
  text-decoration:underline;
  margin:6px 0 22px;
  color:#1B2430;
}

.section-card{ margin-bottom:22px; }
.section-card-title{
  font-family:var(--f-display);
  font-weight:700;
  font-size:13.5px;
  text-decoration:underline;
  margin-bottom:8px;
}
.section-card-desc{ font-size:11.5px; color:#57657A; margin-bottom:6px; white-space:pre-line; }

.items-table, .summary-table{
  width:100%;
  border-collapse:collapse;
  font-size:12px;
}
.items-table th, .items-table td,
.summary-table th, .summary-table td{
  border:1px solid #2B3A4A;
  padding:6px 8px;
  vertical-align:top;
}
.items-table thead th{
  background:var(--ink-2);
  color:var(--text-on-ink);
  font-family:var(--f-display);
  font-weight:600;
  font-size:11px;
  text-transform:uppercase;
  letter-spacing:.04em;
}
.items-table .col-qty{ text-align:right; width:52px; }
.items-table .col-unit{ width:56px; }
.items-table .col-price{ text-align:right; width:112px; }
.items-table .col-total{ text-align:right; width:118px; }
.items-table td.item-line-total{ text-align:right; white-space:nowrap; }
.price-cell{
  display:flex;
  align-items:center;
  gap:3px;
  justify-content:flex-end;
}
.sheet.curr-after .price-cell{ flex-direction:row-reverse; justify-content:flex-end; }
.price-currency{
  flex:none;
  font-size:11px;
  color:#57657A;
}
.section-total-row{
  text-align:right; font-weight:600; font-size:12.5px; margin-top:4px;
}

.sheet-summary{ margin-top:26px; }
.sheet-summary-head{
  font-family:var(--f-display); font-weight:700; font-size:13.5px;
  text-decoration:underline; margin-bottom:8px;
}
.summary-table td:first-child{ width:70%; }
.summary-table td:last-child{ text-align:right; font-family:var(--f-mono); }
.summary-table tr.total-ht td, .summary-table tr.total-ttc td{
  font-weight:700; background:#EFE9D9;
}
.summary-table tr.total-ttc td{ background:var(--accent); color:#1a0e05; font-size:13px; }

.sheet-footer{
  margin-top:24px; padding-top:12px; border-top:1px solid #C9BFA6;
  font-size:9.5px; color:#5A6779; white-space:pre-line; line-height:1.5;
}

.page-footer{
  margin-top:26px;
  padding-top:8px;
  border-top:1px solid #2B3A4A;
  display:flex;
  justify-content:space-between;
  align-items:baseline;
  gap:16px;
  font-family:var(--f-mono);
  font-size:8.5px;
  color:#5A6779;
}
.page-footer-left{ white-space:nowrap; }
.page-footer-right{ text-align:right; }

/* ============ EDITABLE SECTION CARD (rail-driven, lives in canvas) ============ */
.section-card{
  border:1px solid #2B3A4A;
  padding:14px 16px 16px;
  background:#FBF9F3;
}
.section-card-bar{
  display:flex; align-items:center; gap:8px; margin-bottom:6px;
}
.drag-hint{ color:#9AA6B4; font-size:13px; }
.section-title-input{
  flex:1;
  font-family:var(--f-display); font-weight:700; font-size:14px;
  border:none; border-bottom:1px dashed #2B3A4A; background:transparent;
  padding:4px 2px; color:#1B2430;
}
.section-title-input:focus{ outline:none; border-bottom-color:var(--accent); }
.section-card-actions{ display:flex; gap:2px; }
.section-desc-input{
  width:100%; border:none; background:transparent; resize:vertical; overflow:hidden;
  font-size:11.5px; color:#57657A; padding:2px; margin-bottom:8px; font-family:var(--f-body);
  min-height:2.6em;
}
.section-desc-input:focus{ outline:none; }

.items-table textarea.item-desc{
  width:100%; border:none; background:transparent; resize:vertical; overflow:hidden;
  font-family:var(--f-body); font-size:12px; padding:2px;
}
.items-table input.item-qty{
  width:100%; border:none; background:transparent; text-align:right;
  font-family:var(--f-mono); font-size:12px; padding:2px;
}
.items-table input.item-price{
  flex:1; min-width:0; border:none; background:transparent; text-align:right;
  font-family:var(--f-mono); font-size:12px; padding:2px;
}
.items-table input.item-unit{
  width:100%; border:none; background:transparent; text-align:center;
  font-family:var(--f-body); font-size:12px; padding:2px;
}
.items-table textarea:focus, .items-table input:focus{ outline:1px solid var(--accent); }
.items-table .col-del{ width:26px; padding:2px; }

/* ============ RESPONSIVE ============ */
/* Tablet / narrow desktop: stack rail above canvas, let the page scroll
   normally instead of trapping scroll inside two independent panes. */
@media (max-width: 860px){
  html, body{ height:auto; }
  .app{ display:block; height:auto; }

  .rail{
    border-right:none;
    border-bottom:1px solid var(--line-dim);
    max-height:none;
  }
  .rail-scroll{ overflow-y:visible; padding-bottom:24px; }

  .canvas-wrap{
    overflow-y:visible;
    overflow-x:auto;
    -webkit-overflow-scrolling:touch;
  }
  /* Keep the invoice sheet at a legible working width; the wrap scrolls
     horizontally on narrow screens rather than squashing every column. */
  .sheet{ min-width:640px; }

  .sheet-head-row{ grid-template-columns:1fr; }
  .sheet-head-client{ justify-self:stretch; margin-top:8px; }
}

/* Phone: tighten chrome, enlarge touch targets, single-column rail actions */
@media (max-width: 520px){
  .topbar{ padding:0 14px; }
  .topbar-brand-title{ font-size:15px; }
  .topbar-brand-sub{ display:none; }
  .rail-scroll{ padding:6px 14px 28px; }
  .rail-actions{ padding:12px 14px; gap:8px; }

  .canvas-wrap{ padding:16px 12px 40px; }
  .canvas-toolbar{ flex-wrap:wrap; gap:8px; }
  .canvas-toolbar-hint{ font-size:11px; }

  .sheet{ min-width:580px; padding:26px 22px 20px; }

  /* Bigger tap targets throughout */
  .btn{ padding:11px 12px; font-size:13px; }
  .icon-btn{ padding:6px 9px; font-size:15px; }
  .section-card-actions .icon-btn{ padding:8px 10px; }
  .inp, .tax-label, .tax-rate{ font-size:16px; } /* prevents iOS auto-zoom on focus */
  .tax-rate{ width:64px; }
  .items-table input, .items-table textarea{ font-size:13px; }
  .section-title-input{ font-size:15px; }
}
