/* HVAC Control Portal — friendly, clean, phone-first. */
:root {
  --blue: #1e5aa8;
  --blue-dark: #16467f;
  --red: #c0392b;
  --green: #1e8e4f;
  --amber: #b7791f;
  --ink: #1f2a37;
  --muted: #6b7280;
  --line: #e5e7eb;
  --bg: #f4f7fb;
  --card: #ffffff;
  --radius: 14px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
}

.hidden { display: none !important; }

h1, h2, h3 { margin: 0 0 8px; }
p { margin: 0 0 12px; }
a { color: var(--blue); }

button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; padding: 10px 12px; border: 1px solid #d1d5db; border-radius: 10px; width: 100%; background: #fff; }
input:focus, select:focus { outline: 2px solid var(--blue); outline-offset: 1px; border-color: var(--blue); }
label { display: block; font-weight: 600; margin: 12px 0 4px; font-size: 14px; }
label .hint { font-weight: 400; color: var(--muted); }

/* ---------- buttons ---------- */
.btn {
  border: 0;
  border-radius: 10px;
  padding: 10px 16px;
  font-weight: 600;
  background: var(--blue);
  color: #fff;
}
.btn:hover { background: var(--blue-dark); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-secondary { background: #e8eef5; color: var(--ink); }
.btn-secondary:hover { background: #dbe4ef; }
.btn-danger { background: var(--red); }
.btn-danger:hover { background: #a93226; }
.btn-ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.btn-ghost:hover { background: #eef2f7; }
.btn-big { padding: 14px 22px; font-size: 17px; }

/* ---------- header / nav ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo { height: 46px; width: auto; max-width: 180px; object-fit: contain; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 18px; }
.brand-text span { font-size: 13px; color: var(--muted); }
.brand-contact { font-size: 13px; }
.brand-contact a, .auth-contact a { color: var(--blue); text-decoration: none; }
.brand-contact a:hover, .auth-contact a:hover { text-decoration: underline; }
.auth-contact { font-size: 14px; color: var(--muted); margin: 6px 0 0; }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.user-chip { font-size: 14px; font-weight: 600; background: #e8eef5; padding: 6px 10px; border-radius: 999px; }

.tabs {
  display: flex;
  gap: 6px;
  padding: 12px 16px 0;
  max-width: 1100px;
  margin: 0 auto;
}
.tab {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: 600;
  color: var(--ink);
}
.tab.active { background: var(--blue); border-color: var(--blue); color: #fff; }

.banner {
  max-width: 1100px;
  margin: 12px auto 0;
  padding: 12px 16px;
  background: #fff7e0;
  border: 1px solid #f0d48a;
  border-radius: var(--radius);
}
.banner strong { display: block; margin-bottom: 4px; }

.view { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* ---------- cards & lists ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.card h3 { font-size: 18px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.spacer { flex: 1; }

.badge {
  display: inline-block;
  font-size: 13px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
  background: #e8eef5;
  color: var(--ink);
  white-space: nowrap;
}
.badge-green { background: #dcf5e5; color: #146c37; }
.badge-amber { background: #fdf0d3; color: #8a5a00; }
.badge-grey { background: #eceff1; color: #546e7a; }

.error {
  background: #fdecea;
  color: #8f1d13;
  border: 1px solid #f5c6c0;
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 14px;
}
.success {
  background: #e6f4ea;
  color: #146c37;
  border: 1px solid #b9dfc6;
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 14px;
}

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 14px; }

/* ---------- thermostat card ---------- */
.unit-card { display: flex; flex-direction: column; gap: 10px; }
.unit-card.off { opacity: 0.85; }
.unit-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.unit-name { font-size: 18px; font-weight: 700; }
.unit-meta { font-size: 13px; color: var(--muted); }
.temp-row { display: flex; align-items: center; justify-content: center; gap: 18px; margin: 6px 0; flex-wrap: wrap; }
.temp-btn {
  width: 56px; height: 56px;
  border-radius: 50%;
  border: 2px solid var(--blue);
  background: #fff;
  color: var(--blue);
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
  flex: none;
}
.temp-btn:hover { background: #e8f0fa; }
.temp-btn:disabled { opacity: 0.4; }
.temp-display { text-align: center; min-width: 110px; }
.temp-display .big { font-size: 46px; font-weight: 800; line-height: 1; }
.temp-display .room { font-size: 13px; color: var(--muted); margin-top: 4px; }
.space-display { text-align: center; min-width: 74px; }
.space-display .space-temp { font-size: 30px; font-weight: 800; line-height: 1; color: var(--ink); }
.space-display .room { font-size: 13px; color: var(--muted); margin-top: 4px; }
.mode-pills { display: flex; gap: 6px; flex-wrap: wrap; }
.pill {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 7px 13px;
  font-weight: 600;
  font-size: 14px;
}
.pill.active { background: var(--blue); border-color: var(--blue); color: #fff; }
.pill:disabled { opacity: 0.45; }
.power-row { display: flex; gap: 8px; align-items: center; }
.power-btn { flex: 1; padding: 12px; font-size: 16px; }
.power-btn.on { background: var(--green); }
.power-btn.on:hover { background: #177441; }
.fan-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.fan-row label { margin: 0; flex: none; }
.louver-label { min-width: 84px; text-align: center; font-weight: 600; }
.unit-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.unit-actions .btn { flex: 1 1 auto; }

/* ---------- tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.table-wrap { overflow-x: auto; }

/* ---------- schedule ---------- */
.day-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 12px 0; }
.day-tab { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 7px 13px; font-weight: 600; }
.day-tab.active { background: var(--blue); border-color: var(--blue); color: #fff; }
.period {
  display: grid;
  grid-template-columns: 110px 1fr 1fr 110px 40px;
  gap: 8px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.alarm-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.alarm-row input, .alarm-row select { padding: 8px 10px; }
.filter-flash {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: #fff;
  background: #c0392b;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 13px;
  font-weight: 600;
  animation: filter-blink 1s step-start infinite;
}
@keyframes filter-blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0.25; }
}
.badge-red { background: #c0392b; color: #fff; }
.period input, .period select { padding: 8px 10px; }
.period .remove { background: none; border: 0; color: var(--red); font-size: 20px; padding: 4px 8px; }

/* ---------- auth ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(180deg, #eaf1f9 0%, var(--bg) 60%);
}
.auth-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px 24px;
  width: 100%;
  max-width: 440px;
  text-align: center;
  box-shadow: 0 8px 30px rgba(31, 42, 55, 0.08);
}
.auth-logo { max-width: 240px; width: 80%; height: auto; margin-bottom: 8px; }
.auth-card h1 { font-size: 24px; }
.auth-sub { color: var(--muted); }
.auth-card form { text-align: left; }
.auth-card .btn { width: 100%; margin-top: 14px; }
.auth-tabs { display: flex; gap: 6px; margin: 14px 0 4px; }
.auth-tabs button {
  flex: 1; border: 1px solid var(--line); background: #fff; border-radius: 999px;
  padding: 9px 10px; font-weight: 600;
}
.auth-tabs button.active { background: var(--blue); border-color: var(--blue); color: #fff; }

/* ---------- modal & toast ---------- */
/* The popup frame always stays inside the viewport: it is capped to the
   screen size and scrolls inside itself, so a thermostat never spills
   out of the frame on a phone. */
.modal {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: center; justify-content: center;
  padding: 24px 12px;
  z-index: 50;
  overflow-y: auto;
}
.modal-card {
  background: #fff; border-radius: 16px; padding: 20px; width: 100%;
  max-width: min(640px, calc(100vw - 24px));
  max-height: calc(100vh - 48px);
  max-height: calc(100dvh - 48px);
  overflow-y: auto;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 8px; }
#modal-title { min-width: 0; overflow-wrap: anywhere; }
/* Rows inside a popup (e.g. the Floor picker) wrap instead of overflowing. */
.modal-card .row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 600;
  z-index: 100;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
  max-width: 90vw;
  text-align: center;
}

.activity-item { padding: 10px 0; border-bottom: 1px solid var(--line); }
.activity-item:last-child { border-bottom: 0; }
.activity-item .when { font-size: 13px; color: var(--muted); }

/* ---------- floors & tiles ---------- */
.floor-section { margin-bottom: 14px; }
.floor-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.floor-head h3 { margin: 0; }
.floor-controls { gap: 6px; }
.floor-controls .btn-ghost { padding: 7px 10px; font-size: 14px; }
.tile-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 10px; }
.unit-tile {
  aspect-ratio: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; padding: 10px 6px;
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  font: inherit; cursor: pointer; text-align: center;
}
.unit-tile:hover { border-color: var(--blue); background: #f2f7fd; }
.unit-tile.off { opacity: 0.6; }
.unit-tile .tile-name { font-weight: 700; font-size: 14px; line-height: 1.2; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.unit-tile .tile-room { font-size: 26px; font-weight: 800; line-height: 1.1; }
.unit-tile .tile-set { font-size: 13px; color: var(--muted); }
.unit-tile .tile-mode { font-size: 13px; font-weight: 600; }
.unit-tile .filter-flash { margin-top: 4px; }

/* ---------- floor plan ---------- */
.plan-wrap { position: relative; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: #fff; }
.plan-img { display: block; width: 100%; height: auto; user-select: none; -webkit-user-drag: none; }
.plan-marker {
  position: absolute; transform: translate(-50%, -50%);
  display: flex; flex-direction: column; align-items: center;
  min-width: 74px; padding: 4px 8px;
  background: rgba(30, 90, 168, 0.94); color: #fff;
  border: 2px solid #fff; border-radius: 10px;
  font: inherit; cursor: grab; text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.plan-marker:active { cursor: grabbing; }
.plan-marker .marker-name { font-size: 12px; font-weight: 700; line-height: 1.2; }
.plan-marker .marker-temps { font-size: 12px; line-height: 1.2; }
.plan-chip {
  border: 1px dashed var(--blue); background: #eef4fc; color: var(--ink);
  border-radius: 999px; padding: 7px 14px; font-weight: 600; font-size: 14px;
  cursor: pointer; touch-action: none; user-select: none; -webkit-user-select: none;
}
.plan-chip.selected { background: var(--blue); border-style: solid; color: #fff; }
.plan-drag-ghost {
  position: fixed; z-index: 200; pointer-events: none;
  transform: translate(-50%, -50%);
  background: rgba(30, 90, 168, 0.94); color: #fff;
  border: 2px solid #fff; border-radius: 10px; padding: 4px 10px;
  font-size: 13px; font-weight: 700; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* ---------- move units to a floor ---------- */
.move-panel { margin: 12px 0; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: #f8fafc; }
.move-check { display: block; font-weight: 400; margin: 6px 0; }
.move-check input[type="checkbox"] { width: auto; margin-right: 6px; }

@media (max-width: 640px) {
  .brand-logo { height: 38px; max-width: 140px; }
  .brand-text strong { font-size: 16px; }
  .period { grid-template-columns: 96px 1fr 1fr 96px 36px; gap: 6px; }
  .topbar { padding: 8px 12px; }
  .view { padding: 12px; }
  /* Thermostat popup on a phone: shrink the controls so the whole
     thermostat fits inside the frame instead of spilling out sideways. */
  .modal { padding: 12px 8px; }
  .modal-card { padding: 16px 14px; }
  .temp-row { gap: 10px; }
  .temp-btn { width: 48px; height: 48px; font-size: 24px; }
  .temp-display { min-width: 88px; }
  .temp-display .big { font-size: 38px; }
  .space-display { min-width: 60px; }
  .space-display .space-temp { font-size: 26px; }
  .louver-label { min-width: 72px; }
}
