:root {
  --bg: #0f1419;
  --bg-elevated: #171e27;
  --bg-row: #1a2330;
  --bg-row-hover: #223040;
  --border: #2a3644;
  --text: #e8eef4;
  --text-muted: #8b9aab;
  --accent: #3dd6c6;
  --accent-dim: rgba(61, 214, 198, 0.15);
  --danger: #ff6b6b;
  --warn: #f0b429;
  --ok: #3dd6c6;
  --paused: #6b7a8a;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: "SF Pro Text", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

body {
  overscroll-behavior-y: contain;
}

#app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 720px;
  margin: 0 auto;
  position: relative;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: rgba(15, 20, 25, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.brand {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}

.screen-title {
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  gap: 4px;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn:active {
  background: var(--bg-row);
}

.view-bar {
  display: flex;
  gap: 6px;
  padding: 10px 12px 0;
  background: var(--bg);
  position: sticky;
  top: calc(64px + var(--safe-top));
  z-index: 16;
}

.view-btn {
  flex: 1;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.view-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.period-bar {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: calc(64px + var(--safe-top));
  z-index: 15;
}

.view-bar:not([hidden]) ~ .period-bar {
  top: calc(114px + var(--safe-top));
}

.period-btn {
  flex: 1;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.period-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.main {
  flex: 1;
  padding: 8px 12px calc(24px + var(--safe-bottom));
}

/* Settings */
.settings {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-top: 12px;
}

.settings h2 {
  margin: 0;
  font-size: 22px;
}

.settings p {
  margin: 0;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.45;
}

label.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

input,
select,
textarea {
  width: 100%;
  min-height: 48px;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 16px;
  font-family: var(--mono);
}

textarea {
  min-height: 96px;
  resize: vertical;
}

.btn {
  min-height: 48px;
  border: none;
  border-radius: var(--radius);
  padding: 0 18px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary {
  background: var(--accent);
  color: #0a1210;
}

.btn-primary:active {
  filter: brightness(0.92);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* Lists */
.list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 14px;
  background: var(--bg-row);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
}

.row:active {
  background: var(--bg-row-hover);
}

.row-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.row-name {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.25;
  word-break: break-word;
}

.row-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.metric strong {
  color: var(--text);
  font-weight: 600;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.badge.active {
  color: var(--ok);
}

.badge.paused {
  color: var(--paused);
}

.badge.error {
  color: var(--danger);
}

.row-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chevron {
  color: var(--text-muted);
  font-size: 18px;
  padding: 0 4px;
}

/* Toggle switch */
.switch {
  position: relative;
  width: 52px;
  height: 32px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.switch .slider {
  position: absolute;
  inset: 0;
  background: #3a4656;
  border-radius: 999px;
  transition: background 0.2s ease;
  cursor: pointer;
}

.switch .slider::before {
  content: "";
  position: absolute;
  width: 26px;
  height: 26px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s ease;
}

.switch input:checked + .slider {
  background: var(--accent);
}

.switch input:checked + .slider::before {
  transform: translateX(20px);
}

.switch input:disabled + .slider {
  opacity: 0.5;
}

/* States */
.state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}

.state h3 {
  margin: 0 0 8px;
  color: var(--text);
  font-size: 18px;
}

.state p {
  margin: 0 0 20px;
  font-size: 14px;
  line-height: 1.45;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 16px;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.summary-wide {
  grid-template-columns: repeat(2, 1fr);
}

.summary-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 10px;
  text-align: center;
}

.summary-card .label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}

.summary-card .value {
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.summary-card .sub {
  margin-top: 2px;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 10px;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.metrics-grid .metric strong {
  color: var(--text);
  font-weight: 600;
}

.filter-check {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  padding: 12px 14px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.filter-check input {
  width: 22px;
  height: 22px;
  min-height: 22px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.filter-check em {
  margin-left: auto;
  font-style: normal;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.level-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.level-tab {
  flex-shrink: 0;
  height: 34px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.level-tab.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: calc(100% - 24px);
  padding: 12px 16px;
  background: #243040;
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  font-size: 14px;
  animation: toast-in 0.2s ease;
}

.toast-undo {
  border: none;
  background: transparent;
  color: var(--accent);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  padding: 4px 0;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

.pull-hint {
  position: fixed;
  top: calc(8px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
}

.account-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 10px;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
}

.hint-box {
  margin-top: 8px;
  padding: 12px;
  border-radius: var(--radius);
  background: var(--bg-elevated);
  border: 1px dashed var(--border);
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.45;
}

.hint-box code {
  font-family: var(--mono);
  color: var(--accent);
  font-size: 11px;
}

/* Login */
.login {
  max-width: 400px;
  margin: 0 auto;
  padding: 48px 8px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-brand {
  font-size: 12px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 700;
}

.login h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.login-sub {
  margin: 0 0 8px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.5;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-error {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.settings h2 {
  margin-top: 8px;
}

.settings strong {
  color: var(--text);
}

/* Cabinet metrics */
.cab-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 4px;
}

.cab-metric {
  padding: 10px;
  border-radius: 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.cab-metric .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.cab-metric strong {
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.cab-metric em {
  font-style: normal;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.cab-metric.highlight {
  border-color: rgba(61, 214, 198, 0.35);
}

.cab-metric.purchases {
  border-color: rgba(240, 180, 41, 0.45);
  background: rgba(240, 180, 41, 0.08);
}

.cab-metric.purchases strong {
  color: var(--warn);
}

.purchases-card {
  border-color: rgba(240, 180, 41, 0.45);
  background: rgba(240, 180, 41, 0.08);
}

.purchases-card .value {
  color: var(--warn);
}

.purchases-metric strong {
  color: var(--warn);
}

.stats-warn {
  font-size: 12px;
  color: var(--danger);
  margin-top: 4px;
}

.progress-bar {
  position: relative;
  margin-bottom: 12px;
  height: 28px;
  border-radius: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  overflow: hidden;
}

.progress-fill {
  position: absolute;
  inset: 0 auto 0 0;
  background: var(--accent-dim);
  transition: width 0.2s ease;
}

.progress-bar span {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.row-cabinet .row-main {
  gap: 8px;
}

@media (min-width: 640px) {
  .cab-metrics {
    grid-template-columns: repeat(4, 1fr);
  }

  .summary-wide {
    grid-template-columns: repeat(4, 1fr);
  }
}
