*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #475569;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --secondary: #0ea5e9;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --radius: 10px;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

h1, h2, h3, p {
  margin: 0;
}

h1 {
  font-size: 1.25rem;
  line-height: 1.3;
}

h2 {
  font-size: 1rem;
  line-height: 1.35;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
  font-size: 0.875rem;
}

.error-text {
  color: var(--danger);
  font-size: 0.875rem;
  font-weight: 500;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 0.875rem;
  min-width: 0;
}

.list-stack {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  min-width: 0;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  min-width: 0;
  max-width: 100%;
}

.card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

button, a.nav-link, input, select, textarea {
  font: inherit;
  min-height: 40px;
  border-radius: 8px;
}

button, a.nav-link {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 140ms ease, border-color 140ms ease, opacity 140ms ease, transform 80ms ease;
}

button:active:not(:disabled), a.nav-link:active {
  transform: scale(0.98);
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

input, select, textarea {
  width: 100%;
  max-width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.875rem;
  font-weight: 500;
  min-width: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.5rem 0.875rem;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
  font-size: 0.875rem;
  text-decoration: none;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.btn-ghost:hover {
  background: #f1f5f9;
}

.btn-danger {
  background: #fef2f2;
  border-color: #fecaca;
  color: var(--danger);
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.8125rem;
}

.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 380px;
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.brand-inline {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 1rem;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 700;
  font-size: 1rem;
  flex-shrink: 0;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0.5rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.top-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.375rem;
  min-width: 0;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.35rem 0.7rem;
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.875rem;
  border-radius: 8px;
}

.nav-link.active {
  background: #eef2ff;
  color: var(--accent);
}

#logout-btn {
  min-height: 44px;
  margin-left: auto;
}

.container {
  max-width: 1120px;
  margin: 0 auto;
  padding: 1rem 0.75rem 2.5rem;
  min-width: 0;
}

.view {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

.view-head {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 0;
}

.title-group {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
  min-width: 0;
}

.range-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.range-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.35rem 0.75rem;
  cursor: pointer;
  font-size: 0.8125rem;
  font-weight: 500;
}

.range-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.kpi-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
}

.kpi-card {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.kpi-label {
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 500;
}

.kpi-value {
  font-size: 1.65rem;
  line-height: 1.2;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.chart-wrap {
  width: 100%;
  overflow: hidden;
}

#timeseries-chart {
  display: block;
  width: 100%;
  height: auto;
}

.chart-legend {
  display: flex;
  gap: 0.75rem;
  font-size: 0.8125rem;
  color: var(--muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  display: inline-block;
}

.dot-pv {
  background: var(--accent);
}

.dot-uv {
  background: var(--secondary);
}

.breakdowns-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
  min-width: 0;
}

.breakdown-card {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.breakdown-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 0;
}

.breakdown-row {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
}

.breakdown-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-width: 0;
}

.breakdown-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  font-size: 0.875rem;
}

.breakdown-val {
  flex-shrink: 0;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.bar-progress, .usage-bar {
  width: 100%;
  height: 6px;
  border: none;
  border-radius: 999px;
  background: #e2e8f0;
  appearance: none;
  overflow: hidden;
}

.usage-bar {
  height: 12px;
}

.bar-progress::-webkit-progress-bar, .usage-bar::-webkit-progress-bar {
  background: #e2e8f0;
  border-radius: 999px;
}

.bar-progress::-webkit-progress-value, .usage-bar::-webkit-progress-value {
  background: var(--accent);
  border-radius: 999px;
}

.bar-progress::-moz-progress-bar, .usage-bar::-moz-progress-bar {
  background: var(--accent);
  border-radius: 999px;
}

.site-row {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  min-width: 0;
}

.site-row-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-width: 0;
}

.site-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-active {
  background: #ecfdf5;
  color: var(--success);
}

.badge-paused {
  background: #fffbeb;
  color: var(--warning);
}

.snippet-box {
  background: #0f172a;
  color: #e2e8f0;
  padding: 0.625rem 0.75rem;
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.75rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
  margin: 0;
}

.actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.doc-section pre {
  background: #0f172a;
  color: #e2e8f0;
  padding: 0.75rem;
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.78rem;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
  margin: 0.5rem 0 0;
}

.empty-state {
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.875rem;
  border: 1px dashed var(--border);
  border-radius: 8px;
}

.toast {
  position: fixed;
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  right: 1rem;
  z-index: 50;
  background: #0f172a;
  color: #ffffff;
  padding: 0.65rem 1rem;
  border-radius: 8px;
  font-size: 0.875rem;
  max-width: calc(100vw - 2rem);
}

@media (min-width: 640px) {
  .topbar-inner {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 1.25rem;
  }

  .view-head {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .kpi-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 768px) {
  .container {
    padding: 1.5rem 1.25rem 3rem;
  }

  .breakdowns-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
