/* ---- sidebar / topbar / shell ---- */

.sidebar {
  width: var(--sidebar-w);
  flex: none;
  height: 100%;
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: var(--space-4) var(--space-3);
  gap: var(--space-4);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 600;
  padding: 0 var(--space-2);
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 9px var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  min-height: 40px;
}
.nav-link:hover {
  background: var(--surface-hover);
  text-decoration: none;
  color: var(--text);
}
.nav-link.active {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 600;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.conn-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--grey-400);
  flex: none;
}
.conn-dot.ok { background: var(--success); }
.conn-dot.bad { background: var(--danger); }
.topbar-conn-dot { margin: 0 var(--space-1); }
@media (min-width: 761px) {
  .topbar-conn-dot { display: none; }
}

.main-col {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.topbar {
  flex: none;
  height: var(--topbar-h);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-5);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-title {
  font-weight: 600;
  font-size: var(--text-lg);
  margin: 0;
}

.topbar-search {
  flex: 1;
  max-width: 420px;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
  padding: 0 var(--space-3);
  height: 36px;
}
.topbar-search input {
  border: none;
  background: transparent;
  padding: 0;
  height: auto;
  min-height: 0;
  width: 100%;
}
.topbar-search input:focus-visible {
  outline: none;
}

.page-body {
  padding: var(--space-5);
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.bottom-tabbar {
  display: none;
}

@media (max-width: 760px) {
  .sidebar { display: none; }
  .bottom-tabbar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: 20;
    padding: 4px 0 max(4px, env(safe-area-inset-bottom));
  }
  .bottom-tabbar .nav-link {
    flex: 1;
    flex-direction: column;
    gap: 2px;
    font-size: 11px;
    justify-content: center;
    text-align: center;
  }
  .page-body { padding: var(--space-3); padding-bottom: 72px; }
}

/* ---- buttons ---- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 40px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
  transition: background var(--duration-fast) var(--ease), border-color var(--duration-fast) var(--ease);
  white-space: nowrap;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-strong); }

.btn-secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}
.btn-secondary:hover:not(:disabled) { background: var(--surface-hover); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover:not(:disabled) { background: var(--surface-hover); color: var(--text); }

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover:not(:disabled) { background: #b32e2e; }

.btn-icon {
  min-height: 36px;
  min-width: 36px;
  padding: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.btn-icon:hover { background: var(--surface-hover); color: var(--text); }
.btn-icon.active { color: var(--accent); background: var(--accent-soft); }
.btn-icon.on { color: var(--warn); }

.btn-sm { min-height: 32px; padding: 0 var(--space-3); font-size: var(--text-sm); }

/* ---- cards ---- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.card-pad { padding: var(--space-5); }

/* ---- KPI ---- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

.kpi-card {
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.kpi-label {
  font-size: var(--text-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.kpi-value {
  font-size: var(--text-2xl);
  font-weight: 650;
}

/* ---- pill / badge / chip ---- */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 600;
}
.pill-success { background: var(--success-soft); color: var(--success); }
.pill-warn { background: var(--warn-soft); color: var(--warn); }
.pill-danger { background: var(--danger-soft); color: var(--danger); }
.pill-neutral { background: var(--surface-muted); color: var(--text-muted); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  background: var(--danger-soft);
  color: var(--danger);
}
.chip-tag {
  background: var(--surface-muted);
  color: var(--text);
}
.chip-remove {
  cursor: pointer;
  border: none;
  background: transparent;
  color: inherit;
  font-size: 13px;
  line-height: 1;
  padding: 0 0 0 2px;
}

.score-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 22px;
  padding: 0 6px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 700;
}
.score-good { background: var(--success-soft); color: var(--success); }
.score-mid { background: var(--warn-soft); color: var(--warn); }
.score-low { background: var(--danger-soft); color: var(--danger); }
.score-none { background: var(--surface-muted); color: var(--text-muted); }

/* ---- toast ---- */

.toast {
  background: var(--grey-900);
  color: #fff;
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  font-size: var(--text-sm);
  animation: toast-in var(--duration-med) var(--ease);
}
.toast-danger { background: var(--danger); }
.toast-success { background: var(--success); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.toast-close {
  background: transparent;
  border: none;
  color: inherit;
  opacity: 0.7;
  cursor: pointer;
  margin-left: auto;
}

/* ---- dialogs / drawers ---- */

.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 90;
  display: flex;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  margin: auto;
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  overflow: auto;
  padding: var(--space-5);
}

.drawer {
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  margin-left: auto;
  width: min(480px, 100vw);
  height: 100%;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.overlay-nested {
  background: transparent;
}
.overlay-nested .drawer {
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.25), var(--shadow-lg);
  border-left: 1px solid var(--border);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

.drawer-body {
  padding: var(--space-5);
  flex: 1;
}

.dialog-title { font-size: var(--text-lg); font-weight: 650; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-5);
}

/* ---- forms ---- */

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: var(--space-4);
  min-width: 0;
}
.field label {
  font-size: var(--text-sm);
  font-weight: 550;
  color: var(--text-muted);
}
.field-hint { font-size: var(--text-xs); color: var(--text-faint); }
.field-error { font-size: var(--text-xs); color: var(--danger); }
.field.has-error input, .field.has-error select, .field.has-error textarea {
  border-color: var(--danger);
}
.field-row {
  display: flex;
  gap: var(--space-3);
  min-width: 0;
}
.field-row > .field { flex: 1; min-width: 0; }
@media (max-width: 480px) {
  .field-row { flex-wrap: wrap; }
  .field-row > .field { flex: 1 1 100%; }
}

.field-group-title {
  font-size: var(--text-sm);
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-faint);
  margin: var(--space-5) 0 var(--space-2);
}
.field-group-title:first-child { margin-top: 0; }

.tag-input {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.tag-input input {
  border: none;
  min-height: 28px;
  flex: 1;
  min-width: 80px;
  padding: 2px 4px;
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}
.segmented button {
  border: none;
  background: var(--surface);
  padding: 6px 12px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: var(--text-sm);
  min-height: 32px;
  white-space: nowrap;
  flex: none;
}
.segmented button + button { border-left: 1px solid var(--border); }
.segmented::-webkit-scrollbar { height: 4px; }
.segmented::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.segmented button.active { background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; }

.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 24px;
  flex: none;
}
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch-track {
  position: absolute;
  inset: 0;
  background: var(--grey-300);
  border-radius: var(--radius-full);
  transition: background var(--duration-fast) var(--ease);
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform var(--duration-fast) var(--ease);
}
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- skeleton ---- */

.skeleton {
  background: linear-gradient(90deg, var(--surface-muted) 25%, var(--border) 37%, var(--surface-muted) 63%);
  background-size: 400% 100%;
  animation: skeleton-shine 1.4s ease infinite;
  border-radius: var(--radius-sm);
}
@keyframes skeleton-shine {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}
.skeleton-line { height: 14px; margin-bottom: 6px; }
.skeleton-line.short { width: 60%; }

/* ---- empty state ---- */

.empty-state {
  text-align: center;
  padding: var(--space-7) var(--space-4);
  color: var(--text-muted);
}
.empty-state .icon { width: 32px; height: 32px; margin: 0 auto var(--space-3); }

/* ---- sticky save bar ---- */

.save-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.05);
  margin: var(--space-5) calc(var(--space-5) * -1) calc(var(--space-5) * -1);
}
