/*
 * WiseMetering UI design system, plain CSS.
 * Tokens copied verbatim from wisemetering/frontend src/css/wisemetering.css
 * (branch feature-new-app-claude); components translated from src/components/ui/*.jsx.
 * Theme: follows the OS; force with <html data-theme="light|dark">.
 * Component looks follow chart-generator (charts.lmit.pt) src/ui.rs so both services match:
 * page on --surface, cards on --background, Tabs-style nav, light code blocks, plain table headers.
 */

:root {
  --background: 0 0% 100%;
  --foreground: 222 47% 11%;
  --surface: 210 40% 98%;
  --muted: 210 40% 96%;
  --muted-foreground: 215 16% 47%;
  --accent: 210 40% 98%;
  --border: 214 32% 91%;
  --input: 214 32% 85%;
  --divider: 215 16% 84%;
  --divider-strong: 215 16% 50%;
  --overlay: 222 47% 8%;
  --primary: 184 40% 22%;
  --primary-foreground: 210 40% 98%;
  --primary-surface: 184 70% 96%;
  --secondary: 87 54% 51%;
  --secondary-foreground: 210 40% 98%;
  --secondary-surface: 78 92% 95%;
  --info: 200 98% 39%;
  --info-foreground: 0 0% 100%;
  --info-surface: 204 100% 97%;
  --info-border: 200 70% 80%;
  --success: 150 58% 40%;
  --success-foreground: 0 0% 100%;
  --success-surface: 152 81% 96%;
  --success-border: 150 55% 78%;
  --warning: 23 84% 31%;
  --warning-foreground: 0 0% 100%;
  --warning-surface: 48 100% 96%;
  --warning-border: 30 80% 78%;
  --destructive: 359 79% 59%;
  --destructive-foreground: 0 0% 100%;
  --destructive-surface: 356 100% 97%;
  --destructive-border: 354 70% 85%;
  --radius: 0.5rem;
  --chart-1: #25787E; --chart-2: #80BC2E; --chart-3: #F59D24; --chart-4: #6EB3E3;
  --chart-5: #C974AD; --chart-6: #E94547; --chart-7: #4D7D9E; --chart-8: #797979;

  --font-sans: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  color-scheme: light;
}

