:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e6e8ec;
  --text: #1a1c22;
  --text-muted: #6b7280;
  --text-faint: #9aa0ab;
  --accent: #d1293f;
  --accent-dark: #a91f32;
  --accent-soft: #fbe7ea;
  --green: #1a9e6b;
  --green-soft: #e3f7ee;
  --amber: #c9821a;
  --amber-soft: #fdf1de;
  --blue: #2563eb;
  --blue-soft: #e8effe;
  --ink: #14161b;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 22, 27, 0.04), 0 4px 16px rgba(20, 22, 27, 0.05);
  --nav-h: 60px;
  --tabbar-h: 62px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }

img { max-width: 100%; }

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Top bar ---------- */
.topbar {
  height: var(--nav-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.topbar .brand img { height: 26px; width: auto; display: block; }

.topbar-nav {
  display: none;
  gap: 4px;
  flex: 1;
  overflow-x: auto;
}

.topbar-nav a {
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}

.topbar-nav a:hover { background: var(--surface-2); color: var(--text); }
.topbar-nav a.active { background: var(--accent-soft); color: var(--accent-dark); }

.topbar-spacer { flex: 1; }

.logout-link {
  font-size: 13px;
  color: var(--text-faint);
  font-weight: 600;
  padding: 8px 10px;
}
.logout-link:hover { color: var(--accent); }

/* ---------- Bottom tab bar (mobile) ---------- */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--tabbar-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 40;
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 10.5px;
  color: var(--text-faint);
  font-weight: 600;
}

.tabbar a svg { width: 20px; height: 20px; }
.tabbar a.active { color: var(--accent); }
.tabbar .more-toggle { position: relative; }

.more-sheet {
  position: fixed;
  inset: 0;
  background: rgba(20, 22, 27, 0.4);
  z-index: 50;
  display: none;
}
.more-sheet.open { display: block; }
.more-sheet .sheet-panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  padding: 10px 8px calc(18px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}
.more-sheet .sheet-panel a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 14px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 15px;
  color: var(--text);
}
.more-sheet .sheet-panel a:hover { background: var(--surface-2); }
.sheet-handle {
  width: 36px; height: 4px; border-radius: 2px; background: var(--border);
  margin: 6px auto 10px;
}

/* ---------- Main content ---------- */
.main {
  flex: 1;
  width: 100%;
  max-width: 1320px;
  margin: 0 auto;
  padding: 18px 14px calc(var(--tabbar-h) + 24px);
}

.page-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.page-header h1 { font-size: 20px; margin: 0; font-weight: 800; }
.page-header .sub { color: var(--text-muted); font-size: 13px; margin-top: 2px; }

/* ---------- Cards / panels ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card-pad { padding: 16px; }

.section-title {
  font-size: 13.5px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 22px 0 10px;
}
.section-title:first-child { margin-top: 0; }

/* ---------- KPI grid ---------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
@media (min-width: 700px) {
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 1050px) {
  .kpi-grid { grid-template-columns: repeat(6, 1fr); }
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
  box-shadow: var(--shadow);
}
.kpi .label { font-size: 11.5px; color: var(--text-muted); font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; }
.kpi .value { font-size: 20px; font-weight: 800; margin-top: 4px; line-height: 1.2; }
.kpi .delta { margin-top: 6px; font-size: 12px; font-weight: 700; display: flex; align-items: center; gap: 4px; }
.kpi .delta.up { color: var(--green); }
.kpi .delta.down { color: var(--accent); }
.kpi .delta.flat { color: var(--text-faint); }
.kpi .spark { margin-top: 8px; height: 30px; }

/* ---------- Insight list (dashboard) ---------- */
.insight-list { display: flex; flex-direction: column; gap: 2px; }
.insight-row {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 10px 4px; border-bottom: 1px dashed var(--border);
  font-size: 13.5px;
}
.insight-row:last-child { border-bottom: none; }
.insight-row .ico { flex-shrink: 0; margin-top: 1px; color: var(--text-faint); }
.insight-row b { font-weight: 700; }

/* ---------- Charts ---------- */
.chart-card { padding: 14px 10px 6px; }
.chart-card .chart-title { font-weight: 700; font-size: 14px; padding: 0 8px 6px; }
.chart { width: 100%; height: 300px; }
.chart.tall { height: 360px; }
.chart-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
@media (min-width: 900px) {
  .chart-grid.cols-2 { grid-template-columns: 1fr 1fr; }
}

