:root {
  color-scheme: light dark;
  --violet: #7c3aed;
  --rose: #f43f5e;
  --grad: linear-gradient(135deg, #7c3aed, #f43f5e);
  --bg: #f6f7fb;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e8eaf2;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 8px 24px rgba(15, 23, 42, 0.06);
}

:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1120;
  --card: #131b2e;
  --ink: #e2e8f0;
  --muted: #8b9bb8;
  --line: #223050;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #0b1120;
    --card: #131b2e;
    --ink: #e2e8f0;
    --muted: #8b9bb8;
    --line: #223050;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; margin: 0; }
html { min-height: 100%; background: var(--bg); }
body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Inter", system-ui, sans-serif;
  line-height: 1.5;
}
img { max-width: 100%; }
button, input, select { font: inherit; }
button, a, input, select, summary { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
[hidden] { display: none !important; }

:where(a, button, input, select, summary):focus-visible {
  outline: 3px solid var(--violet);
  outline-offset: 3px;
}

.visually-hidden:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  white-space: nowrap !important;
}

.skip-link {
  position: fixed;
  top: 0.5rem;
  left: 0.5rem;
  z-index: 100;
  padding: 0.6rem 0.9rem;
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow);
}
.skip-link:not(:focus) { clip-path: inset(50%); width: 1px; height: 1px; overflow: hidden; }

