:root {
  --bg: #ffffff;
  --ink: #1f1f1f;
  --paper: #fffaf2;
  --accent: #136f63;
  --accent-2: #f38d68;
  --line: #d8cfc0;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
}

.hero {
  position: relative;
  overflow: hidden;
  border-bottom: 2px solid var(--line);
}
.hero__bg {
  position: absolute;
  inset: 0;
  background: linear-gradient(120deg, #ffe8cc, #d8f3dc 60%, #caf0f8);
  filter: saturate(120%);
}
.hero__content {
  position: relative;
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 16px 10px;
}
.hero__top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.hero__title {
  min-width: 0;
}

.hero__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.hero__logo {
  width: 68px;
  height: 74px;
  max-width: none;
  object-fit: cover;
  object-position: center center;
  display: block;
}

.hero h1 {
  margin: 0 0 2px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.7rem;
  line-height: 1.08;
}
.hero__actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.header-timestamp {
  font-size: 0.82rem;
  color: #5b4d31;
  font-weight: 700;
  background: rgba(255, 250, 242, 0.92);
  border: 1px solid #d8c6a1;
  border-radius: 999px;
  padding: 3px 10px;
  white-space: nowrap;
}

.header-link {
  color: #0b4f6c;
  font-weight: 700;
  text-decoration: none;
}

.header-link:hover {
  text-decoration: underline;
}

.header-user {
  font-size: 0.95rem;
  color: #0b3f7a;
  font-weight: 700;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid #b8d0e3;
  border-radius: 999px;
  padding: 3px 10px;
}

.hero__panels {
  margin-top: 2px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.header-panel {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  background: #fffaf2;
  border: 1px solid #c7bda8;
  border-radius: 10px;
  box-shadow: 0 8px 18px rgba(43, 35, 20, 0.12);
  padding: 8px;
  min-width: min(92vw, 360px);
}

.header-panel[hidden] {
  display: none !important;
}

.header-panel input {
  padding: 8px;
  min-width: 160px;
}

.header-login-form {
  max-width: 520px;
}
.status {
  min-height: 1em;
  margin: 0;
  line-height: 1.15;
  font-weight: 700;
  color: #8a3b12;
}

.layout {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 14px;
}

.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
}
.card__head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card h2 { margin: 4px 0 8px; font-size: 1.1rem; }

.card__title {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.card__title h2 {
  margin: 4px 0 8px;
}

.panel-toggle {
  width: auto;
  height: auto;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  line-height: 1.1;
  border-radius: 0;
  background: transparent;
  color: #214a44;
  opacity: 0.8;
  border: 0;
}

.panel-toggle:hover {
  background: transparent;
  filter: none;
}

.card.is-collapsed .card__body {
  display: none;
}

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.inline-form input, .inline-form select {
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

#configForm #configClave {
  flex: 0 0 220px;
}

#configForm #configValor {
  flex: 0 0 140px;
}

#configForm #configDescripcion {
  flex: 1 1 420px;
  min-width: 260px;
}

#configForm button {
  flex: 0 0 auto;
}

.inline-form[hidden] {
  display: none !important;
}

.field-label {
  flex: 1 1 100%;
  min-width: 100%;
  margin: 2px 0 0;
  font-weight: 700;
  color: #214a44;
}

.inline-form .field-label {
  margin-top: 0;
}
button {
  background: var(--accent);
  color: white;
  border: 0;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 0.9rem;
}
button:hover { filter: brightness(1.05); }

.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  font-weight: bold;
}

.btn-sm {
  padding: 4px 6px;
  font-size: 0.8rem;
  margin: 0 2px;
}

.action-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.btn-power {
  font-size: 1rem;
  font-weight: 700;
}

.btn-calendar {
  font-size: 1rem;
}

.estado-dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

.estado-on {
  background: #1e9e43;
  box-shadow: 0 0 0 2px rgba(30, 158, 67, 0.18);
}

.estado-off {
  background: #d13232;
  box-shadow: 0 0 0 2px rgba(209, 50, 50, 0.18);
}

.manual-flag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1;
}

