:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1d2733;
  --muted: #64748b;
  --border: #dde3ea;
  --primary: #1f6feb;
  --primary-dark: #175ac2;
  --danger: #c0392b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

.topnav {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  background: #14213d;
  color: #fff;
  padding: 0 1.5rem;
  height: 52px;
}
.topnav a { color: #cbd5e1; text-decoration: none; font-weight: 500; }
.topnav a:hover { color: #fff; }
.topnav .brand { color: #fff; font-weight: 700; font-size: 1.05rem; margin-right: .5rem; }
.topnav .spacer { flex: 1; }
.topnav .whoami { color: #94a3b8; }

.container { max-width: 1100px; margin: 0 auto; padding: 1.5rem; }

h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
h3 { font-size: .95rem; margin: 1.25rem 0 .5rem; }

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.page-head .sub { color: var(--muted); font-size: .9rem; }
.page-head .sub a { color: var(--primary); text-decoration: none; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.1rem 1.25rem;
  margin-bottom: 1.25rem;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; align-items: start; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

.form label { display: block; margin-bottom: .8rem; font-weight: 600; font-size: .85rem; }
.form input, .form select, .form textarea,
.inline-form input, .inline-form select {
  display: block;
  width: 100%;
  margin-top: .3rem;
  padding: .5rem .6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: #fff;
}
.form textarea { resize: vertical; }
.form .row { display: flex; gap: .8rem; flex-wrap: wrap; }
.form .row label { flex: 1 1 180px; }
.form .check { font-weight: 400; }
.form .check input { display: inline; width: auto; margin-right: .35rem; }
.form.narrow { max-width: 560px; }
.subform { padding: .75rem; background: #f8fafc; border: 1px dashed var(--border); border-radius: 8px; margin-bottom: .8rem; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1rem 0; }

.inline-form { display: flex; gap: .5rem; align-items: center; }
.inline-form input, .inline-form select { margin-top: 0; width: auto; flex: 1; }
.inline { display: inline; }

.btn {
  display: inline-block;
  padding: .5rem .9rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: #f1f5f9; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.small { padding: .3rem .6rem; font-size: .8rem; }

.linklike {
  background: none; border: none; padding: 0;
  color: var(--primary); cursor: pointer; font: inherit;
  text-decoration: underline;
}
.linklike.danger { color: var(--danger); }
.topnav .linklike { color: #cbd5e1; text-decoration: none; }
.topnav .linklike:hover { color: #fff; }

.table { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.table th, .table td { text-align: left; padding: .6rem .8rem; border-bottom: 1px solid var(--border); }
.table th { background: #f8fafc; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.table tr:last-child td { border-bottom: none; }
.table td a { color: var(--primary); text-decoration: none; font-weight: 600; }
.table td a:hover { text-decoration: underline; }
.table.compact th, .table.compact td { padding: .45rem .6rem; font-size: .88rem; }
.empty { color: var(--muted); font-style: italic; }

.badge {
  display: inline-block;
  padding: .12rem .55rem;
  border-radius: 999px;
  font-size: .74rem;
  font-weight: 700;
  vertical-align: middle;
}
.badge.big { font-size: .9rem; padding: .3rem .8rem; }
.status-draft { background: #e2e8f0; color: #475569; }
.status-sent { background: #dbeafe; color: #1d4ed8; }
.status-negotiation { background: #fef3c7; color: #b45309; }
.status-accepted { background: #dcfce7; color: #15803d; }
.status-completed { background: #14532d; color: #ffffff; }
.status-lost { background: #fee2e2; color: #b91c1c; }
.badge.stale { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; margin-left: .35rem; }
.badge.accepted { background: #dcfce7; color: #15803d; }
.badge.generated { background: #eef2ff; color: #4338ca; margin-left: .25rem; }

.filters { display: flex; gap: 1rem; align-items: center; margin-bottom: 1rem; flex-wrap: wrap; }
.chips { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }
.chip {
  padding: .3rem .75rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  font-size: .82rem;
}
.chip:hover { border-color: var(--primary); color: var(--primary); }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.searchbox { display: flex; gap: .5rem; align-items: center; }
.searchbox input[type="search"] {
  padding: .45rem .65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: #fff;
  min-width: 260px;
}
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: #fff; }
.segmented a { padding: .4rem .9rem; text-decoration: none; color: var(--muted); font-weight: 600; font-size: .88rem; }
.segmented a.active { background: var(--primary); color: #fff; }
.filters select { padding: .4rem .6rem; border: 1px solid var(--border); border-radius: 6px; font: inherit; background: #fff; }

.notes, .files { list-style: none; padding: 0; margin: 1rem 0 0; }
.notes li, .files li { padding: .6rem 0; border-top: 1px solid var(--border); }
.notes li.system .note-body { color: var(--muted); font-style: italic; }
.notes .meta, .files .meta { font-size: .78rem; color: var(--muted); margin-bottom: .15rem; }
.note-body { white-space: pre-wrap; }
.files li a { color: var(--primary); text-decoration: none; font-weight: 600; }
.files .meta { display: block; }

.hint { color: var(--muted); font-size: .85rem; margin: .6rem 0 0; }

.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.card-head h2 { margin: 0; }

/* overflow must stay visible here, otherwise the item-search dropdown in the
   last table row is clipped away by the table's rounded-corner clipping */
.lines-table { overflow: visible; }

.lines-table input {
  width: 100%;
  padding: .35rem .45rem;
  border: 1px solid var(--border);
  border-radius: 5px;
  font: inherit;
  font-size: .88rem;
}
.lines-table .w-qty { width: 70px; }
.lines-table .w-unit { width: 70px; }
.lines-table .w-price { width: 110px; }
.lines-table .w-total { width: 120px; }
.lines-table .w-actions { width: 130px; }
.lines-table td.num { text-align: right; white-space: nowrap; }
.lines-table td.actions { white-space: nowrap; }
.lines-table .add-row td { background: #f8fafc; }

.totals { margin: .9rem 0 0 auto; max-width: 300px; }
.totals div { display: flex; justify-content: space-between; padding: .25rem 0; }
.totals span { color: var(--muted); }
.totals .grand { border-top: 1px solid var(--border); margin-top: .25rem; padding-top: .5rem; font-size: 1.05rem; }

.save-template { margin-top: 1rem; padding-top: .9rem; border-top: 1px solid var(--border); max-width: 420px; }

.followup-field { display: flex; align-items: flex-end; gap: .6rem; margin-top: .9rem; max-width: 420px; }
.followup-field label { flex: 1; font-weight: 600; font-size: .85rem; }
.followup-field input {
  display: block;
  width: 100%;
  margin-top: .3rem;
  padding: .5rem .6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
}
.followup-field .saved-slot { padding-bottom: .6rem; white-space: nowrap; }
.followup-cell { color: #b45309; font-weight: 600; }

.req { color: #dc2626; }

.saved-slot { color: #15803d; font-size: .78rem; opacity: 0; transition: opacity .2s; }
.saved-slot.show { opacity: 1; }

.suggest-wrap { position: relative; }
.item-suggest {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 100%;
  max-width: 460px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .15);
  z-index: 100;
  max-height: 220px;
  overflow: auto;
}
.item-suggest-row { padding: .45rem .6rem; cursor: pointer; font-size: .85rem; }
.item-suggest-row:hover { background: #f1f5f9; }

.card-head .sub { color: var(--muted); font-size: .85rem; font-weight: 400; margin-top: .2rem; }
.card-head .actions { display: flex; align-items: center; gap: .75rem; }
details summary { cursor: pointer; color: var(--primary); font-weight: 600; font-size: .88rem; }

.logo-block { margin-bottom: .5rem; }
.logo-preview { display: flex; align-items: center; gap: 1rem; margin-bottom: .75rem; }
.logo-preview img { max-height: 60px; max-width: 220px; border: 1px solid var(--border); border-radius: 6px; padding: .3rem .5rem; background: #fff; }

.flash { padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-weight: 500; }
.flash-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.flash-error { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

.auth-card { max-width: 380px; margin: 8vh auto 0; }
.auth-card h1 { margin-bottom: .75rem; }
.auth-card p { color: var(--muted); margin-top: 0; }

/* --- Mobile --- */
@media (max-width: 740px) {
  .container { padding: .9rem .7rem; }

  .topnav {
    flex-wrap: wrap;
    height: auto;
    min-height: 52px;
    padding: .55rem .8rem;
    gap: .35rem .9rem;
    row-gap: .3rem;
  }
  .topnav .brand { font-size: 1rem; margin-right: .25rem; }
  .topnav .whoami { display: none; }

  h1 { font-size: 1.25rem; }
  .card { padding: .85rem .8rem; }

  /* wide tables swipe sideways instead of being squeezed */
  .table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table th, .table td { white-space: nowrap; }

  /* 16px inputs stop iPhones from auto-zooming on focus */
  input, select, textarea, button { font-size: 16px; }
  .lines-table input { font-size: 16px; min-width: 90px; }
  .lines-table input[name="description"] { min-width: 180px; }

  .searchbox { width: 100%; }
  .searchbox input[type="search"] { min-width: 0; flex: 1; }

  .inline-form { flex-wrap: wrap; }
  .inline-form input[type="file"] { min-width: 0; }
  .totals { max-width: none; }
  .followup-field { max-width: none; }
  .save-template { max-width: none; }
  .page-head .btn { width: 100%; text-align: center; }
  .auth-card { margin-top: 4vh; }
}
