:root {
  --bg: #F4F1E8;
  --surface: #FFFDF7;
  --surface-2: #EEF4E8;
  --border: #E2E6D6;
  --text: #2F3B2F;
  --muted: #74836F;
  --accent: #7FB77E;
  --accent-strong: #4E8650;
  --accent-soft: #DDEBD6;
  --ok: #6FAE6E;
  --warn: #E2AE55;
  --warn-soft: #FBEFD8;
  --danger: #D97B78;
  --danger-soft: #F9E1DF;
  --shadow: 0 6px 24px rgba(70, 100, 70, .10), 0 1px 3px rgba(70, 100, 70, .08);
  --radius: 20px;
  --mono: 'JetBrains Mono', ui-monospace, Consolas, monospace;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #171D18;
  --surface: #1F2720;
  --surface-2: #263127;
  --border: #314034;
  --text: #E5EEDF;
  --muted: #9BAE96;
  --accent: #8FC48D;
  --accent-strong: #AED8AA;
  --accent-soft: #2F4232;
  --ok: #8FC48D;
  --warn: #E8C17F;
  --warn-soft: #3B3424;
  --danger: #E59A97;
  --danger-soft: #3F2A29;
  --shadow: 0 6px 24px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 Nunito, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent-strong); text-decoration: none; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: 13px; }

/* ---------- layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; width: 220px; flex: none;
  display: flex; flex-direction: column; gap: 4px; padding: 20px 14px;
  background: var(--surface); border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 800; font-size: 20px; color: var(--text); }
.brand img { width: 36px; height: 36px; border-radius: 10px; }
.sidebar a:not(.brand), .nav-btn {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 14px;
  color: var(--muted); font-weight: 700; font: inherit; font-weight: 700; background: none; border: 0; cursor: pointer; text-align: left;
}
.sidebar a:not(.brand):hover, .nav-btn:hover { background: var(--surface-2); color: var(--text); }
.sidebar a.active { background: var(--accent-soft); color: var(--accent-strong); }
.spacer { flex: 1; }
.view { flex: 1; min-width: 0; padding: 28px clamp(16px, 3vw, 40px) 40px; max-width: 1400px; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.page-head h1 { margin: 0; font-size: 26px; font-weight: 800; display: flex; align-items: center; gap: 10px; }
.page-head .sub { color: var(--muted); font-weight: 600; }

.grid { display: grid; gap: 16px; }
.grid.stats { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid.cards { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.section-title { margin: 30px 0 14px; font-size: 17px; font-weight: 800; display: flex; align-items: center; gap: 8px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px;
}
.card.click { cursor: pointer; transition: transform .15s, box-shadow .15s; }
.card.click:hover { transform: translateY(-2px); }

/* stat card */
.stat { display: flex; flex-direction: column; gap: 10px; overflow: hidden; }
.stat-top { display: flex; align-items: center; gap: 14px; }
.stat-icon { width: 42px; height: 42px; border-radius: 14px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent-strong); }
.stat-label { color: var(--muted); font-weight: 700; font-size: 13px; }
.stat-value { font-size: 26px; font-weight: 800; line-height: 1.1; }
.stat-detail { color: var(--muted); font-size: 13px; font-weight: 600; }
.bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: inherit; background: var(--accent); transition: width .6s ease; }
.bar.warn > i { background: var(--warn); }
.bar.danger > i { background: var(--danger); }
canvas.spark { width: 100%; height: 46px; display: block; margin: 0 -2px -6px; }

/* pills & dots */
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 800; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.ok { background: var(--accent-soft); color: var(--accent-strong); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.danger { background: var(--danger-soft); color: var(--danger); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 0 4px var(--accent-soft); }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 4px var(--warn-soft); }
.dot.danger { background: var(--danger); box-shadow: 0 0 0 4px var(--danger-soft); }