.manual-flag-on {
  color: #127a35;
  background: transparent;
}

.manual-flag-off {
  color: #a23333;
  background: transparent;
}

.inline-accion-manual {
  display: none;
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #faf7ef;
  gap: 8px;
  width: 100%;
  white-space: normal;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  align-items: center;
}

.inline-accion-manual.show {
  display: grid;
}

.inline-accion-manual-historico {
  display: none;
  margin-top: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f5f5f5;
}

.inline-accion-manual-historico.show {
  display: block;
}

.inline-consumo-estimado {
  display: none;
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #faf7ef;
  gap: 8px;
  flex-wrap: wrap;
}

.inline-consumo-estimado.show {
  display: flex;
}

.inline-consumo-list {
  display: none;
  width: 100%;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 8px;
}

.inline-consumo-list.show {
  display: block;
}

.inline-consumo-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid #eee3cc;
}

.inline-consumo-item:last-child {
  border-bottom: none;
}

.inline-consumo-item__main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.inline-consumo-item__meta {
  color: #5d4b2f;
  font-size: 0.9rem;
}

.inline-consumo-item__actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.inline-consumo-estimado input,
.inline-consumo-estimado select,
.inline-consumo-estimado label {
  min-width: 140px;
  flex: 1;
}

.inline-accion-manual input,
.inline-accion-manual select,
.inline-accion-manual label {
  min-width: 0;
}

.inline-field {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.inline-field .field-label {
  min-width: auto;
  margin: 0;
}

.inline-field--wide {
  grid-column: 1 / -1;
}

.inline-field--checkbox {
  justify-content: flex-end;
}

.inline-field--checkbox .check-inline {
  white-space: normal;
}

.check-inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.form-actions.compact {
  width: 100%;
  grid-column: 1 / -1;
}

.form-modal {
  display: none;
  background: #faf7ef;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 10px;
  gap: 8px;
  flex-wrap: wrap;
}

.form-modal.show {
  display: flex;
}

.form-modal input,
.form-modal select,
.form-modal textarea {
  flex: 1;
  min-width: 150px;
}

.form-modal textarea {
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  resize: vertical;
}

.form-modal .full-width {
  flex: 1 1 100%;
  min-width: 100%;
}


.form-actions {
  width: 100%;
  display: flex;
  gap: 8px;
}

#maquinaForm > input {
  flex: 1 1 220px;
}

#maquinaForm > .maquina-check {
  flex: 0 0 auto;
  min-width: auto;
}

#maquinaEnchufableFields {
  flex: 1 1 100%;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

#maquinaEnchufableFields .maquina-check {
  flex: 0 0 auto;
  min-width: auto;
}

#maquinaIntervaloFields,
#maquinaGestionableFields {
  flex: 1 1 100%;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#maquinaIntervaloFields input,
#maquinaGestionableFields input {
  flex: 1 1 200px;
  min-width: 180px;
}

.form-hint {
  width: 100%;
  margin: 0;
  color: #8a3b12;
  font-size: 0.9rem;
}

.form-hint--stable {
  min-height: 1.2em;
}

.form-hint--ok {
  color: #136f63;
}

.form-hint--error {
  color: #8a3b12;
}

.config-fields {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.config-row {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(180px, 240px);
  gap: 10px;
  align-items: center;
  width: 100%;
}

.config-row label {
  font-weight: 700;
  color: #214a44;
}

.config-row input {
  width: 100%;
}

.history-toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.history-toggle-row h3 {
  margin: 0;
}

.btn-toggle {
  min-width: 32px;
  padding: 0;
  font-size: 1rem;
}

.history-panel {
  margin-top: 8px;
}

.chart-block {
  margin-top: 10px;
}

.chart-block h3 {
  margin: 0;
}

.chart-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px;
  flex-wrap: wrap;
}

.chart-range-label {
  font-size: 0.85rem;
  color: #4c3e29;
}

