/* ServiceKey - app-specific components layered over main.css tokens. */

/* ==================== TOP BAR MENU ==================== */
.app-menu { position: relative; }

.app-menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 200px;
  padding: var(--space-xs);
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: 0 6px 24px var(--shadow);
  z-index: 200;
}

.app-menu-panel a {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  min-height: 44px;
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: var(--text-sm);
}

.app-menu-panel a:hover { background: var(--bg-hover); color: var(--text-primary); }

.impersonation-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  padding: var(--space-sm);
  background: var(--warning);
  color: #1a1a1a;
  font-size: var(--text-sm);
  font-weight: 600;
}

@media (max-width: 640px) {
  .impersonation-banner { flex-wrap: wrap; text-align: center; }
}

/* ==================== DASHBOARD ==================== */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-md);
  margin-bottom: var(--space-lg);
}

.stat-tile {
  padding: var(--space-lg);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

button.stat-toggle {
  position: relative;
  width: 100%;
  color: var(--text-primary);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
button.stat-toggle:hover { border-color: var(--accent); }
button.stat-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.stat-toggle.is-expanded {
  border-color: var(--accent);
  background: linear-gradient(var(--accent-light), var(--accent-light)), var(--bg-card);
}
.stat-toggle.is-expanded .stat-label,
.stat-toggle.is-expanded .stat-chevron { color: var(--accent); }
.stat-toggle .stat-label,
.stat-toggle .stat-value,
.stat-toggle .stat-sub { display: block; }
.stat-chevron {
  position: absolute;
  top: var(--space-md);
  right: var(--space-md);
  color: var(--text-tertiary);
  transition: transform .15s ease;
}
.stat-toggle.is-expanded .stat-chevron { transform: rotate(180deg); }

.stat-tile.tone-danger { border-color: var(--danger); }
.stat-label { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .06em; color: var(--text-secondary); }
.stat-value { font-family: var(--font-mono); font-variant-numeric: tabular-nums; font-size: 1.75rem; font-weight: 700; margin: var(--space-xs) 0; }
.stat-tile.tone-danger .stat-value { color: var(--danger); }
.stat-sub { font-size: var(--text-xs); color: var(--text-tertiary); }

.stat-tile.skeleton { min-height: 104px; background: linear-gradient(90deg, var(--bg-card), var(--bg-tertiary), var(--bg-card)); background-size: 200% 100%; animation: shimmer 1.4s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

.dash-columns { display: grid; grid-template-columns: 1.4fr 1fr; gap: var(--space-md); align-items: start; }

.dash-breakdown {
  margin-top: calc(-1 * var(--space-md));
  margin-bottom: var(--space-lg);
  box-shadow: inset 0 3px 0 var(--accent);
}
.dash-breakdown[hidden] { display: none; }
.dash-breakdown-header { display: flex; align-items: baseline; justify-content: space-between; gap: var(--space-md); }
.dash-breakdown-header .money { font-size: var(--text-lg); font-weight: 700; }
.breakdown-list { padding: 0 var(--space-lg) var(--space-sm); }
.dash-breakdown-empty { padding: 0 var(--space-lg) var(--space-lg); }

.queue-group + .queue-group { margin-top: var(--space-lg); }
.queue-title { display: flex; align-items: center; gap: var(--space-sm); margin-bottom: var(--space-sm); font-size: var(--text-sm); color: var(--text-secondary); }
.queue-count { padding: 1px var(--space-sm); border-radius: 999px; background: var(--bg-tertiary); font-size: var(--text-xs); }
.queue-list { list-style: none; }
.queue-list li { display: flex; align-items: center; justify-content: space-between; gap: var(--space-md); padding: var(--space-sm) 0; border-bottom: 1px solid var(--border); }
.queue-list li:last-child { border-bottom: none; }
.queue-main { display: flex; flex-direction: column; min-width: 0; }
.queue-sub { font-size: var(--text-xs); color: var(--text-tertiary); }
.queue-viewed { color: var(--text-secondary); }
.queue-meta { display: flex; align-items: center; gap: var(--space-sm); white-space: nowrap; }

.aging { display: flex; flex-direction: column; gap: var(--space-sm); }
.aging-row { display: grid; grid-template-columns: 90px 1fr auto; align-items: center; gap: var(--space-sm); font-size: var(--text-sm); }
.aging-label { color: var(--text-secondary); font-size: var(--text-xs); }
.aging-track { height: 8px; background: var(--bg-tertiary); border-radius: 999px; overflow: hidden; }
.aging-bar { height: 100%; border-radius: 999px; }
.aging-bar.tone-ok { background: var(--success); }
.aging-bar.tone-warn { background: var(--warning); }
.aging-bar.tone-bad { background: var(--danger); }
.aging-value { font-family: var(--font-mono); font-variant-numeric: tabular-nums; font-size: var(--text-xs); }

.activity-list { list-style: none; }
.activity-list li { display: flex; align-items: baseline; gap: var(--space-sm); padding: var(--space-xs) 0; border-bottom: 1px solid var(--border); font-size: var(--text-sm); }
.activity-list li:last-child { border-bottom: none; }
.activity-event { font-weight: 600; }
.activity-time { margin-left: auto; color: var(--text-tertiary); font-size: var(--text-xs); white-space: nowrap; }

/* ==================== TABLES / LISTS ==================== */
.link-button { background: none; border: 0; padding: 0; color: var(--accent); font: inherit; cursor: pointer; text-align: left; }
.link-button:hover { color: var(--accent-hover); text-decoration: underline; }
.row-archived { opacity: .55; }
.checkbox-inline { display: inline-flex; align-items: center; gap: var(--space-sm); font-size: var(--text-sm); color: var(--text-secondary); cursor: pointer; }
.checkbox-inline input { width: auto; min-height: 0; }
.filters { padding: var(--space-md); }

.contact-list { list-style: none; }
.contact-list li { display: flex; align-items: center; justify-content: space-between; gap: var(--space-md); padding: var(--space-sm) 0; border-bottom: 1px solid var(--border); }
.contact-list li:last-child { border-bottom: none; }

/* ==================== DOCUMENT EDITOR ==================== */
.lines-table input[type="text"] { min-height: 38px; padding: var(--space-xs) var(--space-sm); }
/* The description is a textarea that grows with its content (doc-editor.js
   sizes it). It must match the height and padding of the inputs beside it at
   one line, or a simple line item looks like a different kind of field.
   resize: none because the script owns the height; overflow hidden so no
   scrollbar flashes mid-grow. */
.lines-table textarea.line-desc {
  min-height: 38px; padding: var(--space-xs) var(--space-sm);
  width: 100%; resize: none; overflow: hidden;
  font: inherit; line-height: 1.4;
}
/* Top, not middle: a three-line description must not push the qty and price
   fields down to its centre. */
.lines-table td { padding: var(--space-xs) var(--space-sm); vertical-align: top; }
.lines-table .line-total { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

.totals-box { display: flex; justify-content: flex-end; margin-top: var(--space-md); }
.totals-table { border-collapse: collapse; min-width: 260px; }
.totals-table th { padding: var(--space-xs) var(--space-md) var(--space-xs) 0; text-align: right; font-weight: 400; color: var(--text-secondary); font-size: var(--text-sm); }
.totals-table td { padding: var(--space-xs) 0; text-align: right; font-family: var(--font-mono); font-variant-numeric: tabular-nums; min-width: 110px; }
.totals-table tr.grand th, .totals-table tr.grand td { padding-top: var(--space-sm); border-top: 2px solid var(--accent); font-size: var(--text-lg); font-weight: 700; color: var(--text-primary); }

/* Payment stages. Same input sizing as the lines table so the two read as one
   editor; the amount column is derived and therefore not an input. */
.milestones-table input[type="text"] { min-height: 38px; padding: var(--space-xs) var(--space-sm); }
.milestones-table td { padding: var(--space-xs) var(--space-sm); vertical-align: middle; }
.milestones-table .ms-amount { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* Percent-of-contract dialog. The presets wrap rather than scroll: at 44px
   minimum tap targets five buttons do not fit a narrow phone on one row. */
.pct-presets { display: flex; flex-wrap: wrap; gap: var(--space-xs); margin-bottom: var(--space-sm); }
.pct-presets .btn { min-height: 44px; min-width: 44px; }
#pct-readout { justify-content: flex-start; }

.payment-summary { display: flex; gap: var(--space-xl); flex-wrap: wrap; margin-bottom: var(--space-md); }
.payment-summary > div { display: flex; flex-direction: column; }
.payment-summary span { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .05em; }
.payment-summary strong { font-family: var(--font-mono); font-variant-numeric: tabular-nums; font-size: var(--text-xl); }

/* The preview pane behind the frame.

   #525659 is the same ground DocumentPage::chromeCss() paints behind the paper
   in the owner's new-tab print view, so the two previews sit the document on
   one surface. It is mirrored by FRAME_GROUND in doc-editor.js, which is what
   you actually see - an iframe's canvas is opaque and will not let a parent
   background through, so the frame paints its own. This copy exists so there
   is no flash of a mismatched panel while the frame loads; keep the two in
   sync.

   Not a --bg-* token: those run #1a1a1a-#2f2f2f, and the document's ink is
   #111 from documentCss(), shared with print and /sign and not themeable here
   without forking the document's appearance. See the note on FRAME_PAPER. */
.doc-preview { background: #525659; border-radius: var(--radius-md); overflow: hidden; }
/* The frozen document carries its own print CSS; an iframe keeps that from
   colliding with the app shell in either direction.

   The ground is NOT set here. An iframe's canvas is opaque white by default,
   so a background on this element (or `transparent` on the frame's html/body)
   does not decide what the document is read against - the frame paints its own
   ground from FRAME_CSS in doc-editor.js. This stays transparent only so the
   element itself adds no second layer. */
.doc-frame { width: 100%; border: 0; display: block; background: transparent; min-height: 300px; }

/* Sortable column headers. The 44px min-height keeps them tappable on a phone,
   matching the tap-target rule the rest of the app follows. */
.data-table th.sortable { cursor: pointer; user-select: none; min-height: 44px; }
.data-table th.sortable:hover { color: var(--text-primary); }
.data-table th.sortable:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.data-table th.sorted { color: var(--text-primary); }
.sort-arrow { display: inline-block; width: 1em; color: var(--accent); }

/* The filters card's button row lines up with the inputs beside it. */
.filter-actions { display: flex; align-items: flex-end; gap: var(--space-sm); flex-wrap: wrap; }

/* SMTP debug transcript in the admin test dialog. Monospace and scrollable:
   the lines are long, and wrapping them makes the reply codes hard to find. */
.smtp-transcript {
  max-height: 320px; overflow: auto;
  background: var(--bg-tertiary, #1f1f1f);
  border: 1px solid var(--border, #3f3f3f);
  border-radius: var(--radius-sm, 4px);
  padding: var(--space-sm); margin-top: var(--space-sm);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.5; white-space: pre; tab-size: 2;
  color: var(--text-secondary);
}

.banner { padding: var(--space-md); border-radius: var(--radius-md); margin-bottom: var(--space-md); font-size: var(--text-sm); }
.banner-void { background: rgba(90,90,90,.2); border-left: 3px solid var(--status-void); color: var(--text-secondary); }
/* Revision-chain links: informational, not a warning - a superseded document is
   a normal part of the lifecycle, not an error state. */
.banner-info { background: rgba(0,147,218,.12); border-left: 3px solid var(--accent); color: var(--text-secondary); }
.banner-info a { color: var(--accent); }

/* ==================== MODAL ==================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-md);
  background: rgba(0,0,0,.6);
  /* Deliberately NOT scrollable. The backdrop is a plain fixed region, so a
     drag that auto-scrolls while selecting text cannot move the element under
     the cursor and turn the release into something that reads as a backdrop
     click. .modal-body owns the scrolling instead. */
  overflow: hidden;
}

/* Modals stack (a discard prompt opens over the form it is asking about).
   Every overlay is fixed and appended to <body>, so DOM order alone puts the
   newest on top - the shared z-index needs no per-layer bookkeeping.
   A stacked overlay drops its own dim, because two at .6 compound to near
   black and the form underneath stops being readable. */
.modal-overlay ~ .modal-overlay { background: rgba(0,0,0,.25); }

.modal {
  width: 100%;
  max-width: 520px;
  max-height: calc(100vh - 2 * var(--space-md));
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 12px 48px var(--shadow);
}

.modal-wide { max-width: 760px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; gap: var(--space-md); padding: var(--space-lg); border-bottom: 1px solid var(--border); }
.modal-header h2 { margin: 0; font-size: var(--text-lg); }
.modal-close { width: 44px; height: 44px; border: 0; background: none; color: var(--text-secondary); cursor: pointer; border-radius: var(--radius-sm); }
.modal-close:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.modal-body { padding: var(--space-lg); overflow-y: auto; }
.modal-footer { display: flex; justify-content: flex-end; gap: var(--space-sm); padding: var(--space-lg); border-top: 1px solid var(--border); }
.modal-sep { border: 0; border-top: 1px solid var(--border); margin: var(--space-lg) 0; }
body.modal-open { overflow: hidden; }

/* ==================== LOGIN ==================== */
.login-error { padding: var(--space-md); margin-bottom: var(--space-md); border-radius: var(--radius-md); background: rgba(239,68,68,.15); color: var(--danger); font-size: var(--text-sm); }
.login-notice { text-align: center; }
.login-notice .btn { margin-top: var(--space-md); }
.login-sent { text-align: center; padding: var(--space-lg) 0; }
.login-sent i { font-size: 2.5rem; color: var(--success); margin-bottom: var(--space-md); display: block; }

/* ==================== RESPONSIVE ==================== */
@media (max-width: 900px) {
  .dash-columns { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .payment-summary { gap: var(--space-lg); }
  .modal { max-width: none; }
  .modal-footer { flex-direction: column-reverse; }
  .modal-footer .btn { width: 100%; }
  /* Line items cannot usefully compress below their columns; scroll instead of
     crushing the description field to one word per line. */
  .lines-table { min-width: 560px; }
}

/* ---------- Contract editor toolbar ---------- */
/* Sits directly above #d-body. The textarea stays the source of truth - this
   only writes tags into it, so what the owner sees typed is what the server's
   allowlist sanitizer will keep. */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip: rect(0 0 0 0); clip-path: inset(50%);
  overflow: hidden; white-space: nowrap;
}

.contract-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px;
  padding: 8px;
  border: 1px solid var(--border); border-bottom: 0;
  border-radius: 6px 6px 0 0;
  background: var(--bg-tertiary, #2a2a2a);
}
.contract-toolbar + textarea {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}
.contract-toolbar-group { display: flex; align-items: center; gap: 6px; }

.contract-tool {
  min-width: 44px; min-height: 44px;
  padding: 0 10px;
  font-weight: 600;
}
.contract-tool[data-tag="em"] { font-style: italic; }
.contract-tool[data-tag="h3"] { font-size: 12px; }

.contract-merge-select {
  min-height: 44px;
  padding: 0 10px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg-secondary, #222); color: var(--text-primary, #e5e5e5);
  font-size: 13px;
}

@media (max-width: 560px) {
  .stat-grid { grid-template-columns: 1fr; }
  .breakdown-list li { align-items: flex-start; flex-direction: column; }
  .breakdown-list .queue-main,
  .breakdown-list .queue-meta { width: 100%; }
  .breakdown-list .queue-meta { justify-content: space-between; }
  .contract-toolbar { gap: 8px; }
  .contract-merge-select { width: 100%; }
}

/* ---------- Signature detail (owner's view of a signed contract) ---------- */
.signature-detail { display: flex; flex-direction: column; gap: 10px; }
.sig-row {
  display: grid; grid-template-columns: 140px 1fr; gap: 12px;
  align-items: start;
}
.sig-row > span:last-child { word-break: break-word; }
@media (max-width: 560px) {
  .sig-row { grid-template-columns: 1fr; gap: 2px; }
}

/* ---------- Document attachments ---------- */
.attachment-list { display: flex; flex-direction: column; gap: 8px; }
.attachment-row {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.attachment-row:last-child { border-bottom: none; }
/* The filename takes the free space so the Remove button sits hard right. */
.attachment-row > a { flex: 1 1 12rem; word-break: break-word; }
/* 44px minimum tap target, as everywhere else. */
.attachment-row .btn-sm { min-height: 44px; min-width: 44px; }

/* Marks the signature scan, which is evidence rather than an ordinary file
   and has no Remove button beside it. */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 3px;
  font-size: 0.75rem; line-height: 1.6;
  border: 1px solid var(--accent); color: var(--accent);
  white-space: nowrap;
}
.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.template-grid > button.card {
  appearance: none;
  width: 100%;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.template-grid > button.card:hover,
.template-grid > button.card:focus-visible {
  border-color: var(--accent, #0093da);
}

.template-grid > button.card span { display: block; margin-top: .4rem; }

/* Template cards are a compact index, not a print preview. Keep unusually
   large source images from expanding the entire card; the edit dialog still
   previews the template's configured document width. */
.template-summary .template-logo {
  display: block;
  width: auto;
  height: auto;
  max-width: 160px;
  max-height: 72px;
  object-fit: contain;
  margin-bottom: var(--space-sm);
}

/* ---- Internal notes ---------------------------------------------
 *
 * The owner's private working notes. Styled deliberately UNLIKE the
 * client-facing Notes field beside it: a muted card and an explicit
 * "Not shown to the client" pill, because the two fields sit one apart
 * in the editor and mean opposite things. The visual difference is the
 * only thing stopping someone typing "chasing payment, client is
 * dodging us" into the field that gets printed.
 */
.card-internal {
  border-style: dashed;
  background: var(--surface-muted, rgba(127,127,127,.05));
}

.card-internal .card-header h2 { font-size: var(--text-base); }

.pill {
  display: inline-block;
  padding: .1rem .5rem;
  border-radius: 999px;
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
  vertical-align: middle;
}

.pill-muted {
  background: var(--surface-alt, rgba(127,127,127,.15));
  color: var(--text-tertiary);
  border: 1px solid var(--border);
}

/* The list indicator. A dot, never the note text - the list is the one
 * screen someone might read over the owner's shoulder. */
.internal-note-dot {
  margin-left: .35rem;
  font-size: .75em;
  color: var(--text-tertiary);
  vertical-align: middle;
}
