/* ============================================================
   Invoice Generator — screen + print styles
   ============================================================ */

:root {
  --bg:        #12141a;
  --bg-2:      #191c24;
  --bg-3:      #21252f;
  --line:      #2d323e;
  --text:      #e6e8ee;
  --muted:     #939aab;
  --accent:    #5b8cff;
  --accent-2:  #3d6fe0;
  --danger:    #e05561;
  --radius:    8px;

  /* invoice paper */
  --ink:       #1a1d24;
  --ink-soft:  #5d6472;
  --rule:      #dfe3ea;
  --brand:     #2f4a8a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", system-ui, -apple-system, sans-serif;
}

.app {
  display: grid;
  grid-template-columns: minmax(420px, 560px) 1fr;
  height: 100%;
}

/* ── Editor column ───────────────────────────────────────── */

.editor {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding-bottom: 60px;
}

.editor-head {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
}

.editor-head h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: .2px;
}

.tabs {
  display: flex;
  gap: 2px;
  padding: 12px 20px 0;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 57px;
  background: var(--bg-2);
  z-index: 4;
}

.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 14px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--text); border-bottom-color: var(--accent); }

.panel { display: none; padding: 18px 20px; }
.panel.is-active { display: block; }

.group {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px 16px;
  margin-bottom: 16px;
}

.group-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .8px;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 12px;
}
.group-title .hint { text-transform: none; letter-spacing: 0; font-weight: 400; margin-left: 6px; }

label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 10px;
}
label.inline { display: inline-block; margin-bottom: 0; }

input, select, textarea {
  width: 100%;
  margin-top: 4px;
  padding: 7px 9px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  font-size: 13px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(91,140,255,.18);
}
input[type="file"] { padding: 5px; background: transparent; border: 0; }
input::placeholder, textarea::placeholder { color: #5c6373; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 12px; }

.stepper { display: flex; margin-top: 4px; }
.stepper input { margin: 0; border-radius: 0; text-align: center; }
.stepper button {
  width: 32px; flex: none;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 16px; line-height: 1;
  cursor: pointer;
}
.stepper button:first-child { border-radius: 6px 0 0 6px; border-right: 0; }
.stepper button:last-child  { border-radius: 0 6px 6px 0; border-left: 0; }
.stepper button:hover { background: var(--bg-3); }

.prefixed { display: flex; align-items: stretch; margin-top: 4px; }
.prefixed span {
  display: flex; align-items: center;
  padding: 0 9px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-right: 0;
  border-radius: 6px 0 0 6px;
  color: var(--muted);
  font-size: 13px;
}
.prefixed input { margin: 0; border-radius: 0 6px 6px 0; }

.fill-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 4px;
}
.fill-row input { width: 90px; }

.hint { font-size: 11.5px; color: var(--muted); }

/* ── Week rows ───────────────────────────────────────────── */

.week-head, .week-row {
  display: grid;
  grid-template-columns: 1fr 78px 96px 26px;
  gap: 8px;
  align-items: center;
}
.week-head {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .6px;
  color: var(--muted);
  padding: 0 0 6px;
}
.week-head span:nth-child(2),
.week-head span:nth-child(3) { text-align: right; }

.week-row { margin-bottom: 6px; }
.week-row input { margin-top: 0; }
.week-row .hrs { text-align: right; }
.week-row .amt {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 13px;
  padding-right: 2px;
}
.week-row .dates {
  display: block;
  font-size: 10.5px;
  color: #6b7285;
  margin-top: 2px;
}
.week-row .reset {
  background: transparent; border: 0; color: #5c6373;
  cursor: pointer; font-size: 15px; line-height: 1; padding: 0;
}
.week-row .reset:hover { color: var(--text); }

.week-total {
  display: flex;
  justify-content: space-between;
  border-top: 1px solid var(--line);
  margin-top: 10px;
  padding-top: 10px;
  font-size: 13px;
  color: var(--muted);
}
.week-total strong { color: var(--text); font-variant-numeric: tabular-nums; }

