* { box-sizing: border-box; }

/* ---------- theme tokens ---------- */
:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface2: #f0f3f9;
  --hover: #f7f9fd;
  --border: #e3e8f1;
  --border-strong: #cfd7e6;
  --text: #0f172a;
  --text2: #475569;
  --muted: #64748b;
  --primary: #0b5fe8;
  --primary-hover: #084fc4;
  --on-primary: #ffffff;
  --link: #0b5fe8;
  --ring: rgba(11, 95, 232, .22);
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 4px 14px rgba(15, 23, 42, .05);
  --blue-bg: #e6efff;   --blue: #1c5fd6;
  --green-bg: #e1f6ea;  --green: #12794a;
  --orange-bg: #fff0dc; --orange: #b45309;
  --red-bg: #fde8e8;    --red: #c42b2b;
  --purple-bg: #efe9fe; --purple: #6b3fd4;
  --slate-bg: #eaeef5;  --slate: #475569;
  --radius: 10px;
  --maxw: 1760px;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1020;
  --surface: #131a2e;
  --surface2: #1b2440;
  --hover: #172039;
  --border: #26314f;
  --border-strong: #35436a;
  --text: #e8edf8;
  --text2: #b6c1da;
  --muted: #8b99b8;
  --primary: #3b82f6;
  --primary-hover: #5b9aff;
  --on-primary: #ffffff;
  --link: #7db1ff;
  --ring: rgba(91, 154, 255, .3);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .25);
  --blue-bg: rgba(91, 154, 255, .16);   --blue: #8dbaff;
  --green-bg: rgba(52, 211, 153, .15);  --green: #5fe0aa;
  --orange-bg: rgba(251, 169, 60, .16); --orange: #ffbe6b;
  --red-bg: rgba(248, 113, 113, .17);   --red: #ff9b9b;
  --purple-bg: rgba(167, 139, 250, .18); --purple: #c4b0ff;
  --slate-bg: rgba(148, 163, 184, .16); --slate: #b6c1da;
}

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.5 Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  transition: background .2s, color .2s;
}
[hidden] { display: none !important; }
a { color: inherit; }
h1, h2, p { margin: 0; }
.container { max-width: var(--maxw); margin: 0 auto; padding: 0 24px; }
.spacer { flex: 1; }
.muted { color: var(--muted); }

