@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Roboto:wght@300;400&family=Permanent+Marker&display=swap');

:root {
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

html {
  overflow-y: scroll;
}

* {
  box-sizing: border-box;
}

body {
  --bg-color: #f7f7f7;
  --panel-color: rgba(255, 255, 255, 0.85);
  --card-border: #e5e7eb;
  --muted: #4b5563;
  --text-color: #111827;
  --table-header: #f3f4f6;
  --shadow: rgba(15, 23, 42, 0.08);
  --button-bg: #2563eb;
  --button-bg-hover: #1d4ed8;
  --button-text: #ffffff;
  margin: 0;
  min-height: 100vh;
  padding: 2rem;
  background: var(--bg-color);
  color: var(--text-color);
  transition: background 0.3s ease, color 0.3s ease;
}

body[data-theme="dark"] {
  --bg-color: #0f172a;
  --panel-color: #111827;
  --card-border: #1f2937;
  --muted: #94a3b8;
  --text-color: #e2e8f0;
  --table-header: #1f2937;
  --shadow: rgba(0, 0, 0, 0.45);
  --button-bg: #334155;
  --button-bg-hover: #1e293b;
  --button-text: #f8fafc;
  color-scheme: dark;
}

.page {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
}

header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

h1 {
  margin: 0;
  font-size: clamp(1.5rem, 4vw, 2.25rem);
}

.title-row {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

#status {
  font-weight: 600;
  margin-top: 0.3rem;
  color: var(--muted);
}

.live-heart {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.4rem, 3.5vw, 1.9rem);
  width: 2.1rem;
  height: 2.1rem;
  line-height: 1;
  user-select: none;
  transition: color 0.2s ease;
}

.live-heart.is-live {
  color: #ef4444;
  animation: heartbeat 1s ease-in-out infinite;
}

.live-heart.is-connecting {
  color: #f59e0b;
  animation: heartbeat 1.25s ease-in-out infinite;
}

.live-heart.is-down {
  color: var(--muted);
  animation: none;
}

@keyframes heartbeat {
  0% { transform: scale(1); }
  25% { transform: scale(1.15); }
  40% { transform: scale(0.98); }
  60% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.view-switcher {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem;
  background: var(--panel-color);
  border: 1px solid var(--card-border);
  border-radius: 999px;
  box-shadow: 0 10px 25px var(--shadow);
}

.view-button {
  border: none;
  background: transparent;
  padding: 0.5rem 0.9rem;
  font-weight: 700;
  cursor: pointer;
  color: var(--muted);
  border-radius: 999px;
  transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
}

.view-button:hover {
  transform: translateY(-1px);
  background: var(--table-header);
}

.view-button.active {
  background: var(--button-bg);
  color: var(--button-text);
}

#theme-toggle {
  border: none;
  border-radius: 999px;
  padding: 0.65rem 1.25rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--button-bg);
  color: var(--button-text);
  box-shadow: 0 5px 15px var(--shadow);
  transition: transform 0.15s ease, background 0.2s ease;
}

#theme-toggle:hover {
  background: var(--button-bg-hover);
  transform: translateY(-1px);
}

#previews {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.preview {
  background: var(--panel-color);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 0.9rem 1rem;
  box-shadow: 0 10px 25px var(--shadow);
}


#meter-panel table th:nth-child(1),
#meter-panel table td:nth-child(1) {
  width: 29%;
}

#meter-panel table th:nth-child(2),
#meter-panel table td:nth-child(2) {
  width: 15%;
}

#meter-panel table th:nth-child(3),
#meter-panel table td:nth-child(3) {
  width: 8%;
}

#meter-panel table th:nth-child(4),
#meter-panel table td:nth-child(4) {
  width: 48%;
}

.preview.is-stale-warning {
  background: #fef9c3;
  border-color: #f59e0b;
}

.preview.is-stale-critical {
  background: #fee2e2;
  border-color: #dc2626;
}

.preview.is-stale-critical table td {
  color: #7f1d1d;
}

body[data-theme="dark"] .preview.is-stale-warning,
body[data-theme="dark"] .preview.is-stale-critical {
  background: var(--panel-color);
  border-color: var(--card-border);
  color: var(--text-color);
}

body[data-theme="dark"] .preview.is-stale-critical table td {
  color: inherit;
}

