:root {
  --bg: #08101f;
  --bg-soft: #0f172a;
  --card: rgba(15, 23, 42, 0.9);
  --border: rgba(148, 163, 184, 0.18);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #60a5fa;
  --accent-2: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --bg-soft: #e2e8f0;
  --card: rgba(255, 255, 255, 0.92);
  --border: rgba(100, 116, 139, 0.22);
  --text: #1e293b;
  --muted: #64748b;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f1f5f9;
    --bg-soft: #e2e8f0;
    --card: rgba(255, 255, 255, 0.92);
    --border: rgba(100, 116, 139, 0.22);
    --text: #1e293b;
    --muted: #64748b;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(37, 99, 235, 0.2), transparent 0, transparent 35%),
    radial-gradient(circle at top right, rgba(34, 197, 94, 0.16), transparent 0, transparent 30%),
    var(--bg);
}

.page {
  max-width: 1180px;
  margin: 0 auto;
  padding: 16px 18px 48px;
}

/* ── Top bar ── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}

.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  min-width: 0;
}

.topbar-icon {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
}

.topbar-title {
  margin: 0;
  font-size: 1.15rem;
  white-space: nowrap;
}

.topbar-sync {
  margin: 0;
  font-size: 0.78rem;
}

.burger {
  background: none;
  border: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 8px;
  flex-shrink: 0;
}

.burger span {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: transform 0.2s, opacity 0.2s;
}

.burger.open span:nth-child(1) {
  transform: translateY(7px) rotate(45deg);
}

.burger.open span:nth-child(2) {
  opacity: 0;
}

.burger.open span:nth-child(3) {
  transform: translateY(-7px) rotate(-45deg);
}

/* ── Drawer ── */

.drawer {
  position: fixed;
  top: 0;
  right: -320px;
  width: 300px;
  max-width: 85vw;
  height: 100vh;
  z-index: 200;
  background: var(--card);
  border-left: 1px solid var(--border);
  padding: 60px 20px 20px;
  overflow-y: auto;
  transition: right 0.25s ease;
  display: flex;
  flex-direction: column;
}

.drawer.open {
  right: 0;
}

.drawer-overlay {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.drawer-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.drawer-section {
  margin-bottom: 20px;
}

.drawer-heading {
  margin: 0 0 8px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  font-weight: 700;
}

.drawer-footer {
  margin-top: auto;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.78rem;
}

.drawer-footer-actions {
  display: flex;
  gap: 6px;
}

.update-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 0.78rem;
  color: var(--accent);
  cursor: pointer;
  transition: background 0.15s;
}

.update-btn:hover {
  background: rgba(96, 165, 250, 0.12);
}

.update-btn svg {
  flex-shrink: 0;
}

.update-btn.spinning svg {
  animation: spin 0.8s linear infinite;
}

.update-btn.has-update {
  color: var(--accent-2);
  border-color: var(--accent-2);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.toggle-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toggle-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  user-select: none;
}

.toggle-item .drag-handle {
  cursor: grab;
  font-size: 1rem;
  line-height: 1;
  opacity: 0.4;
  touch-action: none;
}

.toggle-item .drag-handle:active {
  cursor: grabbing;
}

.toggle-item.dragging {
  opacity: 0.35;
}

.toggle-item.dragging-touch {
  background: var(--card);
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  border-radius: 10px;
}

.toggle-placeholder {
  border: 2px dashed var(--border);
  border-radius: 10px;
}

.toggle-item:has(input:checked) {
  background: rgba(96, 165, 250, 0.14);
  color: var(--text);
}

.toggle-item input[type="checkbox"] {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ── Login modal ── */

.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(6px);
  padding: 20px;
}

.login-overlay.hidden {
  display: none;
}

.login-modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 32px 28px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.login-icon {
  width: 48px;
  height: 48px;
  align-self: center;
}

.login-modal h2 {
  margin: 0;
  text-align: center;
  font-size: 1.25rem;
}

.login-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--muted);
}

.login-field input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-soft);
  color: var(--text);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.15s;
}

.login-field input:focus {
  border-color: var(--accent);
}

