:root {
  --bg: var(--tm-bg);
  --surface: var(--tm-surface);
  --surface-2: var(--tm-surface-2);
  --border: var(--tm-border);
  --fg: var(--tm-text);
  --fg-dim: var(--tm-text-muted);
  --accent: var(--tm-accent);
  --accent-fg: #ffffff;
  --danger: var(--tm-error);
  --ok: var(--tm-success);
  --warn: #c4932f;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  font-size: 14px;
  line-height: 1.5;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* layout */
.app { display: grid; grid-template-rows: auto 1fr; height: 100vh; }
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-weight: 600; margin: 0; flex: 1; font-size: 16px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.user-display { color: var(--fg-dim); font-size: 13px; }
.body { display: grid; grid-template-columns: 220px 1fr; min-height: 0; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 12px; overflow-y: auto; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar-nav button {
  background: transparent;
  border: none;
  color: var(--fg-dim);
  text-align: left;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
}
.sidebar-nav button:hover { background: var(--surface-2); color: var(--fg); }
.sidebar-nav button.active { background: var(--surface-2); color: var(--fg); }
.main { padding: 18px; overflow: auto; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
}
.card h2, .card h3 { margin-top: 0; }
.muted { color: var(--fg-dim); }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { min-width: 0; }
.spacer { flex: 1; }

button.btn, .btn {
  background: var(--surface-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font: inherit;
}
.btn:hover { background: var(--border); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.compact { padding: 4px 9px; font-size: 12px; }

input, select, textarea {
  background: var(--surface-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font: inherit;
}
textarea { width: 100%; min-height: 80px; resize: vertical; }

table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--fg-dim); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }

/* role badges */
.role { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 11px; }
.role.master { background: #6e3fb3; color: #fff; }
.role.admin  { background: #2c6cd1; color: #fff; }
.role.user   { background: #475160; color: #fff; }

.empty { padding: 24px; text-align: center; color: var(--fg-dim); }
.empty.inline { padding: 18px 0; text-align: left; }

.banner { padding: 10px 14px; border-radius: 6px; margin-bottom: 10px; }
.banner.error { background: rgba(217,100,89,0.18); border: 1px solid var(--danger); color: var(--fg); }

/* admin operations */
.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}
.page-head h1 {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
  font-weight: 650;
}
.page-head p { margin: 4px 0 0; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.ops-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}
.metric,
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.metric {
  min-height: 112px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.metric-label {
  color: var(--fg-dim);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.metric-value {
  font-size: 28px;
  line-height: 1.1;
  font-weight: 700;
  margin-top: 8px;
}
.metric-foot {
  min-height: 22px;
  color: var(--fg-dim);
  font-size: 13px;
  margin-top: 10px;
}
.ops-split {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(280px, 0.8fr);
  gap: 14px;
}
.panel { padding: 14px; min-width: 0; }
.panel-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}
.panel-title {
  font-size: 14px;
  font-weight: 650;
}
.compact-table th,
.compact-table td {
  padding: 7px 6px;
}
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 8px;
  color: var(--fg);
  font-size: 12px;
  white-space: nowrap;
}
.status-pill span {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: var(--fg-dim);
}
.status-pill.ok span { background: var(--ok); }
.status-pill.warn span { background: var(--warn); }
.checklist {
  display: grid;
  gap: 8px;
}
.check-row {
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  min-height: 30px;
}
.skeleton {
  position: relative;
  overflow: hidden;
}
.skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--surface-2) 80%, transparent), transparent);
  animation: shimmer 1.3s ease-out infinite;
}
@keyframes shimmer {
  from { transform: translateX(-100%); }
  to { transform: translateX(100%); }
}

@media (max-width: 900px) {
  .body { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
  .sidebar-nav { flex-direction: row; }
  .sidebar-nav button { white-space: nowrap; }
  .ops-grid { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
  .ops-split { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
  .main { padding: 12px; }
  .page-head { display: block; }
  .page-actions { justify-content: flex-start; margin-top: 10px; }
  .ops-grid { grid-template-columns: 1fr; }
  .row { flex-wrap: wrap; align-items: stretch; }
}

/* chat (user mode) */
.chat { display: grid; grid-template-rows: 1fr auto; height: 100%; }
.chat-messages { padding: 8px; overflow-y: auto; }
.chat-msg { padding: 8px 12px; margin: 4px 0; border-radius: 10px; max-width: 75%; }
.chat-msg.user { background: var(--accent); color: var(--accent-fg); margin-left: auto; }
.chat-msg.assistant { background: var(--surface-2); color: var(--fg); }
.chat-input { display: flex; gap: 8px; padding: 8px; border-top: 1px solid var(--border); background: var(--surface); }
.chat-input textarea { flex: 1; min-height: 36px; max-height: 120px; }