/* container card */
.ct-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.ct-name { font-weight: 800; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ct-image { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ct-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.ct-res { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; font-size: 12px; font-weight: 700; color: var(--muted); }

/* buttons & inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 9px 16px; border-radius: 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 700; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(.95); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 11px; }
.btn.icon { padding: 8px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 9px 12px; outline: none; min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.toolbar .grow { flex: 1; min-width: 160px; }
label.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; color: var(--muted); cursor: pointer; }
label.check input { accent-color: var(--accent); }

/* logs */
.logs {
  margin-top: 14px; background: var(--surface-2); border-radius: 16px; padding: 12px 0; height: 62vh; overflow: auto;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.55;
}
.log-line { padding: 1px 16px; white-space: pre-wrap; word-break: break-word; display: flex; gap: 12px; }
.log-line time { color: var(--muted); flex: none; opacity: .8; }
.log-line.error { background: var(--danger-soft); color: var(--danger); }
.log-line.warn { color: var(--warn); }
.empty { padding: 30px; text-align: center; color: var(--muted); }

/* tables (DB) */
.db-layout { display: grid; grid-template-columns: 260px 1fr; gap: 16px; align-items: start; }
.db-side { display: flex; flex-direction: column; gap: 10px; position: sticky; top: 20px; max-height: calc(100vh - 40px); overflow: auto; }
.db-conn { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 14px; cursor: pointer; font-weight: 700; }
.db-conn:hover { background: var(--surface-2); }
.db-conn.active { background: var(--accent-soft); color: var(--accent-strong); }
.db-conn small { display: block; color: var(--muted); font-weight: 600; font-size: 11px; }
.tbl-list { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.tbl-item { display: flex; justify-content: space-between; gap: 8px; padding: 7px 12px; border-radius: 11px; cursor: pointer; font-size: 14px; }
.tbl-item:hover { background: var(--surface-2); }
.tbl-item.active { background: var(--accent-soft); color: var(--accent-strong); font-weight: 700; }
.tbl-item span:last-child { color: var(--muted); font-size: 12px; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 14px; width: fit-content; }
.tabs button { border: 0; background: none; font: inherit; font-weight: 700; color: var(--muted); padding: 7px 14px; border-radius: 11px; cursor: pointer; }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.table-wrap { overflow: auto; max-height: 64vh; border-radius: 14px; border: 1px solid var(--border); margin-top: 12px; }
table.data { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
table.data th {
  position: sticky; top: 0; background: var(--surface-2); text-align: left; padding: 9px 12px; font-weight: 800;
  white-space: nowrap; cursor: pointer; border-bottom: 1px solid var(--border); z-index: 1;
}
table.data th small { color: var(--muted); font-weight: 600; margin-left: 4px; }
table.data td { padding: 7px 12px; border-bottom: 1px solid var(--border); max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 12.5px; }
table.data tr:hover td { background: var(--surface-2); }
table.data td.null { color: var(--muted); font-style: italic; }
table.data td.editable { cursor: text; }
table.data td input { width: 100%; padding: 3px 6px; font-family: var(--mono); font-size: 12.5px; }
table.data td.actions { width: 1%; }
.pager { display: flex; align-items: center; gap: 10px; justify-content: flex-end; margin-top: 10px; color: var(--muted); font-weight: 700; font-size: 13px; }
textarea.sql { width: 100%; min-height: 140px; font-family: var(--mono); font-size: 13px; resize: vertical; margin-top: 12px; }

/* 3x-ui */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 14px; }
.kv dt { color: var(--muted); font-weight: 700; }
.kv dd { margin: 0; font-weight: 700; }

/* toasts & modal */
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); padding: 12px 16px; border-radius: 14px; font-weight: 700; max-width: 360px; animation: pop .2s ease; }
.toast.error { border-color: var(--danger); color: var(--danger); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }
.modal { border: 0; border-radius: var(--radius); padding: 0; background: var(--surface); color: var(--text); box-shadow: var(--shadow); width: min(520px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(30, 40, 30, .35); backdrop-filter: blur(3px); }
.modal form { padding: 22px; display: flex; flex-direction: column; gap: 12px; }
.modal h2 { margin: 0 0 4px; font-size: 20px; }
.modal .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 700; color: var(--muted); }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(circle at 20% 10%, var(--accent-soft), transparent 50%), radial-gradient(circle at 90% 90%, var(--accent-soft), transparent 45%), var(--bg); }
.login-card { width: min(360px, 100%); background: var(--surface); border-radius: 28px; box-shadow: var(--shadow); padding: 34px 28px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.login-logo { width: 72px; height: 72px; margin: 0 auto; border-radius: 20px; }
.login-card h1 { margin: 0; font-size: 28px; }
.login-card p { margin: 0; }
.login-error { color: var(--danger); font-weight: 700; min-height: 1.5em; }

/* ---------- phone ---------- */
@media (max-width: 820px) {
  .app { flex-direction: column; }
  .sidebar {
    position: fixed; bottom: 0; left: 0; right: 0; top: auto; height: auto; width: auto; z-index: 20;
    flex-direction: row; justify-content: space-around; padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--border);
  }
  .brand, .spacer, #logout { display: none !important; }
  .sidebar a:not(.brand), .nav-btn { flex-direction: column; gap: 2px; padding: 6px 8px; font-size: 11px; border-radius: 12px; }
  .view { padding: 18px 14px 96px; }
  .page-head h1 { font-size: 22px; }
  .db-layout { grid-template-columns: 1fr; }
  .db-side { position: static; max-height: none; }
  .logs { height: 60vh; font-size: 11.5px; }
  .log-line time { display: none; }
  .stat-value { font-size: 22px; }
}