/* ---------- buttons & controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 36px; padding: 0 14px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 13px; font-weight: 500; text-decoration: none; cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.btn:hover { background: var(--surface2); border-color: var(--border-strong); }
.btn:focus-visible, .chip-btn:focus-visible, .avatar:focus-visible { outline: 0; box-shadow: 0 0 0 3px var(--ring); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); box-shadow: 0 1px 2px rgba(11, 95, 232, .3); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.primary[disabled] { opacity: .6; cursor: default; }
.btn.icon { width: 36px; padding: 0; }
.btn.sm { width: 34px; height: 34px; padding: 0; background: transparent; border-color: transparent; color: var(--text2); }
.btn.sm:hover { background: var(--surface2); }
.btn.sm.del:hover { background: var(--red-bg); color: var(--red); }
.btn.block { width: 100%; height: 40px; font-size: 14px; }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.danger:hover { filter: brightness(.92); }
.btn.danger-outline { background: var(--red-bg); border-color: transparent; color: var(--red); }
.btn.danger-outline:hover { filter: brightness(.96); }
.btn.disabled { opacity: .45; cursor: default; }
.btn svg, .ico svg { display: block; }

input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface2);
  border: 1px solid var(--border); border-radius: var(--radius); outline: 0;
  transition: border-color .12s, box-shadow .12s, background .12s;
}
input, textarea { width: 100%; padding: 8px 11px; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .8; }
input:focus, textarea:focus, select:focus { background: var(--surface); border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); }
select { height: 36px; padding: 0 10px; font-size: 13px; cursor: pointer; }

.field { display: block; min-width: 0; }
.field > span { display: block; margin-bottom: 5px; font-size: 12px; font-weight: 500; color: var(--text2); }
.field input { height: 38px; }
.field textarea { resize: vertical; min-height: 72px; }
.field.invalid input, .field.invalid textarea { background: var(--red-bg); border-color: var(--red); }
.req { color: var(--red); font-style: normal; }
.help { display: block; margin-top: 8px; font-size: 12px; color: var(--muted); }

.alert { margin-bottom: 14px; padding: 10px 14px; border-radius: var(--radius); background: var(--red-bg); color: var(--red); font-size: 13px; }
.alert .linkbtn { color: inherit; margin-left: 6px; font-weight: 600; }
.linkbtn { padding: 0; border: 0; background: none; font: inherit; color: var(--link); text-decoration: underline; cursor: pointer; }

.badge { display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.blue { background: var(--blue-bg); color: var(--blue); }
.badge.green { background: var(--green-bg); color: var(--green); }
.badge.orange { background: var(--orange-bg); color: var(--orange); }

.chip { display: inline-flex; align-items: center; height: 20px; padding: 0 8px; border-radius: 999px; font-size: 11px; font-weight: 600; width: fit-content; }
.chip.green { background: var(--green-bg); color: var(--green); }
.chip.orange { background: var(--orange-bg); color: var(--orange); }
.chip.red { background: var(--red-bg); color: var(--red); }

/* ---------- login ---------- */
.login {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: radial-gradient(1200px 500px at 50% -10%, var(--blue-bg), transparent 70%), var(--bg);
}
.login-card { width: 100%; max-width: 380px; padding: 32px 28px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.login-logo { width: 64px; height: 64px; margin: 0 auto; border-radius: 16px; box-shadow: 0 6px 18px rgba(11, 95, 232, .35); }
.login-card h1 { text-align: center; font-size: 20px; font-weight: 700; }
.login-sub { text-align: center; margin-top: -8px; color: var(--muted); font-size: 13px; }

/* ---------- navbar ---------- */
.navbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--border); }
.nav-inner { display: flex; align-items: center; gap: 10px; height: 60px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 700; font-size: 15px; letter-spacing: -.01em; }
.brand img { border-radius: 8px; }
.sep { color: var(--muted); }
.crumb { font-size: 14px; color: var(--text2); text-decoration: none; }
.crumb.current { color: var(--muted); max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-wrap { position: relative; }
.avatar { width: 34px; height: 34px; border: 0; border-radius: 50%; background: var(--blue-bg); color: var(--blue); font: inherit; font-size: 14px; font-weight: 700; cursor: pointer; }
.menu { position: absolute; right: 0; top: 44px; min-width: 220px; padding: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 12px 32px rgba(0, 0, 0, .18); }
.menu-user { padding: 6px 10px; font-size: 12px; color: var(--muted); }
.menu-item { display: block; width: 100%; padding: 8px 10px; border: 0; border-radius: 8px; background: transparent; color: var(--text); text-align: left; font: inherit; cursor: pointer; }
.menu-item:hover { background: var(--surface2); }

/* ---------- banner / status ---------- */
.banner { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 16px; padding: 12px 14px; border-radius: 12px; background: var(--orange-bg); color: var(--orange); font-size: 13px; }
.banner-text b { font-weight: 700; }
.status-line { display: flex; align-items: center; gap: 8px; margin-top: 4px; font-size: 12px; color: var(--muted); }
.status-line .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 3px var(--green-bg); }

