:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --line: #e6e9f0;
  --text: #16202e;
  --muted: #7b8798;
  --primary: #2f6bff;
  --primary-dark: #1f52d6;
  --green: #12b76a;
  --amber: #f79009;
  --red: #f04438;
  --purple: #7a5af8;
  --side: #0d1526;
  --side-2: #131d33;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px -12px rgba(16, 24, 40, .18);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
a { color: inherit; text-decoration: none; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; }
p { margin: 0; }
button, input, select, textarea { font-family: inherit; font-size: 14px; }

/* ---------- login ---------- */
.login {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: radial-gradient(1200px 600px at 20% 10%, #1b2a4a 0%, #0d1526 45%, #0a1020 100%);
}
.login-box {
  width: 400px; background: #fff; border-radius: 18px; padding: 32px 30px 26px;
  box-shadow: 0 40px 80px -30px rgba(0, 0, 0, .55);
  animation: rise .45s ease both;
}
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 26px; }
.brand h1 { font-size: 19px; }
.brand p { font-size: 12px; color: var(--muted); margin-top: 4px; }
.logo {
  width: 42px; height: 42px; border-radius: 12px; flex: none;
  background: linear-gradient(135deg, #2f6bff, #17c3b2); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 20px;
}
.logo.sm { width: 32px; height: 32px; font-size: 15px; border-radius: 9px; }
.login-form { display: grid; gap: 14px; }
.login-form label, .field { display: grid; gap: 6px; font-size: 12.5px; color: var(--muted); }
input, select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px;
  background: #fff; color: var(--text); outline: none; transition: .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(47, 107, 255, .13); }
textarea { resize: vertical; min-height: 70px; }
.login-error { color: var(--red); font-size: 12.5px; min-height: 16px; }
.demo-hint {
  margin-top: 18px; padding-top: 14px; border-top: 1px dashed var(--line);
  display: grid; gap: 4px; font-size: 12px; color: var(--muted);
}
.demo-hint b { color: var(--text); margin-bottom: 2px; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 224px 1fr; min-height: 100vh; }
.sidebar {
  background: linear-gradient(180deg, var(--side) 0%, #0a1220 100%);
  color: #c7d2e3; display: flex; flex-direction: column; padding: 18px 12px;
  position: sticky; top: 0; height: 100vh;
}
.side-brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 18px; }
.side-brand b { display: block; font-size: 14px; color: #fff; }
.side-brand span { font-size: 11px; color: #6b7c96; letter-spacing: .4px; }
#nav { display: grid; gap: 3px; }
#nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 11px; border-radius: 9px;
  font-size: 13.5px; color: #a9b6ca; cursor: pointer; transition: .15s; position: relative;
}
#nav a:hover { background: #17223a; color: #fff; }
#nav a.active { background: linear-gradient(90deg, rgba(47,107,255,.28), rgba(47,107,255,.06)); color: #fff; }
#nav a.active::before {
  content: ""; position: absolute; left: -12px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 18px; border-radius: 2px; background: var(--primary);
}
#nav i { width: 16px; height: 16px; display: grid; place-items: center; opacity: .9; }
#nav svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.7; }
#nav .dot { width: 6px; height: 6px; border-radius: 50%; background: #17c3b2; margin-left: auto; box-shadow: 0 0 0 3px rgba(23,195,178,.15); }
.side-foot { margin-top: auto; padding: 12px 8px 2px; border-top: 1px solid #1b2740; }
.side-user { display: flex; gap: 9px; align-items: center; }
.side-user b { display: block; font-size: 13px; color: #fff; }
.side-user span { font-size: 11px; color: #6b7c96; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: linear-gradient(135deg, #2f6bff, #7a5af8); color: #fff; font-size: 12px; font-weight: 600;
}

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 62px; background: rgba(255,255,255,.86); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line); display: flex; align-items: center;
  justify-content: space-between; padding: 0 22px; position: sticky; top: 0; z-index: 20;
}
.topbar-left { display: flex; align-items: baseline; gap: 10px; }
.topbar h2 { font-size: 16px; }
.page-sub { font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.month-pick { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.month-pick input { width: 138px; padding: 6px 8px; font-size: 12.5px; }
.view { padding: 20px 22px 40px; animation: fade .25s ease; }

/* ---------- generic ---------- */
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.grid { display: grid; gap: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.mono { font-variant-numeric: tabular-nums; }
.pos { color: var(--green); }
.neg { color: var(--red); }
.mb { margin-bottom: 14px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-head h3 { font-size: 14px; }
.card-head .sub { font-size: 12px; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 9px; border: 1px solid var(--line);
  background: #fff; color: var(--text); cursor: pointer; transition: .15s; white-space: nowrap;
}
.btn:hover { border-color: #c9d2e3; background: #fafbff; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--red); border-color: #fbd3cf; background: #fff6f5; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 10px; font-size: 12.5px; border-radius: 7px; }
.btn.block { width: 100%; padding: 11px; font-size: 14.5px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px;
  font-size: 11.5px; background: #eef2f8; color: #51607a; white-space: nowrap;
}
.badge.green { background: #e7f8ef; color: #0b8a4f; }
.badge.blue { background: #e9f0ff; color: #2456c9; }
.badge.amber { background: #fef4e6; color: #b25e00; }
.badge.red { background: #feecea; color: #c2321f; }
.badge.purple { background: #f0ecff; color: #5b3fd6; }

/* ---------- kpi ---------- */
.kpis { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 16px; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 15px; box-shadow: var(--shadow); }
.kpi .label { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }
.kpi .value { font-size: 22px; font-weight: 650; margin-top: 6px; letter-spacing: -.3px; }
.kpi .value small { font-size: 12px; font-weight: 500; color: var(--muted); margin-left: 2px; }
.kpi .foot { font-size: 11.5px; color: var(--muted); margin-top: 5px; }

.dash-grid { display: grid; grid-template-columns: 1.35fr 1fr; gap: 14px; margin-bottom: 14px; }
.dash-grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ---------- table ---------- */
.table-wrap { overflow: auto; border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-weight: 500; font-size: 12px; color: var(--muted);
  padding: 9px 10px; background: #f8fafd; border-bottom: 1px solid var(--line); white-space: nowrap;
}
td { padding: 10px; border-bottom: 1px solid #f0f2f7; vertical-align: middle; }
tbody tr:hover { background: #fafcff; }
tbody tr.clickable { cursor: pointer; }
td.name { font-weight: 550; }
th.right, td.right { text-align: right; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input, .toolbar select { width: auto; min-width: 130px; padding: 7px 10px; }
.tabs { display: flex; gap: 4px; background: #eef1f7; padding: 4px; border-radius: 10px; width: fit-content; margin-bottom: 14px; }
.tabs button { border: 0; background: transparent; padding: 6px 14px; border-radius: 7px; cursor: pointer; color: var(--muted); font-size: 13px; }
.tabs button.active { background: #fff; color: var(--text); box-shadow: 0 1px 3px rgba(16,24,40,.1); }

.bar-track { height: 6px; border-radius: 4px; background: #eef1f7; overflow: hidden; min-width: 60px; }
.bar-fill { height: 100%; border-radius: 4px; background: linear-gradient(90deg, #2f6bff, #17c3b2); }
.bar-fill.warn { background: linear-gradient(90deg, #f79009, #f04438); }

.legend { display: flex; flex-wrap: wrap; gap: 10px 16px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.legend i { width: 9px; height: 9px; border-radius: 3px; display: inline-block; margin-right: 6px; }

.empty { padding: 26px; text-align: center; color: var(--muted); font-size: 13px; }

/* ---------- modal ---------- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(10, 18, 32, .45); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; z-index: 60; padding: 20px;
  animation: fade .15s ease;
}
.modal {
  background: #fff; border-radius: 16px; width: 520px; max-width: 100%; max-height: 88vh;
  display: flex; flex-direction: column; box-shadow: 0 30px 70px -20px rgba(0,0,0,.4);
  animation: rise .22s ease both;
}
.modal.wide { width: 860px; }
.modal-head { padding: 18px 20px 12px; display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { font-size: 15.5px; }
.modal-body { padding: 4px 20px 8px; overflow: auto; }
.modal-foot { padding: 14px 20px 18px; display: flex; justify-content: flex-end; gap: 8px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.modal .close { border: 0; background: transparent; font-size: 20px; color: var(--muted); cursor: pointer; line-height: 1; }

/* ---------- drawer ---------- */
.drawer-mask { position: fixed; inset: 0; background: rgba(10,18,32,.4); z-index: 55; animation: fade .15s; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100vh; width: 760px; max-width: 92vw; background: #fff;
  z-index: 56; box-shadow: -20px 0 60px -30px rgba(0,0,0,.4); display: flex; flex-direction: column;
  animation: slide .24s ease both;
}
.drawer-head { padding: 18px 22px 12px; border-bottom: 1px solid var(--line); }
.drawer-body { padding: 18px 22px 30px; overflow: auto; }

/* ---------- charts ---------- */
.chart svg { width: 100%; height: auto; display: block; }
.donut-wrap { display: flex; gap: 18px; align-items: center; }
.donut { width: 132px; height: 132px; border-radius: 50%; flex: none; position: relative; }
.donut::after {
  content: ""; position: absolute; inset: 26px; background: #fff; border-radius: 50%;
  box-shadow: inset 0 0 0 1px #f0f2f7;
}
.donut-center { position: absolute; inset: 0; display: grid; place-items: center; z-index: 2; text-align: center; }
.donut-center b { font-size: 16px; display: block; }
.donut-center span { font-size: 10.5px; color: var(--muted); }

/* ---------- AI ---------- */
.ai-layout { display: grid; grid-template-columns: 1fr 286px; gap: 14px; height: calc(100vh - 122px); }
.ai-main { display: flex; flex-direction: column; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.ai-head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.ai-head b { font-size: 14px; }
.ai-head span { font-size: 12px; color: var(--muted); }
.ai-msgs { flex: 1; overflow: auto; padding: 18px; background: linear-gradient(180deg, #fbfcff, #f7f9fd); }
.msg { display: flex; gap: 10px; margin-bottom: 16px; animation: fade .2s ease; }
.msg .bubble {
  padding: 11px 14px; border-radius: 12px; line-height: 1.65; max-width: 76%; white-space: pre-wrap;
  word-break: break-word; font-size: 13.5px;
}
.msg.user { flex-direction: row-reverse; }
.msg.user .bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.ai .bubble { background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 4px; box-shadow: 0 2px 8px rgba(16,24,40,.05); }
.msg .who { width: 28px; height: 28px; border-radius: 50%; flex: none; display: grid; place-items: center; font-size: 11px; font-weight: 600; color: #fff; }
.msg.ai .who { background: linear-gradient(135deg, #7a5af8, #17c3b2); }
.msg.user .who { background: #2f6bff; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--primary); }
.ai-input { border-top: 1px solid var(--line); padding: 12px 14px; display: flex; gap: 10px; align-items: flex-end; background: #fff; }
.ai-input textarea { min-height: 44px; max-height: 140px; resize: none; }
.ai-side { display: flex; flex-direction: column; gap: 12px; overflow: auto; }
.q-item {
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px; font-size: 12.5px;
  cursor: pointer; background: #fff; transition: .15s; line-height: 1.5;
}
.q-item:hover { border-color: #b9c9ff; background: #f7f9ff; color: var(--primary-dark); }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 34px; transform: translate(-50%, 20px); z-index: 90;
  background: #101a2c; color: #fff; padding: 10px 18px; border-radius: 10px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: .22s; box-shadow: 0 12px 30px -10px rgba(0,0,0,.4);
  max-width: 80vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: #b42318; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes slide { from { transform: translateX(28px); opacity: .6; } to { transform: none; opacity: 1; } }
@keyframes blink { 50% { opacity: 0; } }

@media (max-width: 1280px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .dash-grid, .dash-grid2 { grid-template-columns: 1fr; }
  .ai-layout { grid-template-columns: 1fr; height: auto; }
  .ai-side { order: -1; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; align-items: center; padding: 10px; overflow-x: auto; }
  #nav { display: flex; }
  .side-brand, .side-foot { display: none; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .view { padding: 14px; }
  .ai-msgs .msg .bubble { max-width: 88%; }
}