:root[data-theme="dark"], .dark {
  --background: 224 71% 4%;
  --foreground: 213 31% 91%;
  --surface: 216 34% 14%;
  --muted: 216 34% 18%;
  --muted-foreground: 215 16% 65%;
  --accent: 216 34% 14%;
  --border: 216 34% 22%;
  --input: 216 34% 32%;
  --divider: 215 16% 42%;
  --divider-strong: 215 16% 82%;
  --overlay: 224 71% 2%;
  --primary: 184 30% 70%;
  --primary-foreground: 222 47% 11%;
  --primary-surface: 184 30% 18%;
  --secondary: 87 54% 51%;
  --secondary-foreground: 222 47% 11%;
  --secondary-surface: 87 30% 18%;
  --info: 200 80% 55%;
  --info-foreground: 222 47% 11%;
  --info-surface: 200 60% 18%;
  --info-border: 200 50% 38%;
  --success: 150 50% 55%;
  --success-foreground: 0 0% 100%;
  --success-surface: 150 50% 18%;
  --success-border: 150 40% 38%;
  --warning: 30 75% 45%;
  --warning-foreground: 0 0% 100%;
  --warning-surface: 30 70% 18%;
  --warning-border: 30 60% 38%;
  --destructive: 0 63% 50%;
  --destructive-foreground: 0 0% 100%;
  --destructive-surface: 0 60% 18%;
  --destructive-border: 0 50% 38%;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --background: 224 71% 4%;
    --foreground: 213 31% 91%;
    --surface: 216 34% 14%;
    --muted: 216 34% 18%;
    --muted-foreground: 215 16% 65%;
    --accent: 216 34% 14%;
    --border: 216 34% 22%;
    --input: 216 34% 32%;
    --divider: 215 16% 42%;
    --divider-strong: 215 16% 82%;
    --overlay: 224 71% 2%;
    --primary: 184 30% 70%;
    --primary-foreground: 222 47% 11%;
    --primary-surface: 184 30% 18%;
    --secondary: 87 54% 51%;
    --secondary-foreground: 222 47% 11%;
    --secondary-surface: 87 30% 18%;
    --info: 200 80% 55%;
    --info-foreground: 222 47% 11%;
    --info-surface: 200 60% 18%;
    --info-border: 200 50% 38%;
    --success: 150 50% 55%;
    --success-foreground: 0 0% 100%;
    --success-surface: 150 50% 18%;
    --success-border: 150 40% 38%;
    --warning: 30 75% 45%;
    --warning-foreground: 0 0% 100%;
    --warning-surface: 30 70% 18%;
    --warning-border: 30 60% 38%;
    --destructive: 0 63% 50%;
    --destructive-foreground: 0 0% 100%;
    --destructive-surface: 0 60% 18%;
    --destructive-border: 0 50% 38%;
    color-scheme: dark;
  }
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; border: 0 solid hsl(var(--border)); }
html, body { margin: 0; }
body {
  font-family: var(--font-sans); font-size: 14px; line-height: 1.5; font-weight: 400;
  background: hsl(var(--surface)); color: hsl(var(--foreground)); font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
}
a { color: hsl(var(--primary)); text-underline-offset: 2px; text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre, kbd { font-family: var(--font-mono); }
img { max-width: 100%; }

/* ---------- Typography (Typography.jsx) ---------- */
.display { font-size: 30px; font-weight: 600; line-height: 1.25; letter-spacing: -0.025em; margin: 0; }
.title-lg { font-size: 20px; font-weight: 600; line-height: 1.25; margin: 0; }
.title { font-size: 18px; font-weight: 600; line-height: 1.25; margin: 0; }
.title-sm { font-size: 16px; font-weight: 600; line-height: 1.25; margin: 0; }
.body { font-size: 14px; line-height: 24px; }
.body-sm { font-size: 12px; line-height: 20px; }
.caption { font-size: 11px; line-height: 20px; }
.overline { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; line-height: 20px; color: hsl(var(--muted-foreground)); }
.control { font-size: 14px; font-weight: 500; line-height: 20px; }
.muted { color: hsl(var(--muted-foreground)); }
.tone-success { color: hsl(var(--success)); } .tone-warning { color: hsl(var(--warning)); }
.tone-destructive { color: hsl(var(--destructive)); } .tone-info { color: hsl(var(--info)); }
.mono { font-family: var(--font-mono); font-size: 13px; }
code.chip, .chip { border-radius: 4px; background: hsl(var(--muted)); padding: 1px 5px; font-size: 13px; font-weight: 400; font-family: var(--font-mono); overflow-wrap: anywhere; }

/* ---------- Layout shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 40; height: 50px; display: flex; align-items: center; gap: 16px;
  padding: 0 24px; background: hsl(var(--background)); border-bottom: 1px solid hsl(var(--border));
}
.topbar .brand { display: flex; align-items: center; gap: 12px; color: hsl(var(--primary)); font-weight: 600; font-size: 16px; }
.topbar .brand img { height: 22px; width: auto; }
.topbar .spacer { flex: 1; }
.logo-dark { display: none; }
:root[data-theme="dark"] .logo-light { display: none; } :root[data-theme="dark"] .logo-dark { display: inline; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-light { display: none; }
  :root:not([data-theme="light"]) .logo-dark { display: inline; }
}
.shell { display: flex; gap: 24px; align-items: flex-start; max-width: 1600px; margin: 0 auto; padding: 24px; }
.aside {
  width: 260px; flex-shrink: 0; position: sticky; top: 74px; max-height: calc(100vh - 98px); overflow: auto;
  border: 1px solid hsl(var(--border)); background: hsl(var(--background)); border-radius: 16px; padding: 16px;
}
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 24px; }
.page { max-width: 1600px; margin: 0 auto; padding: 24px; display: flex; flex-direction: column; gap: 24px; }
.section { border: 1px solid hsl(var(--border)); background: hsl(var(--background)); border-radius: 16px; padding: 24px; min-width: 0; }
@media (min-width: 1024px) { .section { padding: 32px; } .shell, .page { padding: 32px; gap: 32px; } }
.shell, .page { max-width: 1440px; }
@media (max-width: 900px) { .shell { flex-direction: column; } .aside { width: 100%; position: static; max-height: none; } }
.stack { display: flex; flex-direction: column; gap: 16px; } .stack-sm { display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.separator { height: 1px; width: 100%; background: hsl(var(--border)); border: 0; margin: 0; }

/* ---------- SidebarMenu ---------- */
.menu { display: flex; flex-direction: column; gap: 4px; }
.menu a {
  display: flex; gap: 10px; align-items: center; border-radius: 8px; padding: 8px 12px;
  color: hsl(var(--muted-foreground)); font-size: 14px; font-weight: 500; text-decoration: none; transition: background-color .15s, color .15s;
}
.menu a:hover { background: hsl(var(--muted)); color: hsl(var(--foreground)); }
.menu a.active, .menu a[aria-current="page"] { background: hsl(var(--muted)); color: hsl(var(--foreground)); }
.menu .group { margin: 16px 12px 4px; font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: hsl(var(--muted-foreground)); }
.menu .group:first-child { margin-top: 4px; }
/* Tabs-style navigation (segmented control on --muted, active item on --background) */
.nav { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 2px; background: hsl(var(--muted)); padding: 4px; border-radius: 8px; }
.nav a { padding: 6px 12px; border-radius: 6px; color: hsl(var(--muted-foreground)); font-size: 14px; font-weight: 500; line-height: 20px; text-decoration: none; }
.nav a:hover { color: hsl(var(--foreground)); text-decoration: none; }
.nav a[aria-current="page"], .nav a.active { background: hsl(var(--background)); color: hsl(var(--foreground)); }

/* ---------- Button (Button.jsx + buttonAppearance.js) ---------- */
.btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center; gap: 8px; white-space: nowrap;
  height: 36px; padding: 8px 16px; border-radius: 6px; border: 1px solid transparent;
  font: inherit; font-size: 14px; font-weight: 500; line-height: 20px; cursor: pointer; text-decoration: none;
  transition: background-color .15s, color .15s, border-color .15s;
  background: hsl(var(--primary)); color: hsl(var(--primary-foreground));
}
.btn:hover { background: hsl(var(--primary) / .9); text-decoration: none; }
.btn:disabled, .btn[aria-disabled="true"] { pointer-events: none; opacity: .5; }
.btn-sm { height: 28px; padding: 4px 12px; font-size: 13px; }
.btn-lg { height: 44px; padding: 0 32px; }
.btn-outline { background: hsl(var(--primary-surface)); color: hsl(var(--primary)); border-color: hsl(var(--primary)); }
.btn-outline:hover { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.btn-ghost { background: transparent; color: hsl(var(--primary)); }
.btn-ghost:hover { background: hsl(var(--primary-surface)); }
.btn-secondary { background: hsl(var(--secondary)); color: hsl(var(--secondary-foreground)); }
.btn-secondary:hover { background: hsl(var(--secondary) / .8); }
.btn-neutral { background: hsl(var(--background)); color: hsl(var(--foreground)); border-color: hsl(var(--input)); }
.btn-neutral:hover { background: hsl(var(--muted)); }
.btn-destructive { background: hsl(var(--destructive)); color: hsl(var(--destructive-foreground)); }
.btn-destructive:hover { background: hsl(var(--destructive) / .9); }
.btn-destructive-outline { background: hsl(var(--destructive-surface)); color: hsl(var(--destructive)); border-color: hsl(var(--destructive)); }
.btn-destructive-outline:hover { background: hsl(var(--destructive)); color: hsl(var(--destructive-foreground)); }
.btn.loading { color: transparent !important; }
.btn.loading::after {
  content: ""; position: absolute; width: 16px; height: 16px; border-radius: 9999px; opacity: .7;
  border: 4px solid hsl(var(--primary-foreground)); border-top-color: transparent; animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Inputs (Input.jsx, Select.jsx, Label) ---------- */
.label { display: block; font-size: 14px; font-weight: 500; line-height: 20px; margin-bottom: 6px; color: hsl(var(--foreground)); }
.label .req { color: hsl(var(--destructive)); }
.input, .select, .textarea {
  display: block; width: 100%; height: 40px; padding: 8px 12px; border-radius: 6px;
  border: 1px solid hsl(var(--input)); background: hsl(var(--background)); color: hsl(var(--foreground));
  font: inherit; font-size: 14px; line-height: 24px; transition: border-color .15s; outline: none;
}
.input::placeholder, .textarea::placeholder { color: hsl(var(--muted-foreground)); }
.input:focus, .select:focus, .textarea:focus { border-color: hsl(var(--muted-foreground)); }
.input:disabled, .select:disabled { opacity: .45; cursor: not-allowed; }
.input-sm, .select.input-sm { height: 32px; padding: 4px 10px; font-size: 12px; line-height: 20px; }
.textarea { height: auto; min-height: 80px; resize: vertical; }
.select { appearance: none; padding-right: 32px; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, hsl(var(--muted-foreground)) 50%), linear-gradient(135deg, hsl(var(--muted-foreground)) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
.field { display: flex; flex-direction: column; min-width: 0; }
.field .hint { font-size: 12px; line-height: 20px; color: hsl(var(--muted-foreground)); margin-top: 4px; }
.form-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); align-items: end; }
input[type="checkbox"] { accent-color: hsl(var(--primary)); width: 16px; height: 16px; }

/* ---------- Badge (Badge.jsx) ---------- */
.badge {
  display: inline-flex; align-items: center; border-radius: 9999px; border: 1px solid hsl(var(--border));
  padding: 2px 10px; font-size: 12px; font-weight: 600; line-height: 16px; white-space: nowrap;
  background: hsl(var(--background)); color: hsl(var(--foreground));
}
.badge-primary { border-color: hsl(var(--primary)); background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.badge-secondary { border-color: hsl(var(--secondary)); background: hsl(var(--secondary)); color: hsl(var(--secondary-foreground)); }
.badge-info { border-color: hsl(var(--info-border)); background: hsl(var(--info-surface)); color: hsl(var(--info)); }
.badge-success { border-color: hsl(var(--success-border)); background: hsl(var(--success-surface)); color: hsl(var(--success)); }
.badge-warning { border-color: hsl(var(--warning-border)); background: hsl(var(--warning-surface)); color: hsl(var(--warning)); }
.badge-destructive { border-color: hsl(var(--destructive-border)); background: hsl(var(--destructive-surface)); color: hsl(var(--destructive)); }
.pill { display: inline-flex; border-radius: 9999px; border: 1px solid hsl(var(--border)); background: hsl(var(--muted)); padding: 4px 12px; font-size: 12px; font-weight: 500; color: hsl(var(--muted-foreground)); }

/* ---------- Alert (Alert.jsx) ---------- */
.alert { position: relative; width: 100%; border-radius: 8px; border: 1px solid hsl(var(--info-border)); padding: 16px; background: hsl(var(--info-surface)); color: hsl(var(--info)); }
.alert .alert-title { font-size: 16px; font-weight: 600; line-height: 1.25; }
.alert .alert-body { margin-top: 8px; font-size: 14px; line-height: 24px; color: hsl(var(--foreground)); }
.alert-success { border-color: hsl(var(--success-border)); background: hsl(var(--success-surface)); color: hsl(var(--success)); }
.alert-warning { border-color: hsl(var(--warning-border)); background: hsl(var(--warning-surface)); color: hsl(var(--warning)); }
.alert-destructive { border-color: hsl(var(--destructive-border)); background: hsl(var(--destructive-surface)); color: hsl(var(--destructive)); }

/* ---------- Card / Surface (Surface.jsx, Card.jsx) ---------- */
.card { overflow: hidden; border: 1px solid hsl(var(--border)); background: hsl(var(--background)); color: hsl(var(--foreground)); border-radius: 8px; padding: 12px 16px; }
.section .card { background: hsl(var(--surface)); }
.card-muted { background: hsl(var(--muted)); border-color: hsl(var(--border)); }
.card-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.stat .stat-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: hsl(var(--muted-foreground)); }
.stat .stat-value { font-size: 24px; font-weight: 600; line-height: 1.25; margin-top: 2px; }
.stat .stat-hint { font-size: 12px; color: hsl(var(--muted-foreground)); }
.preview { min-height: 120px; border-radius: 16px; border: 1px dashed hsl(var(--border)); background: hsl(var(--muted)); padding: 20px; }

/* ---------- Table (Table.jsx) ---------- */
.table-wrap { position: relative; width: 100%; overflow: auto; background: hsl(var(--background)); border: 1px solid hsl(var(--border)); border-radius: 8px; }
table.table { width: 100%; border-collapse: collapse; font-size: 14px; line-height: 1.5; }
.table thead tr { background: hsl(var(--muted)); }
.table th { height: 48px; padding: 0 16px; text-align: left; vertical-align: middle; font-weight: 500; color: hsl(var(--muted-foreground)); white-space: nowrap; }
.table td { padding: 12px 16px; vertical-align: top; border-top: 1px solid hsl(var(--border)); }
.table tbody tr.clickable:hover td, .table tbody tr:hover td { background: hsl(var(--muted)); }
.table td:first-child { white-space: nowrap; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .empty { text-align: center; color: hsl(var(--muted-foreground)); padding: 32px; }

/* ---------- Tabs (Tabs.jsx) ---------- */
.tabs { display: inline-flex; height: 44px; align-items: center; border-radius: 8px; background: hsl(var(--muted)); padding: 4px; color: hsl(var(--muted-foreground)); }
.tabs button, .tabs a { display: inline-flex; align-items: center; white-space: nowrap; border-radius: 6px; padding: 6px 12px;
  border: 0; background: transparent; color: inherit; font: inherit; font-size: 14px; font-weight: 500; cursor: pointer; text-decoration: none; }
.tabs [aria-selected="true"], .tabs .active { background: hsl(var(--background)); color: hsl(var(--foreground)); }

/* ---------- Code block (as chart-generator `pre`: page background, 1px border) ---------- */
.code { overflow: hidden; border-radius: 8px; border: 1px solid hsl(var(--border)); background: hsl(var(--background)); }
.code .code-head { display: flex; align-items: center; justify-content: space-between; gap: 8px;
  border-bottom: 1px solid hsl(var(--border)); padding: 6px 16px; color: hsl(var(--muted-foreground));
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.code pre { margin: 0; padding: 16px; overflow: auto; font-size: 13px; line-height: 24px; color: hsl(var(--foreground)); background: transparent; }
.code .code-head button { background: hsl(var(--background)); border: 1px solid hsl(var(--input)); color: hsl(var(--foreground)); border-radius: 6px; padding: 2px 10px; font: inherit; font-size: 12px; text-transform: none; letter-spacing: 0; cursor: pointer; }
.code .code-head button:hover { background: hsl(var(--muted)); }
/* JSON highlighting from the theme tokens, so it reads in both themes */
.tok-key { color: hsl(var(--primary)); } .tok-str { color: hsl(var(--success)); }
.tok-num { color: hsl(var(--info)); } .tok-lit { color: hsl(var(--warning)); }

/* ---------- Pagination / misc ---------- */
.pagination { display: flex; gap: 8px; align-items: center; }
.pagination a { height: 36px; display: inline-flex; align-items: center; padding: 0 12px; border-radius: 8px; border: 1px solid transparent; color: hsl(var(--muted-foreground)); }
.pagination a:hover { border-color: hsl(var(--input)); background: hsl(var(--background)); color: hsl(var(--foreground)); text-decoration: none; }
.secret { font-family: var(--font-mono); font-size: 13px; word-break: break-all; padding: 12px; border-radius: 8px; border: 1px dashed hsl(var(--warning-border)); background: hsl(var(--background)); color: hsl(var(--foreground)); }
.chart svg { width: 100%; height: auto; display: block; }
.chart text { fill: hsl(var(--muted-foreground)); font-size: 11px; font-family: var(--font-sans); }
.chart .grid-line { stroke: hsl(var(--border)); }
/* Standalone pages (sign-in, errors): top bar + one centred section */
.standalone { min-height: calc(100vh - 50px); display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.standalone > .section { width: 100%; max-width: 520px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