/* ---------- page head ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 24px 0 16px; }
.page-head h1 { font-size: 24px; font-weight: 700; letter-spacing: -.02em; }
.title-wrap { display: flex; align-items: center; gap: 12px; min-width: 0; }
.title-wrap h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-actions { display: flex; gap: 8px; }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat {
  display: flex; flex-direction: column; gap: 2px; padding: 14px 16px; text-align: left; cursor: pointer;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); font: inherit;
  transition: transform .12s, border-color .12s;
}
.stat:hover { transform: translateY(-1px); border-color: var(--border-strong); }
.stat.active { border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); }
.stat-n { font-size: 26px; font-weight: 700; line-height: 1.1; letter-spacing: -.02em; }
.stat-l { font-size: 12px; color: var(--muted); }
.stat.orange .stat-n { color: var(--orange); }
.stat.blue .stat-n { color: var(--blue); }
.stat.green .stat-n { color: var(--green); }
.stat.red .stat-n { color: var(--red); }
.stat.purple .stat-n { color: var(--purple); }

/* ---------- list ---------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px; margin-bottom: 28px; box-shadow: var(--shadow); }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 2px 2px 12px; }
.filters input { width: 300px; height: 36px; font-size: 13px; }
.filters input#fContact { width: 180px; }
.filters .muted { font-size: 13px; }

.thead, .tr {
  display: grid; align-items: center; gap: 16px; padding: 0 14px;
  grid-template-columns: minmax(210px, 1.5fr) 100px minmax(190px, 1.4fr) minmax(120px, 1fr) minmax(130px, .9fr) minmax(130px, .9fr) minmax(150px, 1fr) 80px;
}
.thead { height: 38px; background: var(--surface2); border-radius: var(--radius); font-size: 12px; font-weight: 600; color: var(--text2); text-transform: uppercase; letter-spacing: .03em; }
.tr { min-height: 64px; padding-top: 10px; padding-bottom: 10px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background .1s; }
.tr:last-child { border-bottom: 0; }
.tr:hover { background: var(--hover); }
.tr:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
.td { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.td a { color: var(--link); text-decoration: none; }
.td a:hover { text-decoration: underline; }
.td.actions { display: flex; justify-content: flex-end; gap: 2px; overflow: visible; }
.td.status { overflow: visible; }
.stack { display: flex; flex-direction: column; gap: 1px; line-height: 1.35; min-width: 0; }
.stack > * { overflow: hidden; text-overflow: ellipsis; }
.stack small { color: var(--muted); font-size: 12px; }
.stack .chip { margin-top: 3px; }
.client-cell { display: flex; align-items: center; gap: 12px; }
.client-cell .stack > span { font-weight: 600; }
.av {
  flex: none; width: 36px; height: 36px; border-radius: 10px; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; background: hsl(var(--h) 80% 50% / .15); color: hsl(var(--h) 65% 38%);
}
:root[data-theme="dark"] .av { color: hsl(var(--h) 85% 74%); background: hsl(var(--h) 80% 60% / .18); }

.empty { padding: 64px 16px; text-align: center; color: var(--muted); }
.empty p { margin: 12px 0 16px; }
.empty-ico { width: 52px; height: 52px; margin: 0 auto; display: flex; align-items: center; justify-content: center; border-radius: 50%; background: var(--surface2); color: var(--text2); }
.empty-ico svg { width: 22px; height: 22px; }

/* Due badge tooltip */
.badge.has-tip { position: relative; cursor: help; }
.badge.has-tip::after {
  content: attr(data-tip); position: absolute; left: 0; top: calc(100% + 6px); z-index: 30; width: max-content; max-width: 230px;
  padding: 7px 10px; border-radius: 8px; background: var(--text); color: var(--bg); font-size: 12px; font-weight: 400; line-height: 1.35; white-space: normal;
  opacity: 0; pointer-events: none; transition: opacity .12s;
}
.badge.has-tip:hover::after, .badge.has-tip:focus::after { opacity: 1; }
.badge.has-tip:focus { outline: 0; }
.badge.has-tip::before { background: currentColor; }