.period-filter { display: flex; gap: 6px; flex-wrap: wrap; }
.period-filter button {
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 7px 12px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-muted);
  cursor: pointer;
}
.period-filter button.active { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.toolbar .grow { flex: 1; }

.search-input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  background: var(--surface);
  min-width: 160px;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 700;
  border: 1px solid transparent;
  cursor: pointer;
  white-space: nowrap;
}
.btn-primary { background: var(--ink); color: #fff; }
.btn-primary:hover { background: #000; }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--accent-dark); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--surface); color: var(--accent); border-color: var(--border); }
.btn-danger:hover { background: var(--accent-soft); border-color: var(--accent-soft); }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-icon { padding: 7px; border-radius: var(--radius-sm); background: transparent; border: 1px solid var(--border); }
.btn-icon:hover { background: var(--surface-2); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
table.data-table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 720px; }
table.data-table th, table.data-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data-table th { background: var(--surface-2); font-weight: 700; color: var(--text-muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.03em; position: sticky; top: 0; }
table.data-table th a { display: flex; align-items: center; gap: 4px; color: inherit; }
table.data-table td.wrap { white-space: normal; max-width: 240px; }
table.data-table tr:last-child td { border-bottom: none; }
table.data-table tr:hover td { background: var(--surface-2); }
.row-actions { display: flex; gap: 6px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); }
.link-chip {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--blue-soft); color: var(--blue);
  padding: 4px 9px; border-radius: 999px; font-size: 12px; font-weight: 700;
}
.link-chip:hover { text-decoration: underline; }
.empty-chip { color: var(--text-faint); font-size: 12px; }
tr.flash-highlight td { animation: flash 1.8s ease; }
@keyframes flash { 0% { background: var(--amber-soft); } 100% { background: transparent; } }

/* ---------- Mobile cards for tables ---------- */
.rows-cards { display: flex; flex-direction: column; gap: 10px; }
.row-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 13px; box-shadow: var(--shadow);
}
.row-card .rc-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.row-card .rc-title { font-weight: 700; font-size: 14.5px; }
.row-card .rc-date { font-size: 11.5px; color: var(--text-faint); font-weight: 700; }
.row-card .rc-total { font-weight: 800; font-size: 15px; margin-top: 2px; }
.row-card .rc-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.row-card .tag { background: var(--surface-2); border: 1px solid var(--border); padding: 3px 8px; border-radius: 999px; font-size: 11.5px; color: var(--text-muted); font-weight: 600; }
.row-card .rc-status { display: flex; gap: 10px; margin-top: 10px; flex-wrap: wrap; }
.row-card .rc-actions { display: flex; gap: 8px; margin-top: 10px; }

@media (min-width: 860px) { .rows-cards.has-table { display: none; } }
@media (max-width: 859px) { .table-wrap.has-cards { display: none; } }

/* ---------- Status icons ---------- */
.status-toggle {
  display: inline-flex; align-items: center; gap: 5px;
  border: 1px solid var(--border); background: var(--surface);
  padding: 5px 9px 5px 6px; border-radius: 999px; cursor: pointer;
  font-size: 11.5px; font-weight: 700; color: var(--text-muted);
  -webkit-tap-highlight-color: transparent; user-select: none;
}
.status-toggle svg { width: 16px; height: 16px; display: block; }
.status-toggle.s-empty { color: var(--text-faint); }
.status-toggle.s-in_progress { color: var(--amber); background: var(--amber-soft); border-color: transparent; }
.status-toggle.s-done { color: var(--green); background: var(--green-soft); border-color: transparent; }
.status-toggle:active { transform: scale(0.96); }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12px; font-weight: 700; color: var(--text-muted); }
.field input[type=text], .field input[type=number], .field input[type=date],
.field select, .field textarea {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 14px; background: var(--surface); width: 100%; font-family: inherit; color: var(--text);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea { resize: vertical; min-height: 60px; }
.form-actions { display: flex; gap: 10px; margin-top: 16px; justify-content: flex-end; }
.form-error { color: var(--accent); font-size: 12.5px; margin-top: 6px; }

/* Combobox */
.combobox { position: relative; }
.combobox input { width: 100%; }
.combobox .cb-list {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow); max-height: 220px; overflow-y: auto;
}
.combobox .cb-item { padding: 9px 11px; font-size: 13.5px; cursor: pointer; border-bottom: 1px solid var(--border); }
.combobox .cb-item:last-child { border-bottom: none; }
.combobox .cb-item:hover, .combobox .cb-item.active { background: var(--surface-2); }
.combobox .cb-empty { padding: 10px 11px; font-size: 12.5px; color: var(--text-faint); }
.combobox .cb-clear { position: absolute; right: 8px; top: 8px; font-size: 12px; color: var(--text-faint); background: none; border: none; cursor: pointer; padding: 4px; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 22, 27, 0.45); z-index: 60;
  display: flex; align-items: flex-end; justify-content: center;
}
@media (min-width: 700px) { .modal-backdrop { align-items: center; } }
.modal {
  background: var(--surface); width: 100%; max-width: 640px; max-height: 92vh; overflow-y: auto;
  border-radius: 16px 16px 0 0; padding: 18px 18px calc(20px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}
@media (min-width: 700px) { .modal { border-radius: 16px; padding: 22px; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h3 { margin: 0; font-size: 17px; font-weight: 800; }
.modal-close { background: none; border: none; font-size: 20px; color: var(--text-faint); cursor: pointer; padding: 4px; }

/* Login */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; background: var(--ink); }
.login-card { background: var(--surface); border-radius: 18px; padding: 34px 28px; width: 100%; max-width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,0.35); text-align: center; }
.login-card img { height: 64px; margin-bottom: 6px; }
.login-card h1 { font-size: 18px; margin: 0 0 22px; font-weight: 800; }
.login-card .field { text-align: left; margin-bottom: 12px; }
.login-card .btn { width: 100%; margin-top: 8px; padding: 11px; font-size: 14.5px; }

/* Conversion table extras */
.conv-cell { font-variant-numeric: tabular-nums; }
.conv-add-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; background: var(--surface-2); color: var(--text-muted); }

.empty-state { text-align: center; padding: 50px 20px; color: var(--text-muted); }
.empty-state .ico { font-size: 34px; margin-bottom: 10px; }

@media (min-width: 900px) {
  .topbar-nav { display: flex; }
  .tabbar { display: none; }
  .main { padding-bottom: 30px; }
}
@media (max-width: 899px) {
  .topbar .brand span.full { display: none; }
}