/* ── Extra line items ────────────────────────────────────── */

.extra-row {
  display: grid;
  grid-template-columns: 1fr 110px 26px;
  gap: 8px;
  margin-bottom: 6px;
}
.extra-row input { margin-top: 0; }
.extra-row button {
  background: transparent; border: 0; color: #5c6373;
  cursor: pointer; font-size: 16px; line-height: 1;
}
.extra-row button:hover { color: var(--danger); }

/* ── Clients ─────────────────────────────────────────────── */

.client-picker { display: flex; gap: 8px; margin-bottom: 14px; }
.client-picker select { margin-top: 0; }

/* ── History ─────────────────────────────────────────────── */

.history-list { display: flex; flex-direction: column; gap: 8px; }

.history-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
}
.history-item .num { font-weight: 600; }
.history-item .meta { font-size: 11.5px; color: var(--muted); }
.history-item .tot {
  grid-row: 1 / span 2;
  grid-column: 2;
  align-self: center;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.history-item .row-actions { grid-column: 1 / -1; display: flex; gap: 6px; margin-top: 6px; }
.history-empty { color: var(--muted); font-size: 13px; }

/* ── Buttons ─────────────────────────────────────────────── */

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 6px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-3); border-color: #3a4152; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn.small { padding: 5px 10px; font-size: 12.5px; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #45303a; }
.btn.danger:hover { background: #2a1c22; }

.actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.save-state { font-size: 12px; color: var(--muted); }

/* ── Preview column ──────────────────────────────────────── */

.preview {
  overflow-y: auto;
  padding: 28px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background:
    radial-gradient(circle at 30% 0%, #1b1f29 0%, var(--bg) 60%);
}

.paper {
  width: 210mm;
  min-height: 297mm;
  padding: 18mm 16mm;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 10px 40px rgba(0,0,0,.45);
  font-size: 11.5pt;
  line-height: 1.45;
}

/* ── Invoice document ────────────────────────────────────── */

.inv-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  padding-bottom: 14px;
  border-bottom: 2px solid var(--brand);
}
.inv-logo { max-width: 190px; max-height: 80px; display: block; margin-bottom: 8px; }
.inv-from .name { font-size: 15pt; font-weight: 700; letter-spacing: .2px; }
.inv-from .lines { color: var(--ink-soft); font-size: 9.5pt; white-space: pre-line; margin-top: 3px; }

.inv-title {
  text-align: right;
  flex: none;
}
.inv-title h2 {
  margin: 0;
  font-size: 24pt;
  font-weight: 300;
  letter-spacing: 3px;
  color: var(--brand);
  text-transform: uppercase;
}
.inv-title .num { font-size: 10.5pt; color: var(--ink-soft); margin-top: 2px; }

.inv-mid {
  display: flex;
  justify-content: space-between;
  gap: 30px;
  margin: 18px 0 22px;
}
.inv-billto .label,
.inv-facts .label {
  font-size: 8pt;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin-bottom: 4px;
}
.inv-billto .who { font-weight: 700; }
.inv-billto .lines { color: var(--ink-soft); font-size: 9.5pt; white-space: pre-line; }

.inv-facts { flex: none; min-width: 210px; }
.inv-facts table { border-collapse: collapse; width: 100%; font-size: 10pt; }
.inv-facts td { padding: 2px 0; }
.inv-facts td:first-child { color: var(--ink-soft); padding-right: 16px; }
.inv-facts td:last-child { text-align: right; font-weight: 600; }
.inv-facts .due td:last-child { color: var(--brand); }

table.items {
  width: 100%;
  border-collapse: collapse;
  font-size: 10pt;
}
table.items thead th {
  background: #f2f5fa;
  color: var(--ink-soft);
  font-size: 8pt;
  letter-spacing: 1px;
  text-transform: uppercase;
  text-align: right;
  padding: 8px 10px;
  border-bottom: 1px solid var(--rule);
}
table.items thead th:first-child { text-align: left; }
table.items tbody td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--rule);
  text-align: right;
  font-variant-numeric: tabular-nums;
}
table.items tbody td:first-child { text-align: left; font-variant-numeric: normal; }
table.items .sub { display: block; font-size: 8.5pt; color: var(--ink-soft); }
table.items tfoot td { padding: 5px 10px; text-align: right; font-size: 10pt; }
table.items tfoot td:first-child { color: var(--ink-soft); }
table.items tfoot tr.grand td {
  border-top: 2px solid var(--brand);
  padding-top: 9px;
  font-size: 13pt;
  font-weight: 700;
}
table.items tfoot tr.grand td:first-child { color: var(--ink); }

