:root {
  --bg: #0d1017;
  --panel: #151a24;
  --panel-2: #1b2230;
  --line: #262f40;
  --text: #e6eaf2;
  --muted: #8a94a8;
  --accent: #4f8cff;
  --good: #3ddc84;
  --bad: #ff6b6b;
  --warn: #ffd166;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2, h3 { margin: 0; font-weight: 650; }
h2 { font-size: 15px; }

header {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 5;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; }

.logo {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: linear-gradient(135deg, #4f8cff, #7b5cff);
  font-weight: 800;
  font-size: 20px;
  color: #fff;
}
.logo.small { width: 28px; height: 28px; border-radius: 8px; font-size: 11px; }

nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
nav button {
  background: none;
  border: 0;
  color: var(--muted);
  padding: 7px 12px;
  border-radius: 8px;
  font: inherit;
  cursor: pointer;
}
nav button:hover { color: var(--text); background: var(--panel-2); }
nav button.active { color: var(--text); background: var(--panel-2); }

.user { display: flex; align-items: center; gap: 10px; color: var(--muted); }

main { max-width: 1180px; margin: 0 auto; padding: 24px; display: grid; gap: 18px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }
.card, .panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}
.card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.card .value { font-size: 26px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.card .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 18px; }
.chart { position: relative; height: 260px; }
.chart-total { color: var(--muted); margin: -4px 0 10px; }
.chart-total strong { color: var(--text); font-size: 18px; margin-right: 4px; }
.dates { display: flex; gap: 8px; margin: -4px 0 12px; }
.dates input { padding: 5px 8px; }
.seg { display: flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.seg button { background: none; border: 0; color: var(--muted); font: inherit; font-size: 12px; padding: 4px 10px; border-radius: 6px; cursor: pointer; }
.seg button.active { background: var(--accent); color: #fff; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr:last-child td { border-bottom: 0; }
tfoot td { font-weight: 700; border-top: 1px solid var(--line); }
td.wrap { white-space: normal; }

.pos { color: var(--good); }
.neg { color: var(--bad); }
.muted { color: var(--muted); }

.tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--panel-2);
  font-size: 12px;
}
.tag i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

form.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; }
label[hidden] { display: none; }
label { display: grid; gap: 4px; color: var(--muted); font-size: 12px; }
input, select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  min-width: 0;
}
input:focus, select:focus { outline: 1px solid var(--accent); border-color: var(--accent); }
input[type=color] { padding: 2px; width: 42px; height: 36px; }
input.sm { width: 110px; }

.btn {
  display: inline-block;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font: inherit;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.ghost { background: none; }
.btn.danger:hover { border-color: var(--bad); color: var(--bad); }
.btn.xs { padding: 3px 9px; font-size: 12px; }

#login { min-height: 100vh; display: grid; place-items: center; }
#login[hidden], #app[hidden] { display: none; }
.login-card { display: grid; gap: 16px; justify-items: center; padding: 40px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; }

.error { color: var(--bad); margin: 0; }
pre { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 12px; margin: 10px 0 0; max-height: 320px; overflow: auto; font-size: 12px; user-select: all; }
pre[hidden] { display: none; }
.note { color: var(--muted); font-size: 12px; margin: 8px 0 0; }

#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 10px 16px;
  border-radius: 8px;
  z-index: 10;
}
#toast.bad { border-color: var(--bad); color: var(--bad); }

.bar { height: 6px; border-radius: 3px; background: var(--panel-2); overflow: hidden; margin-top: 4px; }
.bar span { display: block; height: 100%; background: var(--accent); }

#chat-open { position: fixed; right: 20px; bottom: 20px; z-index: 8; border-radius: 999px; padding: 10px 18px; box-shadow: 0 6px 20px rgba(0,0,0,.4); }
#chat {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 9;
  width: min(380px, calc(100vw - 24px));
  height: min(520px, calc(100vh - 100px));
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
}
#chat[hidden], #chat-open[hidden] { display: none; }
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); }
#chat-log { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 88%; padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; }
.msg.assistant { align-self: flex-start; background: var(--panel-2); }
.msg.pending { color: var(--muted); }
.msg.failed { color: var(--bad); }
.chat-hints { display: flex; flex-wrap: wrap; gap: 6px; }
#chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
#chat-form input { flex: 1; }

@media (max-width: 860px) {
  .grid-2 { grid-template-columns: 1fr; }
  main { padding: 14px; }
  header { padding: 10px 14px; }
}
