:root {
  --bg: #0f172a;
  --card: #1e293b;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --green: #4ade80;
  --red: #f87171;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  width: 100%;
  max-width: 1100px;
}

h1 { font-size: 1.6rem; font-weight: 600; }
h2 { font-size: 1.05rem; margin-bottom: 1rem; color: var(--muted); }
h3 { margin-bottom: 1rem; }

.status {
  font-size: 0.85rem;
  padding: 0.3rem 0.7rem;
  border-radius: 9999px;
  background: #334155;
}
.status.ok { background: #14532d; color: var(--green); }
.status.error { background: #450a0a; color: var(--red); }

main {
  display: grid;
  gap: 1.25rem;
  width: 100%;
  max-width: 1100px;
}

.card {
  background: var(--card);
  border-radius: 12px;
  padding: 1.1rem 1.25rem;
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.3);
  overflow-x: auto;          /* allows tables to scroll instead of breaking layout */
}

.summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 1.25rem;
}
.summary-item .label {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
}
.summary-item .value {
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--accent);
}

/* Category bars */
#category-breakdown,
#energy-by-category {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.cat-row {
  display: grid;
  grid-template-columns: 100px 1fr 85px;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.9rem;
}

.cat-name {
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cat-bar-bg {
  background: #334155;
  border-radius: 4px;
  height: 10px;
  overflow: hidden;
  min-width: 40px;
}

.cat-bar-fill {
  background: var(--accent);
  height: 100%;
  border-radius: 4px;
  transition: width 0.4s ease;
}

.cat-value {
  text-align: right;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  font-size: 0.85rem;
}

/* Chart */
.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.6rem;
}
.chart-header h2 {
  margin-bottom: 0;
  flex-shrink: 0;
}
.timeframe-buttons {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}
.timeframe-buttons button {
  background: #334155;
  color: var(--text);
  border: none;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}
.timeframe-buttons button:hover { background: #475569; }
.timeframe-buttons button.active {
  background: var(--accent);
  color: #0f172a;
  font-weight: 600;
}
.chart-container {
  position: relative;
  height: 260px;
  min-width: 280px;
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  min-width: 500px;          /* forces horizontal scroll on very small screens instead of crushing */
}
th, td {
  text-align: left;
  padding: 0.55rem 0.4rem;
  border-bottom: 1px solid #334155;
  white-space: nowrap;
}
th { color: var(--muted); font-weight: 500; }
.power { font-weight: 600; color: var(--accent); }

button.edit-btn {
  background: #334155;
  color: var(--text);
  border: none;
  padding: 0.25rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}
button.edit-btn:hover { background: #475569; }

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.6rem;
}
.section-header h2 { margin-bottom: 0; }

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

#category-filter,
.export-controls select {
  background: #0f172a;
  color: var(--text);
  border: 1px solid #475569;
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
}

.export-controls {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.export-btn {
  background: #334155;
  color: var(--text);
  border: none;
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
}
.export-btn:hover { background: #475569; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 1rem;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--card);
  padding: 1.5rem;
  border-radius: 12px;
  width: 100%;
  max-width: 380px;
}
.modal-content label {
  display: block;
  margin-top: 1rem;
  margin-bottom: 0.3rem;
  font-size: 0.9rem;
  color: var(--muted);
}
.modal-content input[type="text"],
.modal-content select {
  width: 100%;
  padding: 0.5rem;
  border-radius: 6px;
  border: 1px solid #475569;
  background: #0f172a;
  color: var(--text);
}
.checkbox {
  display: flex !important;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1.2rem;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.5rem;
}
.modal-actions button {
  padding: 0.5rem 1rem;
  border-radius: 6px;
  border: none;
  cursor: pointer;
}
#cancel-edit { background: #334155; color: var(--text); }
.modal-actions button[type="submit"] {
  background: var(--accent);
  color: #0f172a;
  font-weight: 600;
}

/* Extra small screens */
@media (max-width: 600px) {
  body { padding: 0.75rem; }
  h1 { font-size: 1.4rem; }
  .cat-row {
    grid-template-columns: 80px 1fr 75px;
    font-size: 0.8rem;
  }
  .summary-item .value { font-size: 1.2rem; }
  .chart-container { height: 220px; }
}

/* Branding */
.brand {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.logo {
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: #38bdf8;
}

.tagline {
  font-size: 0.8rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}

/* Logo left, controls right; wraps on narrow screens */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  width: 100%;
  max-width: 1100px;
}

.brand {
  flex-shrink: 0;
}

.header-right {
  margin-left: auto;
}

.logo-img {
  height: 64px;
  width: auto;
  display: block;
  margin: 10px 0;
}

@media (max-width: 600px) {
  .logo-img { height: 44px; margin: 6px 0; }
}

.status-online {
  color: #4ade80;
  font-weight: 600;
  font-size: 0.85rem;
}

.status-offline {
  color: #f87171;
  font-weight: 600;
  font-size: 0.85rem;
}

.offline-banner {
  width: 100%;
  max-width: 1100px;
  background: #450a0a;
  color: #fca5a5;
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.45rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  text-align: center;
}

.offline-banner.hidden {
  display: none;
}

.storage-card {
  padding-bottom: 1rem;
}
.storage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.6rem;
}
.storage-header h2 {
  margin-bottom: 0;
}
#storage-text {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--accent);
}
.storage-bar-bg {
  background: #334155;
  border-radius: 6px;
  height: 12px;
  overflow: hidden;
}
.storage-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 6px;
  transition: width 0.4s ease, background 0.3s ease;
}
.storage-bar-fill.warning {
  background: #f87171;
}
.storage-detail {
  margin-top: 0.4rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.admin-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 0.3rem 0.7rem;
  border-radius: 6px;
  border: 1px solid #475569;
}
.admin-link:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.chart-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-left: auto;
}
.chart-controls select {
  background: #0f172a;
  color: var(--text);
  border: 1px solid #475569;
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
}

/* History chart controls – single row */
.chart-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
}

.chart-controls {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
}

.chart-controls select {
  background: #0f172a;
  color: var(--text);
  border: 1px solid #475569;
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
  height: 32px;
}

.chart-controls .timeframe-buttons {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.35rem;
}

.chart-controls .timeframe-buttons button {
  height: 32px;
  white-space: nowrap;
}

@media (max-width: 900px) {
  .chart-controls {
    flex-wrap: wrap;
    margin-left: 0;
    width: 100%;
  }
}

.chart-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.55);
  color: var(--accent);
  font-weight: 600;
  font-size: 0.95rem;
  z-index: 2;
  border-radius: 8px;
}
.chart-loading.hidden { display: none; }
.timeframe-buttons button:disabled {
  opacity: 0.5;
  cursor: wait;
}

.chart-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  width: 100%;
  max-width: 1100px;
}
.chart-row .card .chart-container {
  height: 440px !important;
}
#energyByCategoryChart,
#powerByCategoryChart {
  max-height: 440px;
}
@media (max-width: 900px) {
  .chart-row { grid-template-columns: 1fr; }
}

.chart-row canvas + * { }
#energyByCategoryChart,
section:has(#energyByCategoryChart) {
  color: #e2e8f0;
}

.summary-item .sub { color: var(--muted, #94a3b8); font-size: 0.8rem; margin-top: 0.2rem; }

/* Demo: white backing so the dark wordmark reads on the dark header */
.logo-img { background:#fff; padding:6px 12px; border-radius:10px; box-sizing:content-box; height:48px; }
@media (max-width:600px){ .logo-img { height:34px; padding:5px 10px; } }