.inv-summary-wrap { display: flex; justify-content: flex-end; }
.inv-summary { min-width: 300px; }
.inv-summary table { width: 100%; border-collapse: collapse; }

.inv-bottom { margin-top: 26px; display: flex; gap: 34px; }
.inv-bottom .block { flex: 1; }
.inv-bottom .label {
  font-size: 8pt; letter-spacing: 1.2px; text-transform: uppercase;
  color: var(--ink-soft); margin-bottom: 4px;
}
.inv-bottom .body { font-size: 9.5pt; white-space: pre-line; color: #333842; }

.inv-foot {
  margin-top: 30px;
  padding-top: 10px;
  border-top: 1px solid var(--rule);
  text-align: center;
  font-size: 8.5pt;
  color: var(--ink-soft);
}

/* ── Ruled layout ────────────────────────────────────────── */
/* The classic bordered contractor invoice: teal headings, a full
   grid of ruled rows, plain numbers, no currency symbols.        */

.paper.layout-ruled {
  --teal: #1f4e5f;
  --grid: #a9c0cb;
  font-family: "Gill Sans MT", "Gill Sans", Calibri, "Segoe UI", sans-serif;
  font-size: 11pt;
  color: #16181c;
}

.rl-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; }
.rl-logo { max-width: 170px; max-height: 60px; display: block; margin-bottom: 6px; }
.rl-name { font-size: 17pt; font-weight: 700; color: var(--teal); letter-spacing: .2px; }
.rl-lines { line-height: 1.35; }

.rl-title { text-align: right; flex: none; }
.rl-title h2 {
  margin: 0 0 26px;
  font-size: 27pt;
  font-weight: 700;
  color: var(--teal);
  letter-spacing: .5px;
}
.rl-meta { font-weight: 700; color: var(--teal); font-size: 10.5pt; }

.rl-parties { display: flex; justify-content: space-between; gap: 30px; margin: 30px 0 34px; }
.rl-for { text-align: right; }
.rl-label { font-weight: 700; color: var(--teal); margin-bottom: 6px; font-size: 10.5pt; }

table.rl-table { width: 100%; border-collapse: collapse; font-size: 10.5pt; }

table.rl-table th {
  color: var(--teal);
  font-weight: 700;
  font-size: 10pt;
  padding: 2px 6px 4px;
  border-bottom: 1px solid var(--grid);
  vertical-align: bottom;
}
table.rl-table td {
  padding: 3px 6px;
  border-bottom: 1px solid var(--grid);
  border-left: 1px solid var(--grid);
  height: 1.55em;
}
table.rl-table td:first-child { border-left: 0; }

table.rl-table .c-desc   { text-align: left; }
table.rl-table .c-date   { text-align: center; width: 27%; }
table.rl-table .c-hours  { text-align: center; width: 12%; }
table.rl-table .c-rate   { text-align: center; width: 13%; }
table.rl-table .c-amount { text-align: center; width: 14%; }
table.rl-table th.c-desc { text-align: left; }

/* The total row floats free of the grid apart from its two dividers. */
table.rl-table tfoot td { border-bottom: 0; border-left: 0; padding-top: 6px; }
table.rl-table tfoot .c-blank { border-left: 0; }
table.rl-table tfoot .c-hours { border-left: 1px solid var(--grid); }
table.rl-table tfoot .c-total {
  border-left: 1px solid var(--grid);
  text-align: right;
  font-weight: 700;
  color: var(--teal);
}
table.rl-table tfoot .c-amount { border-left: 1px solid var(--grid); font-weight: 700; }