.login-error {
  margin: 0;
  font-size: 0.85rem;
  color: var(--danger);
  text-align: center;
}

.login-submit {
  padding: 10px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}

.login-submit:hover {
  opacity: 0.85;
}

.login-submit:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.status-pill {
  border-radius: 999px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  font-size: 0.9rem;
  font-weight: 600;
}

.status-pill.info {
  background: rgba(96, 165, 250, 0.16);
}

.status-pill.success {
  background: rgba(34, 197, 94, 0.16);
}

.status-pill.warning {
  background: rgba(245, 158, 11, 0.16);
}

.status-pill.error {
  background: rgba(239, 68, 68, 0.16);
}

.muted {
  color: var(--muted);
}

/* ── Period selector ── */

.period-bar {
  max-width: 1180px;
  margin: 0 auto 12px;
  padding: 0 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.period-buttons {
  display: flex;
  gap: 4px;
}

.period-btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: background 0.15s, border-color 0.15s;
}

.period-btn:hover {
  border-color: var(--accent);
}

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

.period-custom {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.period-custom.hidden {
  display: none;
}

.period-custom label {
  font-size: 0.85rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 4px;
}

.period-custom input[type="date"] {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 0.85rem;
}

.period-apply {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 6px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
}

.period-apply:hover {
  opacity: 0.85;
}

.card-grid {
  display: grid;
  gap: 16px;
}

.metrics {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin-bottom: 16px;
}

.panels {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-bottom: 16px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 12px 40px rgba(15, 23, 42, 0.2);
  overflow: hidden;
}

.metric-card {
  padding: 18px;
}

.metric-card .label {
  margin: 0 0 8px;
  font-size: 0.9rem;
  color: var(--muted);
}

.metric-card h2 {
  margin: 0 0 8px;
  font-size: clamp(1.5rem, 2vw, 2rem);
}

.panel {
  padding: 18px;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
}

.panel-header h3 {
  margin: 0 0 4px;
}

/* ── Chart wrapper with Y-axis ── */

.chart-wrapper {
  display: flex;
  position: relative;
}

.y-axis {
  width: 42px;
  min-width: 42px;
  flex-shrink: 0;
  position: relative;
  align-self: flex-end;
  pointer-events: none;
}

.y-tick {
  position: absolute;
  right: 4px;
  font-size: 0.6rem;
  color: var(--muted);
  line-height: 1;
  transform: translateY(50%);
  white-space: nowrap;
  text-align: right;
}

.y-unit {
  position: absolute;
  right: 4px;
  font-size: 0.5rem;
  color: var(--muted);
  opacity: 0.7;
  white-space: nowrap;
  text-align: right;
}

.grid-overlay {
  position: absolute;
  left: 42px;
  right: 0;
  bottom: 0;
  top: 0;
  pointer-events: none;
  z-index: 1;
}

.grid-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 0;
  border-top: 1px solid var(--muted);
  opacity: 0.25;
  pointer-events: none;
}

.avg-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 0;
  border-top: 1.5px dashed var(--warning);
  pointer-events: none;
  z-index: 1;
}

.avg-label {
  position: absolute;
  right: 4px;
  bottom: 2px;
  font-size: 0.6rem;
  color: var(--warning);
  white-space: nowrap;
  font-weight: 600;
  background: var(--card);
  padding: 0 4px;
  border-radius: 2px;
}

.bar-chart {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(12px, 28px);
  gap: 8px;
  align-items: end;
  min-height: 196px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0 14px;
  flex: 1;
  min-width: 0;
  position: relative;
}

.bar-item {
  display: grid;
  gap: 6px;
  min-width: 12px;
  justify-items: center;
  position: relative;
}

.bar-item span {
  font-size: 0.68rem;
  color: var(--muted);
  writing-mode: vertical-rl;
  transform: rotate(180deg);
}

.bar-item .bar-date {
  position: absolute;
  top: calc(100% + 2px);
  left: 50%;
  transform: translateX(-50%);
  writing-mode: horizontal-tb;
  font-size: 0.58rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  line-height: 1;
}

.bar-item .bar-date-empty {
  display: none;
}

