:root {
  --navy: #17375e;
  --navy-2: #1f4a7a;
  --navy-soft: #e8eef6;
  --orange: #f29d1f;
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1d2733;
  --muted: #6a7686;
  --line: #dde3ea;
  --green: #1e8a5a;
  --green-soft: #e3f4ec;
  --red: #c0392b;
  --red-soft: #fbe8e6;
  --amber: #b7791f;
  --amber-soft: #fdf3e1;
  --blue: #2c6cb0;
  --blue-soft: #e5effa;
  --gray-soft: #eef1f4;
  --radius: 10px;
  --font: "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); font-size: 14px; color: var(--text); background: var(--bg); }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--navy); color: #fff; padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; }
.brand { padding: 4px 10px 18px; border-bottom: 1px solid rgba(255,255,255,.12); margin-bottom: 12px; }
.brand b { display: block; font-size: 17px; letter-spacing: .3px; }
.brand span { font-size: 12px; opacity: .7; }
.brand .dot { color: var(--orange); }
.nav a { display: flex; align-items: center; gap: 10px; color: #dbe5f1; padding: 9px 12px; border-radius: 8px; margin-bottom: 2px; font-size: 14px; }
.nav a:hover { background: rgba(255,255,255,.08); text-decoration: none; }
.nav a.active { background: rgba(255,255,255,.16); color: #fff; font-weight: 600; }
.nav .ico { width: 18px; text-align: center; opacity: .9; }
.sidebar .publish { margin-top: auto; padding: 12px 10px; border-top: 1px solid rgba(255,255,255,.12); }
.sidebar .publish .btn { width: 100%; justify-content: center; background: var(--orange); border-color: var(--orange); color: #fff; }
.sidebar .publish .pub-state { font-size: 11px; opacity: .75; margin-top: 6px; line-height: 1.4; }
.sidebar .publish + .foot { margin-top: 8px; }
.sidebar .foot { margin-top: auto; font-size: 11px; opacity: .55; padding: 0 10px; }
.main { padding: 22px 28px 60px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .spacer { flex: 1; }
.sub { color: var(--muted); font-size: 13px; }

/* ---------- elements ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-head .spacer { flex: 1; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.btn { font: inherit; border: 1px solid var(--line); background: #fff; color: var(--text); padding: 7px 14px; border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.btn:hover { border-color: #b9c4d0; background: #f8fafc; }
.btn.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn.primary:hover { background: var(--navy-2); }
.btn.accent { background: var(--orange); border-color: var(--orange); color: #fff; }
.btn.danger { color: var(--red); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: inherit; border: 1px solid var(--line); border-radius: 7px; padding: 7px 10px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #b8cfe9; border-color: var(--blue); }
textarea { resize: vertical; min-height: 64px; line-height: 1.45; }
label.f { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field { margin-bottom: 12px; }
.row { display: flex; gap: 10px; align-items: center; }
.row > * { min-width: 0; }
.chk { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); }
.chk input { width: auto; }

table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-weight: 600; font-size: 12px; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.list td { padding: 9px 10px; border-bottom: 1px solid #edf0f3; vertical-align: top; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: #f7f9fc; }
table.list .num, .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.list tfoot td { font-weight: 600; border-top: 2px solid var(--line); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.st-draft { background: var(--gray-soft); color: #4e5a68; }
.st-sent { background: var(--blue-soft); color: var(--blue); }
.st-responded { background: var(--amber-soft); color: var(--amber); }
.st-won { background: var(--green-soft); color: var(--green); }
.st-lost { background: var(--red-soft); color: var(--red); }
.st-no_response { background: #f3e8fb; color: #7d3c98; }
.tag { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 4px; background: var(--navy-soft); color: var(--navy); }
.tag.dept { background: #fdf0dc; color: #9a5b00; }
.tag.bill-onetime { background: #eef1f4; color: #4e5a68; }
.tag.bill-monthly { background: #e3f4ec; color: #1e8a5a; }
.tag.bill-yearly { background: #f3e8fb; color: #7d3c98; }

.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-size: 24px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .s { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.navy { background: var(--navy); color: #fff; border-color: var(--navy); }
.kpi.navy .k, .kpi.navy .s { color: #c9d6e6; }

.bar-legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 8px; flex-wrap: wrap; }
.bar-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.stack { display: flex; height: 26px; border-radius: 6px; overflow: hidden; background: var(--gray-soft); }
.stack > div { display: flex; align-items: center; justify-content: center; color: #fff; font-size: 12px; font-weight: 600; min-width: 0; }

.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar input, .toolbar select { width: auto; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--navy); border-bottom-color: var(--navy); font-weight: 600; }
.tabs a:hover { text-decoration: none; color: var(--navy); }

/* ---------- editor ---------- */
.editor { display: grid; grid-template-columns: minmax(420px, 46%) 1fr; gap: 18px; align-items: start; }
.editor .preview-wrap { position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow: auto; background: #d9dee5; border-radius: var(--radius); padding: 16px; }
.editor .preview-wrap .doc { zoom: .72; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,.15); }
.sec-item { border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 10px; background: #fbfcfd; }
.sec-item .sec-top { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.sec-item .sec-top input { font-weight: 600; }
.sec-item .sec-top select { width: 180px; flex: none; }
.sec-item textarea { font-size: 13px; }
.fee-row { display: grid; grid-template-columns: 42px 1fr 120px 60px 110px 28px; gap: 6px; align-items: start; margin-bottom: 6px; }
.fee-row .opt { grid-column: 2 / -1; display: flex; gap: 10px; align-items: center; }
.fee-row .opt input[type=text] { flex: 1; }
.fee-head { font-size: 11px; color: var(--muted); }
.totals { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; padding: 8px 10px; background: var(--navy-soft); border-radius: 8px; margin-top: 6px; }
.totals b { font-variant-numeric: tabular-nums; }
.save-state { font-size: 12px; color: var(--muted); }

/* ---------- timeline ---------- */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 14px 22px; }
.timeline li::before { content: ""; position: absolute; left: 5px; top: 6px; width: 9px; height: 9px; border-radius: 50%; background: var(--navy); }
.timeline li::after { content: ""; position: absolute; left: 9px; top: 18px; bottom: 0; width: 1px; background: var(--line); }
.timeline li:last-child::after { display: none; }
.timeline .d { font-size: 12px; color: var(--muted); }
.timeline .x { float: right; opacity: 0; }
.timeline li:hover .x { opacity: 1; }

/* ---------- modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15,30,50,.45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; overflow: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 620px; padding: 20px 22px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal h2 { margin-bottom: 14px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.toast { position: fixed; bottom: 22px; right: 22px; background: var(--navy); color: #fff; padding: 10px 16px; border-radius: 8px; z-index: 60; box-shadow: 0 4px 16px rgba(0,0,0,.2); }
.toast.err { background: var(--red); }

.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.tpl { border: 2px solid var(--line); border-radius: 10px; padding: 14px; cursor: pointer; background: #fff; }
.tpl:hover { border-color: #b9c4d0; }
.tpl.sel { border-color: var(--navy); background: var(--navy-soft); }
.tpl b { display: block; margin-bottom: 4px; }
.tpl span { font-size: 12px; color: var(--muted); }

/* ================= Proposal document ================= */
.doc { width: 210mm; min-height: 297mm; background: #fff; color: #1b1b1b; padding: 16mm 17mm 18mm; font-family: "Segoe UI", "Leelawadee UI", Tahoma, sans-serif; font-size: 10.5pt; line-height: 1.5; }
.doc .d-group { text-align: right; font-size: 9pt; color: #44546a; margin-bottom: 6mm; }
.doc .d-provider-head { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 1px solid #2e75b6; padding-bottom: 4mm; margin-bottom: 6mm; }
.doc .d-provider-head .addr { text-align: right; font-size: 8.5pt; color: #333; white-space: pre-line; }
.doc .d-logos { display: flex; justify-content: space-around; align-items: center; margin: 0 0 6mm; gap: 10mm; }
.doc .d-logos img { max-height: 24mm; max-width: 70mm; object-fit: contain; }
.doc .d-title { text-align: center; color: #1f3864; font-size: 17pt; font-weight: 400; margin: 0 0 1mm; padding-bottom: 3mm; border-bottom: 1.5px solid #2e75b6; }
.doc .d-subtitle { text-align: center; color: #1f3864; font-weight: 700; font-size: 12pt; margin: 3mm 0 2mm; }
.doc .d-number { text-align: center; font-size: 9pt; color: #666; margin-bottom: 3mm; }
.doc h2.d-h { color: #10284a; font-size: 13pt; font-weight: 700; margin: 6mm 0 2mm; break-after: avoid; }
.doc h3.d-ctitle { text-align: center; color: #1f3864; font-size: 12pt; font-weight: 700; margin: 5mm 0 1.5mm; }
.doc p { margin: 0 0 2.5mm; text-align: justify; }
.doc ul, .doc ol { margin: 0 0 2.5mm; padding-left: 7mm; }
.doc li { margin-bottom: 1mm; }
.doc .d-foot { font-weight: 700; margin: 1mm 0 2.5mm; }
.doc table.d-t { width: 100%; border-collapse: collapse; margin: 1mm 0 3mm; font-size: 10pt; break-inside: auto; }
.doc table.d-t th { background: #17375e; color: #fff; text-align: left; font-weight: 700; padding: 2.2mm 2.5mm; border: 1px solid #d0d7e0; }
.doc table.d-t td { padding: 2mm 2.5mm; border: 1px solid #d0d7e0; vertical-align: middle; white-space: pre-line; }
.doc table.d-t tr:nth-child(even) td { background: #f2f5f9; }
.doc table.d-t td.n, .doc table.d-t th.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.doc table.d-t tr.tot td { font-weight: 700; background: #fff; }
.doc table.d-t tr.hl td { color: #10284a; font-weight: 700; }
.doc table.d-t td.k { width: 38%; }
.doc table.d-t tr { break-inside: avoid; }
.doc .d-sign { display: grid; grid-template-columns: 1fr 1fr; gap: 10mm; margin-top: 6mm; break-inside: avoid; }
.doc .d-sign h4 { margin: 0 0 3mm; font-size: 10.5pt; }
.doc .d-sign div.l { margin-bottom: 3.5mm; }
.doc .d-sign .ln { display: inline-block; border-bottom: 1px solid #333; min-width: 45mm; height: 1em; vertical-align: bottom; }
.doc .draft-mark { color: #c00; font-size: 9pt; text-align: center; margin-bottom: 2mm; }

/* ---------- print ---------- */
.print-bar { position: sticky; top: 0; z-index: 5; background: var(--navy); color: #fff; padding: 10px 18px; display: flex; gap: 10px; align-items: center; }
.print-bar .spacer { flex: 1; }
.print-page { background: #d9dee5; min-height: 100vh; }
.print-page .doc { margin: 18px auto; box-shadow: 0 2px 12px rgba(0,0,0,.18); }

@page { size: A4; margin: 14mm 0 16mm; }
@media print {
  body { background: #fff; }
  .app { display: block; }
  .main { padding: 0 12mm; }
  .card, .kpi { break-inside: avoid; }
  .no-print, .print-bar, .sidebar { display: none !important; }
  .print-page { background: #fff; }
  .print-page .doc { margin: 0; box-shadow: none; width: auto; min-height: 0; padding: 0 17mm; }
  .doc table.d-t th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .doc table.d-t tr:nth-child(even) td { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

@media (max-width: 1100px) {
  .editor { grid-template-columns: 1fr; }
  .editor .preview-wrap { position: static; max-height: none; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .main { padding: 16px; }
  .g2, .g3 { grid-template-columns: 1fr; }
}