.rl-foot { margin-top: 18px; line-height: 1.45; }

/* ── Print ───────────────────────────────────────────────── */

@page { size: A4; margin: 12mm; }

@media print {
  html, body { height: auto; background: #fff; }
  .app { display: block; height: auto; }
  .editor, .view-switch { display: none !important; }
  /* !important so the phone layout's view switching can't hide the invoice */
  .preview { display: block !important; padding: 0; overflow: visible; background: #fff; }
  .paper {
    width: auto;
    min-height: 0;
    padding: 0;
    box-shadow: none;
    font-size: 10.5pt;
  }
  table.items thead th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  table.items tbody tr { page-break-inside: avoid; }
  .inv-bottom, .inv-summary-wrap { page-break-inside: avoid; }
}

/* ── Narrow screens ──────────────────────────────────────── */

/* The edit/preview switch only exists on phone-sized screens. */
.view-switch { display: none; }

@media (max-width: 1100px) and (min-width: 861px) {
  .app { grid-template-columns: 1fr; height: auto; }
  .editor { border-right: 0; border-bottom: 1px solid var(--line); overflow: visible; }
  .preview { padding: 16px; }
  .paper { width: 100%; min-height: 0; padding: 24px; }
}

@media (max-width: 860px) {
  html, body { height: auto; }

  .app { display: block; height: auto; }

  .editor, .preview { display: none; }
  body[data-view="edit"] .editor { display: block; }
  body[data-view="preview"] .preview { display: flex; }

  .editor {
    border-right: 0;
    overflow: visible;
    padding-bottom: calc(84px + env(safe-area-inset-bottom));
  }
  .editor-head { padding: 12px 14px calc(12px + env(safe-area-inset-top)); padding-top: max(12px, env(safe-area-inset-top)); }
  .tabs { top: 53px; padding: 10px 14px 0; overflow-x: auto; }
  .tab { padding: 8px 11px; }
  .panel { padding: 14px; }
  .group { padding: 12px 12px 14px; }

  /* Stacking every field full-width beats cramped two-up columns. */
  .grid-2, .grid-3 { grid-template-columns: 1fr; }

  /* Comfortable tap targets, and 16px stops iOS zooming on focus. */
  input, select, textarea { font-size: 16px; padding: 10px; }
  .stepper button { width: 44px; font-size: 20px; }

  .week-head, .week-row { grid-template-columns: 1fr 72px 26px; }
  .week-head span:nth-child(3), .week-row .amt { display: none; }
  .extra-row { grid-template-columns: 1fr 100px 26px; }

  .preview {
    padding: 12px 12px calc(84px + env(safe-area-inset-bottom));
    min-height: 100vh;
  }
  .paper {
    width: 100%;
    min-height: 0;
    padding: 18px 16px;
    font-size: 9.5pt;
    box-shadow: 0 4px 20px rgba(0,0,0,.4);
  }
  .inv-top, .inv-mid, .inv-bottom { flex-direction: column; gap: 14px; }
  .inv-title { text-align: left; }
  .inv-title h2 { font-size: 20pt; }
  .inv-facts { min-width: 0; }
  .inv-summary { min-width: 0; width: 100%; }
  .inv-summary-wrap { display: block; }
  table.items thead th, table.items tbody td { padding: 6px 5px; }

  .view-switch {
    display: flex;
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    z-index: 20;
    background: var(--bg-3);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 4px;
    box-shadow: 0 6px 24px rgba(0,0,0,.5);
  }
  .view-switch button {
    flex: 1;
    appearance: none;
    border: 0;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-weight: 600;
    padding: 10px;
    border-radius: 999px;
    cursor: pointer;
  }
  .view-switch button.is-active { background: var(--accent); color: #fff; }
}