body[data-theme="dark"] .preview.is-stale-warning table thead th,
body[data-theme="dark"] .preview.is-stale-critical table thead th {
  background: var(--table-header);
  color: var(--muted);
}

.preview-title {
  font-size: 1rem;
  font-weight: 500;
}

.consumption-card .preview-title {
  font-weight: 600;
}

.meta {
  font-size: 0.875rem;
  color: var(--muted);
  margin-top: 0.25rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.82rem;
  letter-spacing: 0.01em;
  gap: 0.25rem;
}

.meta .badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  margin-right: 0.35rem;
  border-radius: 999px;
  background: rgba(37, 99, 235, 0.1);
  color: #1d4ed8;
  font-weight: 600;
  font-size: 0.78rem;
  letter-spacing: 0.01em;
}

.preview.is-stale-warning .meta .badge,
.preview.is-stale-critical .meta .badge {
  background: rgba(37, 99, 235, 0.1);
  color: #1d4ed8;
}

body[data-theme="dark"] .meta .badge {
  background: rgba(37, 99, 235, 0.1);
  color: #1d4ed8;
}

body[data-theme="dark"] .preview.is-stale-warning .meta .badge,
body[data-theme="dark"] .preview.is-stale-critical .meta .badge {
  background: rgba(37, 99, 235, 0.1);
  color: #1d4ed8;
}

.meta .badge-disabled {
  background: rgba(107, 114, 128, 0.15);
  color: #4b5563;
}

body[data-theme="dark"] .meta .badge-disabled {
  background: rgba(107, 114, 128, 0.2);
  color: #9ca3af;
}

table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  margin-top: 0.75rem;
  table-layout: fixed;
  border-radius: 8px;
  overflow: hidden;
}

th, td {
  border: 1px solid var(--card-border);
  border-left: none;
  border-top: none;
  padding: 0.5rem 0.65rem;
  width: 25%;
  word-break: break-word;
  font-size: 0.875rem;
  font-weight: 400;
  vertical-align: top;
}

th:first-child, td:first-child {
  border-left: 1px solid var(--card-border);
}

thead tr:first-child th {
  border-top: 1px solid var(--card-border);
}

thead tr:first-child th:first-child {
  border-top-left-radius: 8px;
}

thead tr:first-child th:last-child {
  border-top-right-radius: 8px;
}

tbody tr:last-child td:first-child {
  border-bottom-left-radius: 8px;
}

tbody tr:last-child td:last-child {
  border-bottom-right-radius: 8px;
}

th, td:not(.value-cell) {
  text-align: left;
}

.value-cell {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}

th {
  background: var(--table-header);
  font-weight: 500;
  font-size: 0.875rem;
  color: var(--muted);
}

.view-panel {
  margin-top: 0.5rem;
}

.view-panel[hidden] {
  display: none;
}


.logs-container {
  display: grid;
  gap: 0.75rem;
}

.preview.has-meter-warning,
.preview.has-meter-critical {
  background: var(--panel-color);
  border-color: var(--card-border);
  box-shadow:
    0 0 8px rgba(239, 68, 68, 0.4),
    0 0 16px rgba(239, 68, 68, 0.25),
    0 0 24px rgba(239, 68, 68, 0.1),
    0 14px 32px rgba(239, 68, 68, 0.25);
}

.status-icon {
  display: inline-block;
  font-size: 1.1rem;
  vertical-align: middle;
  margin-right: 0.35rem;
}

.log-table {
  margin-top: 0.5rem;
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  border: 1px solid var(--card-border);
  border-radius: 8px;
  overflow: hidden;
}

.log-table td {
  padding: 0.4rem 0.6rem;
  font-size: 0.875rem;
  word-break: break-word;
}

.log-table .error-message {
  color: var(--text-color);
  border-left: 3px solid var(--muted);
  padding-left: 0.75rem;
  line-height: 1.5;
}

.log-table .no-errors {
  color: var(--muted);
  font-style: italic;
}

.consumption-card {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.billing-card {
  margin-top: 1.5rem;
}

.consumption-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.5rem;
  align-items: baseline;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0.65rem;
  border-radius: 999px;
  background: var(--table-header);
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  min-width: 20ch;
  justify-content: space-between;
  text-align: right;
}