/* ---------- form: main column + sticky sidebar ---------- */
.form-layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 16px 20px; align-items: start; margin-bottom: 32px; }
.form-layout > .alert { grid-column: 1 / -1; margin-bottom: 0; }
.form-main { display: grid; gap: 16px; min-width: 0; }
.form-side { display: grid; gap: 16px; position: sticky; top: 80px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 18px; box-shadow: var(--shadow); }
.card-title { margin-bottom: 14px; font-size: 14px; font-weight: 700; letter-spacing: -.005em; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px 16px; }
.grid .full { grid-column: 1 / -1; }
.with-btn { display: flex; gap: 8px; }
.with-btn input { flex: 1; }
.with-btn .btn { height: 38px; flex: none; }
.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip-btn { height: 28px; padding: 0 11px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface2); color: var(--text2); font: inherit; font-size: 12px; font-weight: 600; cursor: pointer; }
.chip-btn:hover { border-color: var(--primary); color: var(--primary); }
.follow { border-color: var(--primary); background: linear-gradient(var(--blue-bg), var(--surface) 70%); }
.follow .card-title { color: var(--blue); margin-bottom: 4px; }
.side-note { margin-bottom: 12px; font-size: 13px; color: var(--text2); }
.follow .inline { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.follow .inline input { width: 72px; height: 36px; text-align: center; }
.follow .btn.block { margin-top: 12px; }

/* ---------- dialog + toast ---------- */
dialog { width: calc(100% - 32px); max-width: 400px; padding: 22px; border: 1px solid var(--border); border-radius: 16px; background: var(--surface); box-shadow: 0 20px 56px rgba(0, 0, 0, .35); color: var(--text); }
dialog::backdrop { background: rgba(5, 10, 25, .55); }
dialog h2 { font-size: 17px; font-weight: 700; margin-bottom: 6px; }
dialog p { color: var(--text2); word-break: break-word; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50; max-width: calc(100% - 32px); padding: 11px 18px; border-radius: 12px; background: var(--text); color: var(--bg); font-size: 14px; box-shadow: 0 10px 28px rgba(0, 0, 0, .3); }
#toast.error { background: var(--red); color: #fff; }

/* ---------- tablet / phone ---------- */
@media (max-width: 1100px) {
  .form-layout { display: flex; flex-direction: column; align-items: stretch; }
  .form-side { display: contents; }
  .form-layout > .alert { order: 0; }
  #followCard { order: 1; }
  #quickActions { order: 2; }
  .form-main { order: 3; }
  #reminderCard { order: 4; }
}
@media (max-width: 1000px) {
  .thead { display: none; }
  .tr { display: block; position: relative; padding: 16px 14px; }
  .td { display: flex; gap: 10px; padding: 3px 0; white-space: normal; overflow: visible; }
  .td::before { content: attr(data-label); flex: 0 0 84px; font-size: 12px; color: var(--muted); padding-top: 1px; }
  .td.company { padding-right: 100px; }
  .td.company::before, .td.actions::before { display: none; }
  .td.status { position: absolute; top: 16px; right: 14px; padding: 0; }
  .td.status::before { display: none; }
  .td.is-empty { display: none; }
  .td.actions { margin-top: 6px; }
  .td.remarks { word-break: break-word; }
  .badge.has-tip::after { left: auto; right: 0; }
}
@media (max-width: 760px) {
  .container { padding: 0 14px; }
  .brand-name { display: none; }
  .page-head h1 { font-size: 21px; }
  .filters input, .filters input#fContact { flex: 1 1 140px; width: auto; }
  .filters input:first-child { flex-basis: 100%; }
  .stats { display: flex; overflow-x: auto; gap: 10px; margin: 0 -14px 14px; padding: 2px 14px 6px; scrollbar-width: none; }
  .stats::-webkit-scrollbar { display: none; }
  .stat { flex: 0 0 132px; padding: 10px 12px; }
  .stat-n { font-size: 20px; }
  .banner { flex-direction: column; align-items: stretch; }
  .card { padding: 16px; }
}