.topnav {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.7rem 1.25rem;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.topnav .brand { display: flex; align-items: center; gap: 0.5rem; text-decoration: none; }
.topnav .brand img { width: 30px; height: 30px; object-fit: contain; }
.topnav .brand b {
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.topnav .links { display: flex; gap: 1.25rem; margin-left: auto; align-items: center; }
.topnav a:not(.brand) { color: var(--muted); text-decoration: none; font-size: 0.9rem; font-weight: 600; }
.topnav a.active { color: var(--ink); text-decoration: underline; text-decoration-color: var(--violet); text-underline-offset: 0.35rem; }
.theme-btn, .logout-btn {
  min-width: 36px;
  min-height: 36px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: 999px;
  padding: 0.35rem 0.7rem;
  font-size: 0.85rem;
}
.logout-btn { border-radius: 10px; font-weight: 600; }
.logout-form { display: flex; }

main.container { max-width: 1080px; margin: 0 auto; padding: 1.5rem 1.25rem 6rem; }
main.container.narrow { max-width: 720px; }
section { margin-top: 1.5rem; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 1.25rem;
  box-shadow: var(--shadow);
}
.card h2 { font-size: 1rem; font-weight: 700; letter-spacing: -0.01em; margin-bottom: 0.25rem; }
.card .sub { color: var(--muted); font-size: 0.82rem; margin-bottom: 1rem; }
.muted, .hint, .empty { color: var(--muted); }
.hint { font-size: 0.75rem; margin-top: 0.6rem; }
.empty { padding: 1rem 0; }

.hero {
  background: var(--grad);
  border-radius: 24px;
  color: #fff;
  padding: 1.75rem;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 1.25rem;
  align-items: center;
  box-shadow: 0 16px 40px rgba(124, 58, 237, 0.35);
}
.hero small { opacity: 0.85; font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase; font-size: 0.72rem; }
.hero .big { font-size: 3.2rem; font-weight: 800; letter-spacing: -0.03em; line-height: 1.05; }
.hero .big span { font-size: 1.3rem; font-weight: 600; opacity: 0.85; }
.hero .big .hero-value { font-size: inherit; font-weight: inherit; opacity: 1; }
.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.9rem; }
.chip {
  background: rgba(255, 255, 255, 0.22);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
}
.ring { position: relative; width: 128px; height: 128px; }
.ring svg { transform: rotate(-90deg); }
.ring .ring-label { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .ring-label b { font-size: 1.35rem; }
.ring .ring-label small { opacity: 0.85; font-size: 0.7rem; text-transform: none; }

canvas#chart { width: 100%; height: 260px; display: block; }
.legend { display: flex; flex-wrap: wrap; gap: 1.1rem; margin-top: 0.75rem; font-size: 0.78rem; color: var(--muted); font-weight: 600; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 0.35rem; vertical-align: -1px; }
.chart-description { margin-top: 0.75rem; color: var(--muted); font-size: 0.78rem; }
.chart-description a { color: inherit; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 0.85rem; }
.metric {
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 0.9rem 1rem;
  box-shadow: var(--shadow);
  transition: transform 0.12s, border-color 0.12s;
}
.metric:hover { transform: translateY(-2px); border-color: var(--violet); }
.metric small { color: var(--muted); font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.metric .val { font-size: 1.3rem; font-weight: 800; letter-spacing: -0.02em; margin-top: 0.15rem; overflow-wrap: anywhere; }
.metric .val .unit { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.tag {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1.5;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  margin-left: 0.35rem;
  vertical-align: 2px;
}
.tag.ok, .pill { background: #dcfce7; color: #15803d; }
.tag.warn { background: #fef3c7; color: #92400e; }
.tag.bad { background: #ffe4e6; color: #be123c; }
[data-theme="dark"] .tag.ok, [data-theme="dark"] .pill { background: #14532d99; color: #86efac; }
[data-theme="dark"] .tag.warn { background: #78350f99; color: #fcd34d; }
[data-theme="dark"] .tag.bad { background: #88133799; color: #fda4af; }
.spark { margin-top: 0.5rem; display: block; width: 100%; height: 30px; }

form.add, .fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0.75rem; }
.fields { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 0.85rem; }
form.add > div, .fields > div { min-width: 0; }
form.add label, .fields label, .login-card label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.25rem;
}
form.add input, form.add select, .fields input, .fields select, .search {
  width: 100%;
  min-height: 48px;
  padding: 0.7rem 0.8rem;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-size: max(1rem, 1em);
  font-weight: 600;
}
form.add input:focus, form.add select:focus, .fields input:focus, .fields select:focus, .search:focus {
  outline: none;
  border-color: var(--violet);
  box-shadow: 0 0 0 4px rgba(124, 58, 237, 0.18);
}
form.add .actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin-top: 0.35rem; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  border: none;
  border-radius: 12px;
  padding: 0.8rem 1.5rem;
  background: var(--grad);
  color: #fff;
  font-weight: 700;
  text-decoration: none;
  box-shadow: 0 8px 20px rgba(124, 58, 237, 0.3);
}
.btn.ghost { background: transparent; color: var(--violet); border: 1.5px solid var(--violet); box-shadow: none; }
[data-theme="dark"] .btn.ghost { color: #c4b5fd; border-color: #c4b5fd; }
#log:target { border-color: var(--violet); box-shadow: 0 0 0 4px rgba(124, 58, 237, 0.18), var(--shadow); }

.error-box, .success-box {
  border-radius: 12px;
  padding: 0.85rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
}
.error-box { background: #ffe4e6; color: #9f1239; border: 1px solid #fda4af; }
.success-box { background: #dcfce7; color: #166534; border: 1px solid #86efac; }
[data-theme="dark"] .error-box { background: #88133766; color: #fecdd3; border-color: #be123c; }
[data-theme="dark"] .success-box { background: #14532d66; color: #bbf7d0; border-color: #15803d; }
.error-box ul { padding-left: 1.25rem; }

.strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0.85rem; margin-bottom: 1.5rem; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 0.9rem 1rem; box-shadow: var(--shadow); }
.stat small { color: var(--muted); font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.stat .val { font-size: 1.3rem; font-weight: 800; letter-spacing: -0.02em; margin-top: 0.15rem; }
.stat .val span { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.table-card { padding: 0; overflow: hidden; contain: paint; }
.table-card header { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; padding: 1.1rem 1.25rem 0.9rem; }
.table-card header .spacer { margin-left: auto; }
.search { width: 220px; min-height: 42px; padding: 0.45rem 0.8rem; font-size: 0.85rem; }
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; white-space: nowrap; }
thead th {
  text-align: right;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: var(--card);
  padding: 0.6rem 0.9rem;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
thead th:first-child, tbody td:first-child { text-align: left; padding-left: 1.25rem; }
tbody td { text-align: right; padding: 0.65rem 0.9rem; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(124, 58, 237, 0.05); }
td.date { font-weight: 700; }
td .rel { display: block; font-size: 0.7rem; color: var(--muted); font-weight: 500; }
td.w { font-weight: 800; }
.delta { font-size: 0.75rem; font-weight: 700; padding: 0.08rem 0.45rem; border-radius: 999px; }
.delta.down { background: #dcfce7; color: #15803d; }
.delta.up { background: #ffe4e6; color: #be123c; }
.delta.flat { background: var(--line); color: var(--muted); }
[data-theme="dark"] .delta.down { background: #14532d99; color: #86efac; }
[data-theme="dark"] .delta.up { background: #88133799; color: #fda4af; }
.rowbtn { display: inline-flex; align-items: center; justify-content: center; min-width: 32px; min-height: 32px; border: none; background: transparent; color: var(--ink); text-decoration: none; padding: 0.15rem 0.35rem; border-radius: 8px; opacity: 0.7; }
.rowbtn:hover { opacity: 1; background: rgba(124, 58, 237, 0.12); }
.actions-cell { white-space: nowrap; }
.actions-cell form { display: inline; }

.settings-card { margin-bottom: 1.25rem; margin-top: 0; }
.settings-card .btn { margin-top: 1rem; }
.rowline { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; padding: 0.7rem 0; border-bottom: 1px solid var(--line); }
.rowline:last-child { border-bottom: none; }
.rowline .grow { flex: 1; min-width: 180px; }
.rowline b { font-size: 0.9rem; }
.rowline small { display: block; color: var(--muted); font-size: 0.75rem; margin-top: 0.1rem; }
.pill { font-size: 0.7rem; font-weight: 700; padding: 0.15rem 0.6rem; border-radius: 999px; }
.pill.soon { background: var(--line); color: var(--muted); }
.mini {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border: 1.5px solid var(--line);
  background: transparent;
  color: var(--ink);
  text-decoration: none;
  border-radius: 10px;
  padding: 0.4rem 0.9rem;
  font-size: 0.82rem;
  font-weight: 600;
}
.mini:hover { border-color: var(--violet); color: var(--violet); }
.password-change { margin-top: 0.75rem; }
.password-change summary { display: inline-flex; list-style: none; cursor: pointer; }
.password-change summary::-webkit-details-marker { display: none; }
.password-change form { margin-top: 1rem; }

.bottomnav {
  display: none;
  position: fixed;
  top: auto;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding: 0.5rem 1rem calc(0.5rem + env(safe-area-inset-bottom));
  justify-content: space-around;
}
.bottomnav a { min-height: 48px; color: var(--muted); text-decoration: none; font-size: 0.72rem; font-weight: 600; text-align: center; }
.bottomnav a.active { color: var(--violet); }
.bottomnav a.active .ico { text-decoration: underline; }
.bottomnav .ico { font-size: 1.2rem; display: block; }

.login-page {
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(60rem 60rem at 110% -10%, rgba(244, 63, 94, 0.14), transparent 60%),
    radial-gradient(50rem 50rem at -10% 110%, rgba(124, 58, 237, 0.16), transparent 60%),
    #0f172a;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: #fff;
  color: #0f172a;
  border-radius: 24px;
  padding: 2.5rem 2rem 2rem;
  box-shadow: 0 24px 60px rgba(2, 6, 23, 0.45);
  text-align: center;
}
.login-card .mark { width: 84px; height: 84px; object-fit: contain; }
.login-card h1 {
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin-top: 0.5rem;
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.login-card .sub { color: #64748b; font-size: 0.9rem; margin: 0.35rem 0 1.75rem; }
.login-card form { text-align: left; }
.login-card input:not([type="hidden"]) {
  width: 100%;
  min-height: 48px;
  padding: 0.8rem 1rem;
  border: 1.5px solid #cbd5e1;
  border-radius: 12px;
  font-size: 1rem;
  margin-bottom: 1rem;
  background: #fff;
  color: #0f172a;
}
.login-card input:focus { outline: none; border-color: var(--violet); box-shadow: 0 0 0 4px rgba(124, 58, 237, 0.12); }
.login-card .btn { width: 100%; }
.login-card .passkey-btn { margin-top: 0.75rem; box-shadow: none; }
.login-card .hint { color: #64748b; margin-top: 1.25rem; }
.login-error { text-align: left; }

@media (max-width: 720px) {
  .topnav { gap: 0.75rem; padding-inline: 0.85rem; }
  .topnav .links { gap: 0.65rem; }
  .topnav .links > a:not(.active) { display: none; }
  .logout-btn { padding-inline: 0.55rem; }
  .hero { grid-template-columns: 1fr; }
  .ring { display: none; }
  .bottomnav { display: flex; }
  .search { width: 150px; }
}

@media (max-width: 420px) {
  main.container { padding-inline: 0.85rem; }
  .hero { padding: 1.35rem; }
  .hero .big { font-size: 2.8rem; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric { padding-inline: 0.75rem; }
  .topnav .brand b { font-size: 1.1rem; }
  .topnav a:not(.brand) { font-size: 0.8rem; }
  .logout-btn { font-size: 0.75rem; }
}

@media (pointer: coarse) {
  input, select { min-height: 52px; }
  .rowbtn { min-width: 44px; min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}

@media (forced-colors: active) {
  .btn, .hero, .chip, .tag, .delta, .pill { border: 1px solid ButtonText; }
  .topnav a.active, .bottomnav a.active { text-decoration: underline; }
}