.consumption-table th:nth-child(2),
.consumption-table th:nth-child(3) {
  text-align: right;
}

.consumption-table .total-row td {
  font-weight: inherit;
  background: rgba(37, 99, 235, 0.07);
}

.consumption-table .meter-cell {
  font-weight: inherit;
}

.meter-cell-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}

.meter-label {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
}

.meter-marker {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.75rem;
  padding: 0 0.25rem;
  cursor: pointer;
}

/* Custom responsive tooltips */
[data-tooltip] {
  position: relative;
}

[data-tooltip]::before,
[data-tooltip]::after {
  position: absolute;
  z-index: 1000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

/* Tooltip text bubble */
[data-tooltip]::after {
  content: attr(data-tooltip);
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%) translateY(4px);
  padding: 0.4rem 0.65rem;
  border-radius: 6px;
  background: var(--text-color);
  color: var(--bg-color);
  font-size: 0.8rem;
  font-weight: 500;
  line-height: 1.3;
  white-space: nowrap;
  max-width: min(280px, 90vw);
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 4px 12px var(--shadow);
}

/* Tooltip arrow */
[data-tooltip]::before {
  content: '';
  bottom: calc(100% + 2px);
  left: 50%;
  transform: translateX(-50%) translateY(4px);
  border: 6px solid transparent;
  border-top-color: var(--text-color);
}