/* Midnight marker for simple bar charts */
.bar-item.bar-midnight::before,
.bar-col.bar-midnight::before {
  content: "";
  position: absolute;
  left: -5px;
  bottom: 0;
  height: 300px;
  width: 2px;
  background: var(--accent);
  opacity: 0.4;
  pointer-events: none;
  z-index: 1;
}

.bar-col .bar-date {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.58rem;
  font-weight: 600;
  color: var(--accent);
  line-height: 1;
  white-space: nowrap;
  margin-top: 2px;
  height: 10px;
}

.bar-col .bar-date-empty {
  visibility: hidden;
}

.bar {
  width: 100%;
  min-height: 10px;
  border-radius: 10px 10px 4px 4px;
}

.bar-price {
  background: linear-gradient(180deg, #93c5fd, #2563eb);
}

.bar-consumption {
  background: linear-gradient(180deg, #6ee7b7, #059669);
}

.bar-homey {
  background: linear-gradient(180deg, #c4b5fd, #7c3aed);
}

.bar-ex-ev {
  background: linear-gradient(180deg, #a7f3d0, #10b981);
}

.bar-ev {
  background: linear-gradient(180deg, #fda4af, #e11d48);
}

.bar-cost {
  background: linear-gradient(180deg, #fcd34d, #d97706);
}

.bar-negative {
  border-radius: 4px 4px 10px 10px;
  opacity: 0.7;
}

.bar-col {
  position: relative;
  min-width: 12px;
}

.bar-col .bar-zero {
  position: absolute;
  left: -1px;
  right: -1px;
  height: 1px;
  background: var(--muted);
  opacity: 0.5;
  pointer-events: none;
}

.bar-col-label {
  position: absolute;
  bottom: 2px;
  left: 50%;
  transform: translateX(-50%) rotate(180deg);
  writing-mode: vertical-rl;
  font-size: 0.68rem;
  color: var(--muted);
  line-height: 1;
  white-space: nowrap;
}

.bar-now-highlight {
  position: relative;
}

.bar-now-highlight::before {
  content: "";
  position: absolute;
  top: -2px;
  bottom: -2px;
  left: -2px;
  right: -2px;
  border: 2px solid var(--accent);
  border-radius: 12px;
  pointer-events: none;
  opacity: 0.7;
}

.col-now {
  position: relative;
}

.col-now::before {
  content: "";
  position: absolute;
  top: -2px;
  bottom: 0;
  left: -2px;
  right: -2px;
  border: 2px solid var(--accent);
  border-radius: 8px;
  pointer-events: none;
  opacity: 0.7;
}

.stacked-col-v.col-now::before {
  top: 0;
  bottom: 0;
}

.panel-full {
  grid-column: 1 / -1;
}

.metric-inline {
  text-align: right;
  white-space: nowrap;
}

.metric-inline .value {
  font-size: 1.25rem;
  font-weight: 700;
}

.metric-inline .sub {
  font-size: 0.8rem;
  color: var(--muted);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  margin-bottom: 10px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  flex-shrink: 0;
}

.stacked-chart {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(18px, 1fr);
  gap: 4px;
  align-items: end;
  min-height: 220px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0 32px;
  flex: 1;
  min-width: 0;
}

.stacked-col {
  display: flex;
  flex-direction: column-reverse;
  align-items: stretch;
  min-width: 18px;
  position: relative;
}

.stacked-col .col-label {
  text-align: center;
  font-size: 0.58rem;
  color: var(--muted);
  margin-top: 4px;
  white-space: nowrap;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  margin-left: auto;
  margin-right: auto;
  line-height: 1;
}

.stacked-col .col-total {
  text-align: center;
  font-size: 0.58rem;
  color: var(--text);
  margin-bottom: 2px;
  opacity: 0;
  transition: opacity 0.15s;
}

.stacked-col:hover .col-total {
  opacity: 1;
}

.stacked-col-v {
  position: relative;
  min-width: 18px;
}

.stacked-col-v .seg-stack {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column-reverse;
  align-items: stretch;
}

.stacked-col-v .chart-zero {
  position: absolute;
  left: -1px;
  right: -1px;
  height: 1px;
  background: var(--muted);
  opacity: 0.6;
  pointer-events: none;
}

.stacked-col-v .col-total-v {
  position: absolute;
  top: 2px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 0.58rem;
  color: var(--text);
  opacity: 0;
  transition: opacity 0.15s;
}

.stacked-col-v:hover .col-total-v {
  opacity: 1;
}

.stacked-col-v .col-label-v {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%) rotate(180deg);
  writing-mode: vertical-rl;
  font-size: 0.58rem;
  color: var(--muted);
  line-height: 1;
  white-space: nowrap;
  margin-top: 4px;
}

/* Midnight marker and date label for stacked charts */
.stacked-col.col-midnight::before,
.stacked-col-v.col-midnight::before {
  content: "";
  position: absolute;
  left: -3px;
  bottom: 0;
  height: 300px;
  width: 2px;
  background: var(--accent);
  opacity: 0.4;
  pointer-events: none;
  z-index: 1;
}

.stacked-col .col-date {
  font-size: 0.58rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  line-height: 1;
  text-align: center;
  height: 10px;
}

.stacked-col .col-date-empty {
  visibility: hidden;
}

.stacked-col-v .col-date {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.58rem;
  font-weight: 600;
  color: var(--accent);
  line-height: 1;
  white-space: nowrap;
  margin-top: 2px;
  height: 10px;
}

.stacked-col-v .col-date-empty {
  visibility: hidden;
}

.stacked-seg {
  width: 100%;
  min-height: 1px;
}

.stacked-seg.seg-top {
  border-radius: 6px 6px 0 0;
}

.stacked-seg.seg-bottom {
  border-radius: 0 0 4px 4px;
}

.cheapest-list {
  display: grid;
  gap: 10px;
}

.cheapest-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(148, 163, 184, 0.08);
}

.cheapest-row strong {
  display: block;
}

.empty {
  color: var(--muted);
}

.footer {
  padding-top: 8px;
  text-align: center;
  font-size: 0.92rem;
}

.theme-switch {
  display: inline-flex;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  overflow: hidden;
}

.theme-switch button {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.78rem;
  padding: 6px 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.theme-switch button:hover {
  color: var(--text);
}

.theme-switch button.active {
  background: var(--accent);
  color: #fff;
}

@media (max-width: 920px) {
  .metrics,
  .panels {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 640px) {
  .page {
    padding: 12px 10px 28px;
  }

  .topbar-inner {
    padding: 8px 10px;
  }

  .topbar-title {
    font-size: 1rem;
  }

  .topbar-sync {
    display: none;
  }

  .metrics {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .metrics .metric-card:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  .panels {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .metric-card {
    padding: 12px;
  }

  .metric-card .label {
    font-size: 0.78rem;
    margin-bottom: 4px;
  }

  .metric-card h2 {
    font-size: 1.15rem;
    margin-bottom: 4px;
    word-break: break-word;
  }

  .metric-card .muted {
    font-size: 0.72rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .panel {
    padding: 14px 10px;
  }

  .panel-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .panel-header h3 {
    font-size: 0.95rem;
  }

  .panel-header .muted {
    font-size: 0.75rem;
  }

  .metric-inline {
    text-align: left;
  }

  .bar-chart {
    grid-auto-columns: 22px;
    gap: 3px;
    min-height: 140px;
  }

  .y-axis {
    width: 36px;
    min-width: 36px;
  }

  .y-tick {
    font-size: 0.5rem;
  }

  .y-unit {
    font-size: 0.42rem;
  }

  .bar-item span {
    font-size: 0.56rem;
  }

  .stacked-chart {
    grid-auto-columns: 22px;
    gap: 2px;
    min-height: 160px;
  }

  .stacked-col .col-label {
    font-size: 0.5rem;
  }

  .legend {
    gap: 6px 12px;
  }

  .legend-item {
    font-size: 0.7rem;
  }

  .cheapest-row {
    padding: 10px 12px;
    font-size: 0.88rem;
  }

  .status-pill {
    font-size: 0.78rem;
    padding: 6px 10px;
  }

  .footer {
    font-size: 0.8rem;
  }
}