.chart-range-select {
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.chart-host {
  position: relative;
  overflow: visible;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  padding: 10px;
}

.chart-empty {
  margin: 0;
  color: #8a3b12;
}

.chart-svg {
  width: 100%;
  height: 260px;
  display: block;
}

.chart-svg-tooltip {
  pointer-events: none;
}

.chart-svg-tooltip.is-hidden {
  display: none;
}

.chart-svg-tooltip__box {
  fill: rgba(255, 255, 248, 0.98);
  stroke: #c9bda8;
  stroke-width: 1;
}

.chart-svg-tooltip__title {
  fill: #4b3b23;
  font-size: 11px;
  font-weight: 700;
}

.chart-svg-tooltip__value {
  fill: #1f5d2e;
  font-size: 14px;
  font-weight: 700;
}

.chart-svg-tooltip__label {
  fill: #69563d;
  font-size: 11px;
}

.chart-axis {
  stroke: #9f927a;
  stroke-width: 1;
}

.chart-grid {
  stroke: #ece3d3;
  stroke-width: 1;
}

.chart-line {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chart-hitline {
  fill: none;
  stroke-width: 12;
  stroke-opacity: 0;
  pointer-events: stroke;
}

.chart-label {
  fill: #6c5c44;
  font-size: 11px;
  font-family: Consolas, "Liberation Mono", Menlo, monospace;
}

.chart-legend {
  margin-top: 8px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 6px 10px;
}

.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: 0.85rem;
}

.chart-legend-swatch {
  width: 24px;
  height: 16px;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.18);
  flex: 0 0 auto;
}

.chart-legend-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}
.kpi {
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 8px;
  background: #fff;
  min-width: 0;
}
.kpi .label { font-size: 0.75rem; opacity: 0.8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .value { font-size: 1.1rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.table-scroll {
  overflow-x: auto;
  width: 100%;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  min-width: 320px;
}

#historicoLecturas table,
#historicoLog table,
#configHistorico table {
  font-family: Consolas, "Liberation Mono", Menlo, monospace;
}
th, td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

th.table-header {
  cursor: pointer;
  background: #f0ebe0;
  user-select: none;
  position: relative;
  padding-right: 24px;
}

th.table-header:hover {
  background: #e8e2d5;
}

th.table-header.sorted {
  background: #d8cfc0;
  font-weight: 600;
}

.header-label {
  display: inline-block;
  max-width: 100%;
}

.sort-icon {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.8rem;
  color: #666;
}

th.table-header.sorted .sort-icon {
  color: var(--accent);
  font-weight: bold;
}

tbody tr:nth-child(odd) td {
  background: #fffdf8;
}

tbody tr:nth-child(even) td {
  background: #f8f2e6;
}

tr.row-inactive td {
  color: #7a7a7a;
  font-style: italic;
}

tr.row-editing td {
  background: #fff0cf;
  border-bottom-color: #d9a441;
}

div[class*="table-scroll"] tr.row-editing td {
  background: #fff0cf;
  border-bottom-color: #d9a441;
}

pre {
  background: #faf7ef;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  overflow: auto;
}

@media (max-width: 500px) {
  .kpis { grid-template-columns: 1fr 1fr; }

  .chart-svg {
    height: 220px;
  }

  .chart-toolbar {
    align-items: flex-start;
  }
}

@media (max-width: 800px) {
  .config-row {
    grid-template-columns: 1fr;
  }

  .history-toggle-row {
    align-items: flex-start;
  }
}

@media (min-width: 1024px) {
  .layout {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

/* Modo invitado: ocultar todo excepto panel tiempo real */
body.guest [data-auth-only] {
  display: none;
}

@media (max-width: 760px) {
  .action-btn {
    width: 32px;
    height: 32px;
  }

  .btn-power {
    font-size: 1rem;
  }

  .hero__top {
    flex-direction: column;
    align-items: stretch;
  }

  .hero__actions {
    justify-content: flex-start;
  }

  .hero__panels {
    align-items: stretch;
  }

  .header-panel {
    min-width: 0;
  }
}

/* Ocultar campos de máquina cuando están hidden */
#maquinaEnchufableFields[hidden],
#maquinaIntervaloFields[hidden],
#maquinaGestionableFields[hidden] {
  display: none !important;
}