:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --ink: #1a2233;
  --muted: #6b7384;
  --line: #e6e9f0;
  --brand: #4f46e5;
  --brand-dark: #4338ca;
  --pro: #0ea05a;
  --pro-dark: #0c8a4d;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16,24,40,.06), 0 6px 24px rgba(16,24,40,.06);
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--card);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
  transition: transform .25s ease;
}
.topbar--hidden { transform: translateY(-100%); }
.brand { font-weight: 800; font-size: 20px; letter-spacing: -.3px; }
.topbar nav { display: flex; align-items: center; gap: 12px; }
.badge { font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 999px; }
.badge-free { background: #eef0f5; color: var(--muted); }
.badge-pro { background: #dcfce7; color: var(--pro-dark); }

/* Cloud sync indicator (Pro) */
.sync { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--muted); }
.sync::before { content: ""; width: 8px; height: 8px; border-radius: 999px; background: currentColor; }
.sync--syncing { color: #b0870b; }
.sync--syncing::before { animation: syncpulse 1s ease-in-out infinite; }
.sync--ok { color: var(--pro-dark); }
.sync--error { color: #c0392b; }
@keyframes syncpulse { 0%,100% { opacity: 1; } 50% { opacity: .25; } }

/* Account chip (logged-in state in the topbar) */
.account-chip { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.account-email { font-weight: 600; color: var(--ink); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-chip .linkbtn { padding: 0; font-size: 12px; }

.btn {
  font: inherit; font-weight: 600; border: none; border-radius: 9px;
  padding: 10px 16px; cursor: pointer; transition: .15s; line-height: 1;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-pro { background: var(--pro); color: #fff; }
.btn-pro:hover { background: var(--pro-dark); }
.btn-ghost { background: #eef0f5; color: var(--ink); }
.btn-ghost:hover { background: #e3e6ee; }
.btn-block { width: 100%; padding: 13px; font-size: 15px; }

.layout {
  display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1fr);
  gap: 28px; max-width: 1280px; margin: 0 auto; padding: 28px 24px 60px;
}

.page-title { font-size: 26px; margin: 0 0 4px; letter-spacing: -.5px; }
.subtitle { color: var(--muted); margin: 0 0 18px; font-size: 14px; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.card h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin: 0 0 14px; }
.field-label { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin: 12px 0 6px; }
.field-label:first-child { margin-top: 0; }
input, textarea, select {
  width: 100%; font: inherit; font-size: 14px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 9px; background: #fbfcfe; color: var(--ink);
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
textarea { resize: vertical; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.combo { display: flex; gap: 6px; }
.combo input { flex: 1; min-width: 0; }
.combo select { flex: 0 0 64px; padding: 10px 6px; }
.opt { font-weight: 400; color: #9aa1b1; font-size: 11px; }
.logo-row { display: flex; gap: 14px; align-items: flex-start; }
.logo-upload { width: 150px; }
.grow { flex: 1; }

.pro-tag { display:inline-block; font-size:10px; font-weight:700; color:var(--pro-dark);
  background:#dcfce7; padding:2px 6px; border-radius:6px; vertical-align:middle; }

#itemsEditor { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.item-row { display: grid; grid-template-columns: 1fr 64px 90px 28px; gap: 8px; align-items: center; }
.item-row input { padding: 8px 10px; }
.item-del { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1; }
.item-del:hover { color: #e5484d; }

.actions { display: flex; gap: 10px; margin-top: 4px; }
.micro { font-size: 12px; color: var(--muted); margin-top: 10px; }
.micro.center { text-align: center; }

/* ===== Preview ===== */
.preview-wrap { position: sticky; top: 80px; align-self: start; }
.preview-bar { display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 10px; }
.preview-tag { display: inline-flex; align-items: center; gap: 7px; font-size: 12px;
  font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--pro);
  box-shadow: 0 0 0 3px rgba(14,160,90,.16); }
.preview-hint { font-size: 12px; color: var(--muted); display: none; }
.invoice-paper {
  background: #fff; border: 1px solid var(--line); border-radius: 10px;
  padding: 38px; box-shadow: var(--shadow); position: relative; min-height: 600px;
  font-size: 13px; color: #2a3142;
}
.inv-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; }
.pv-logo { max-height: 56px; max-width: 180px; margin-bottom: 10px; object-fit: contain; }
.inv-word { font-size: 30px; font-weight: 800; letter-spacing: 2px; color: var(--brand); text-align: right; }
.inv-title-block { text-align: right; }
.pv-strong { font-weight: 700; font-size: 15px; }
.pv-muted { color: var(--muted); white-space: pre-line; line-height: 1.5; margin-top: 2px; }
.pv-label { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); font-weight: 600; }
.inv-meta { display: flex; justify-content: space-between; margin: 28px 0 20px; gap: 20px; }
.inv-dates { text-align: right; line-height: 1.9; }

.inv-table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.inv-table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--muted); border-bottom: 2px solid var(--line); padding: 8px 6px; }
.inv-table td { padding: 9px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.inv-table .r { text-align: right; }

.inv-totals { margin-top: 16px; margin-left: auto; width: 240px; }
.tot-row { display: flex; justify-content: space-between; padding: 6px 0; color: var(--muted); }
.tot-grand { border-top: 2px solid var(--line); margin-top: 4px; padding-top: 10px;
  color: var(--ink); font-weight: 800; font-size: 16px; }
.inv-notes { margin-top: 26px; padding-top: 16px; border-top: 1px solid var(--line);
  color: var(--muted); white-space: pre-line; line-height: 1.6; }
.watermark { margin-top: 28px; text-align: center; font-size: 11px; color: #b6bcca; letter-spacing: .3px; }

/* ===== Modal ===== */
.modal-backdrop { position: fixed; inset: 0; background: rgba(18,22,33,.55);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal { background: #fff; border-radius: 16px; padding: 30px; width: 100%; max-width: 380px;
  position: relative; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-x { position: absolute; top: 14px; right: 16px; background: none; border: none;
  font-size: 26px; color: var(--muted); cursor: pointer; line-height: 1; }
.modal h2 { margin: 0 0 4px; font-size: 22px; }
.modal-sub { color: var(--muted); margin: 0 0 18px; font-size: 14px; }
.features { list-style: none; padding: 0; margin: 0 0 18px; }
.features li { padding: 7px 0; font-size: 14px; }
.price { text-align: center; margin: 6px 0 18px; }
.price .amount { font-size: 40px; font-weight: 800; }
.price .per { color: var(--muted); font-size: 16px; }
.haskey { margin-top: 18px; font-size: 13px; color: var(--muted); }
.haskey summary { cursor: pointer; }
.haskey input { margin: 10px 0; }

/* Auth modal */
.auth-error { color: #c0392b; min-height: 1em; }
.auth-error:empty { display: none; }

/* ===== Extras: link buttons, client pick, pay line, history ===== */
.linkbtn { background: none; border: none; color: var(--brand); font: inherit; font-weight: 600;
  font-size: 13px; cursor: pointer; padding: 8px 0 0; }
.linkbtn:hover { text-decoration: underline; }
.client-pick { margin-bottom: 4px; }

.inv-pay { margin-top: 22px; padding: 12px 14px; background: #eef2ff; border: 1px solid #dfe3ff;
  border-radius: 8px; }
.inv-pay .pv-label { display: block; margin-bottom: 2px; }
.inv-pay a { color: var(--brand); font-weight: 600; word-break: break-all; text-decoration: none; }

.modal-wide { max-width: 520px; }
.hist-list { margin-top: 8px; max-height: 60vh; overflow-y: auto; }
.hist-row { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 4px; border-bottom: 1px solid var(--line); }
.hist-row:last-child { border-bottom: none; }
.hist-num { font-weight: 700; font-size: 14px; }
.hist-client { font-weight: 400; color: var(--muted); }
.hist-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; flex-wrap: wrap; }
.hist-actions .btn { padding: 5px 10px; font-size: 13px; }
.hist-del { color: #e5484d; }
.revision-banner { background: #fff7e0; border: 1px solid #ffd166; border-radius: 6px;
  padding: 8px 12px; font-size: 13px; color: #7a5c00; margin-bottom: 8px; }

/* ===== Site footer (internal links) ===== */
.site-foot { border-top: 1px solid var(--line); margin-top: 8px; padding: 22px 24px; background: var(--card); }
.foot-inner { max-width: 1280px; margin: 0 auto; display: flex; flex-wrap: wrap; gap: 8px 16px;
  align-items: center; justify-content: space-between; font-size: 13px; color: var(--muted); }
.foot-inner nav { display: flex; gap: 16px; }
.foot-inner a { color: var(--brand); text-decoration: none; font-weight: 600; }
.foot-inner a:hover { text-decoration: underline; }

/* ===== Responsive overrides (kept last so they win the cascade over the base rules above) ===== */
@media (max-width: 920px){
  .layout { grid-template-columns: 1fr; gap: 20px; }
  /* Form first, preview flows below it — never covers the inputs or the keyboard */
  .preview-wrap { position: static; top: auto; align-self: auto; }
  .preview-hint { display: inline; }
  .preview-scroll { overflow-y: visible; -webkit-overflow-scrolling: touch;
    border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); background: #fff; }
  .preview-scroll .invoice-paper { border: none; box-shadow: none; min-height: auto; padding: 24px; }
}
@media (max-width: 560px){
  .layout { padding: 18px 14px 48px; gap: 16px; }
  .topbar { padding: 12px 14px; }
  .topbar nav { gap: 8px; }
  .badge-free { display: none; }
  .account-email { max-width: 90px; }
  #logoutAllBtn { display: none; }
  .page-title { font-size: 22px; }
  .card { padding: 15px; }
  .preview-scroll .invoice-paper { padding: 18px; font-size: 12px; }
  .inv-word { font-size: 24px; }
  .inv-meta { margin: 20px 0 16px; }
  .inv-totals { width: 200px; }
  .logo-row { flex-direction: column; gap: 10px; }
  .logo-upload { width: 100%; }
  .actions { flex-wrap: wrap; }
  .actions .btn { flex: 1; }
}