/* Show tooltip on click/tap only (prevents pulsing during data updates) */
[data-tooltip].tooltip-active::before,
[data-tooltip].tooltip-active::after {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Position adjustment for tooltips near edges */
[data-tooltip].tooltip-left::after {
  left: 0;
  transform: translateX(0) translateY(4px);
}

[data-tooltip].tooltip-left.tooltip-active::after {
  transform: translateX(0) translateY(0);
}

[data-tooltip].tooltip-left::before {
  left: 12px;
}

[data-tooltip].tooltip-right::after {
  left: auto;
  right: 0;
  transform: translateX(0) translateY(4px);
}

[data-tooltip].tooltip-right.tooltip-active::after {
  transform: translateX(0) translateY(0);
}

[data-tooltip].tooltip-right::before {
  left: auto;
  right: 12px;
  transform: translateX(0) translateY(4px);
}

[data-tooltip].tooltip-right.tooltip-active::before {
  transform: translateX(0) translateY(0);
}

/* Bottom position variant */
[data-tooltip].tooltip-bottom::after {
  bottom: auto;
  top: calc(100% + 8px);
  transform: translateX(-50%) translateY(-4px);
}

[data-tooltip].tooltip-bottom::before {
  bottom: auto;
  top: calc(100% + 2px);
  border-top-color: transparent;
  border-bottom-color: var(--text-color);
  transform: translateX(-50%) translateY(-4px);
}

[data-tooltip].tooltip-bottom.tooltip-active::before,
[data-tooltip].tooltip-bottom.tooltip-active::after {
  transform: translateX(-50%) translateY(0);
}

/* Responsive: smaller tooltips on mobile */
@media (max-width: 640px) {
  [data-tooltip]::after {
    font-size: 0.75rem;
    padding: 0.35rem 0.5rem;
    max-width: min(220px, 85vw);
  }
}

.dashboard-section {
  margin-top: 0.6rem;
  border: 1px solid var(--card-border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--panel-color);
}

.dashboard-table {
  margin: 0;
}

.dashboard-table thead th {
  text-align: center;
}

.dashboard-table th:first-child,
.dashboard-table td:first-child {
  width: 50%;
}

.dashboard-table .meter-heading {
  font-size: 1rem;
  text-align: center;
}

.unified-power-table {
  margin-top: 0.75rem;
}

.unified-power-table thead th {
  font-size: 0.95rem;
}

.unified-power-table .desc-heading {
  text-align: left;
  font-size: 1rem;
}

.unified-power-table .live-heading {
  text-align: center;
}

.unified-power-table tbody tr.section-divider td {
  border: none;
  padding: 0;
  height: 0;
  line-height: 0;
  background: transparent;
}

.unified-power-table tbody tr.section-divider + tr td {
  border-top: 1px solid var(--card-border);
}

.unified-power-table tbody tr.section-total td {
  font-weight: 700;
  background: rgba(37, 99, 235, 0.07);
  border-top: 1px solid var(--card-border);
  border-bottom: none;
}

.unified-power-table tbody tr.section-total .value-cell {
  font-weight: 700;
}

.unified-power-table tbody tr.section-total:last-child td {
  border-bottom: 1px solid var(--card-border);
}

.dashboard-row.subtotal td {
  background: rgba(37, 99, 235, 0.07);
  font-weight: inherit;
}

.dashboard-row.source td {
  background: transparent;
  font-weight: inherit;
}

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

.value-cell.is-positive {
  color: #2563eb;
}

.value-cell.is-negative {
  color: #dc2626;
}

.empty-state {
  color: var(--muted);
  font-size: 0.95rem;
}

.gridbrain-container {
  display: grid;
  gap: 0.75rem;
}

.grid-card {
  background: linear-gradient(135deg, rgba(37, 99, 235, 0.08), rgba(16, 185, 129, 0.08));
  border: 3px solid var(--card-border);
  border-radius: 12px;
  padding: 0.85rem 1rem;
  box-shadow: 0 12px 28px var(--shadow);
  position: relative;
}

body[data-theme="dark"] .grid-card {
  background: linear-gradient(135deg, rgba(37, 99, 235, 0.15), rgba(16, 185, 129, 0.1));
}

.grid-llm-label {
  position: absolute;
  bottom: -1.25rem;
  left: 50%;
  transform: translateX(-50%) scaleX(1.3);
  font-family: 'Roboto', sans-serif;
  font-size: 0.7rem;
  font-weight: 300;
  color: var(--muted);
  opacity: 0.5;
  text-transform: capitalize;
  letter-spacing: 0.15em;
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
  text-align: center;
  z-index: 0;
}

body[data-theme="dark"] .grid-llm-label {
  /* Uses same Roboto font as light mode for consistency */
}

.grid-card.empty-state {
  text-align: center;
  color: var(--muted);
  font-weight: 600;
}

.grid-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.grid-title {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
}

.badge.tone-hot {
  background: #ef4444;
  color: #fff;
}

.badge.tone-soft {
  background: rgba(255, 255, 255, 0.65);
  color: var(--text-color);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

body[data-theme="dark"] .badge.tone-soft {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.08);
}

.badge.tone-ok {
  background: rgba(34, 197, 94, 0.15);
  color: #15803d;
}

body[data-theme="dark"] .badge.tone-ok {
  background: rgba(34, 197, 94, 0.2);
  color: #4ade80;
}

.badge.tone-warn {
  background: rgba(245, 158, 11, 0.15);
  color: #b45309;
}

body[data-theme="dark"] .badge.tone-warn {
  background: rgba(245, 158, 11, 0.2);
  color: #fbbf24;
}

.badge.tone-disabled {
  background: rgba(107, 114, 128, 0.15);
  color: #4b5563;
}

body[data-theme="dark"] .badge.tone-disabled {
  background: rgba(107, 114, 128, 0.2);
  color: #9ca3af;
}

.grid-meta {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: left;
  flex-basis: 100%;
  width: 100%;
}

.grid-summary {
  margin: 0.6rem 0 0.4rem;
  font-size: 1rem;
  line-height: 1.4;
  font-weight: 400;
}

.grid-tech {
  margin-top: 0.35rem;
}

.grid-tech-title {
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--text-color);
  margin-bottom: 0.2rem;
}

.grid-tech ul {
  margin: 0;
  padding-left: 1.1rem;
  color: var(--muted);
}

.grid-tech li {
  margin: 0.1rem 0;
  font-size: 0.85rem;
  line-height: 1.35;
  word-break: break-word;
}

.grid-detail {
  margin: 0.75rem 0 0 0;
  padding-top: 0.5rem;
  border-top: 1px solid var(--card-border);
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.15rem;
}

.grid-detail dt {
  font-weight: 400;
  color: var(--text-color);
  font-size: 0.8rem;
  text-transform: capitalize;
}

.grid-detail dd {
  margin: 0 0 0.35rem;
  font-size: 0.85rem;
  word-break: break-word;
  color: var(--muted);
}

@media (max-width: 640px) {
  body {
    padding: 1rem;
  }

  th, td {
    font-size: 0.85rem;
  }
}
