@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700;900&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
  --bg-void: #0b0d0f;
  --bg-void-2: #14110c;
  --bg-panel: #17191c;
  --bg-panel-2: #1d2024;
  --line: rgba(201, 165, 74, 0.16);
  --line-strong: rgba(201, 165, 74, 0.34);
  --gold: #c9a54a;
  --gold-bright: #e8c874;
  --gold-dim: #8a7038;
  --text-primary: #ede7da;
  --text-muted: #9a9d9f;
  --text-faint: #6b6e70;
  --success: #6fbf8b;
  --danger: #c1665c;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 9px;
  --font-display: 'Vazirmatn', Tahoma, 'Segoe UI', Arial, sans-serif;
  --font-body: 'Vazirmatn', Tahoma, 'Segoe UI', Arial, sans-serif;
  --font-mono: 'JetBrains Mono', 'Consolas', monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: radial-gradient(ellipse 120% 60% at 50% -10%, #241d10 0%, var(--bg-void) 45%, var(--bg-void-2) 100%);
  color: var(--text-primary);
  font-family: var(--font-body);
  direction: rtl;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: inherit; }

::selection { background: var(--gold); color: #14110c; }

/* ---------- عناصر مشترک ---------- */
.eyebrow {
  font-size: 12.5px;
  letter-spacing: 0.14em;
  color: var(--gold-dim);
  font-weight: 500;
  text-transform: none;
}

.gate-glyph { display: block; }
.gate-glyph .stroke { stroke: var(--gold); }
.gate-glyph .stroke-soft { stroke: var(--gold-dim); }
.gate-glyph .glow-dot { fill: var(--gold-bright); }

/* ---------- صفحه‌ی ورود ---------- */
.login-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}

.login-wrap::before {
  content: '';
  position: absolute;
  top: -20%;
  right: 50%;
  transform: translateX(50%);
  width: 640px;
  height: 640px;
  background: radial-gradient(circle, rgba(201,165,74,0.16) 0%, rgba(201,165,74,0) 65%);
  pointer-events: none;
}

.login-card {
  position: relative;
  width: 100%;
  max-width: 380px;
  background: linear-gradient(180deg, var(--bg-panel) 0%, var(--bg-panel-2) 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 40px 32px 32px;
  box-shadow: 0 30px 80px -20px rgba(0,0,0,0.6), 0 0 0 1px rgba(255,255,255,0.02) inset;
  text-align: center;
}

.login-card .gate-glyph { margin: 0 auto 18px; }

.villa-title {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 28px;
  margin: 0 0 6px;
  background: linear-gradient(180deg, var(--gold-bright), var(--gold));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.villa-sub {
  margin: 0 0 30px;
  font-size: 13.5px;
  color: var(--text-muted);
}

.field {
  text-align: right;
  margin-bottom: 18px;
}

.field label {
  display: block;
  font-size: 12.5px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.field input {
  width: 100%;
  background: transparent;
  border: none;
  border-bottom: 1.5px solid var(--line-strong);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 15.5px;
  padding: 8px 2px 10px;
  outline: none;
  transition: border-color 0.2s ease;
}

.field input:focus {
  border-bottom-color: var(--gold);
}

.btn-gold {
  width: 100%;
  margin-top: 8px;
  padding: 13px 18px;
  border: none;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--gold-bright), var(--gold) 60%, var(--gold-dim));
  color: #17130a;
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, filter 0.15s ease;
  box-shadow: 0 10px 24px -8px rgba(201,165,74,0.55);
}

.btn-gold:hover { filter: brightness(1.06); transform: translateY(-1px); }
.btn-gold:active { transform: translateY(0); }
.btn-gold:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--line-strong);
  color: var(--text-muted);
  border-radius: var(--radius-md);
  padding: 9px 16px;
  font-family: var(--font-body);
  font-size: 13.5px;
  cursor: pointer;
  transition: border-color 0.2s ease, color 0.2s ease;
}
.btn-ghost:hover { border-color: var(--gold); color: var(--gold-bright); }

.form-msg {
  min-height: 20px;
  font-size: 13px;
  margin-top: 14px;
  color: var(--danger);
}

.hint-box {
  margin-top: 22px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: rgba(201,165,74,0.06);
  border: 1px dashed var(--line-strong);
  font-size: 12px;
  color: var(--text-faint);
  line-height: 1.9;
  text-align: right;
}
.hint-box b { color: var(--gold-dim); }

/* ---------- داشبورد ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,0.01);
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.topbar .brand-text .name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 17px;
  color: var(--text-primary);
}
.topbar .brand-text .role {
  font-size: 11.5px;
  color: var(--text-faint);
}

.main {
  flex: 1;
  max-width: 920px;
  width: 100%;
  margin: 0 auto;
  padding: 40px 24px 60px;
}

.section-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
  color: var(--text-muted);
  margin: 0 0 18px;
}

.module-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

.gate-card {
  background: linear-gradient(180deg, var(--bg-panel) 0%, var(--bg-panel-2) 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 36px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.gate-card .gate-stage {
  width: 220px;
  height: 150px;
  margin-bottom: 22px;
  position: relative;
}

.gate-card .gate-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 19px;
  margin: 0 0 4px;
}

.gate-card .gate-desc {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0 0 26px;
  max-width: 320px;
}

.gate-panel-l, .gate-panel-r {
  transition: transform 0.6s cubic-bezier(.2,.8,.2,1);
  transform-origin: center;
}
.gate-stage.opening .gate-panel-l { transform: translateX(-14px) rotateY(35deg); }
.gate-stage.opening .gate-panel-r { transform: translateX(14px) rotateY(-35deg); }

.gate-glow-ring {
  opacity: 0;
  transition: opacity 0.5s ease;
}
.gate-stage.opening .gate-glow-ring { opacity: 1; }

.toggle-btn {
  min-width: 220px;
  padding: 15px 26px;
  border: none;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--gold-bright), var(--gold) 60%, var(--gold-dim));
  color: #17130a;
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 15.5px;
  cursor: pointer;
  box-shadow: 0 14px 30px -10px rgba(201,165,74,0.5);
  transition: transform 0.15s ease, filter 0.15s ease;
}
.toggle-btn:hover { filter: brightness(1.06); transform: translateY(-1px); }
.toggle-btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

.status-line {
  margin-top: 20px;
  font-size: 12.5px;
  color: var(--text-faint);
  min-height: 18px;
}
.status-line.ok { color: var(--success); }
.status-line.err { color: var(--danger); }

/* ---------- ماژول‌های داینامیک (Home Assistant) ---------- */
.room-block { margin-bottom: 26px; }
.room-block-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 14.5px;
  color: var(--gold-bright);
  margin: 0 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
}
.module-tile {
  background: linear-gradient(180deg, var(--bg-panel) 0%, var(--bg-panel-2) 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  cursor: pointer;
  user-select: none;
  transition: border-color 0.2s ease, transform 0.15s ease;
  position: relative;
}
.module-tile:active { transform: scale(0.97); }
.module-tile.is-on { border-color: var(--line-strong); box-shadow: 0 0 0 1px var(--gold-dim) inset, 0 10px 24px -14px rgba(201,165,74,0.5); }
.module-tile.view-only { cursor: default; opacity: 0.9; }
.module-tile .tile-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.module-tile .tile-icon {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(201,165,74,0.08);
  color: var(--text-muted);
  transition: background 0.2s ease, color 0.2s ease;
}
.module-tile.is-on .tile-icon { background: rgba(232,200,116,0.18); color: var(--gold-bright); }
.module-tile .tile-badge {
  font-size: 10px;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(255,255,255,0.06);
  color: var(--text-faint);
}
.module-tile.is-on .tile-badge { background: rgba(111,191,139,0.18); color: var(--success); }
.module-tile .tile-name {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-primary);
}
.module-tile .tile-view-tag {
  font-size: 10.5px;
  color: var(--text-faint);
}
.module-tile.loading { opacity: 0.55; pointer-events: none; }

.log-box {
  margin-top: 30px;
  width: 100%;
  border-top: 1px solid var(--line);
  padding-top: 16px;
}
.log-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--text-faint);
  padding: 6px 0;
  border-bottom: 1px dashed rgba(255,255,255,0.04);
}
.log-row .t { color: var(--text-muted); }

.footer-note {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-faint);
  padding: 20px;
}

.camera-card {
  background: linear-gradient(180deg, var(--bg-panel) 0%, var(--bg-panel-2) 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}
.camera-card .cam-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 14.5px;
  margin: 0 0 12px;
}
.camera-card .cam-frame {
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: #000;
  border: 1px solid var(--line);
  margin-bottom: 14px;
  position: relative;
}
.camera-card .cam-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.camera-card .cam-frame .cam-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  font-size: 11.5px;
}
.camera-card .cam-live-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(193,102,92,0.85);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  display: none;
}
.camera-card .cam-frame.live .cam-live-badge { display: block; }

@media (max-width: 520px) {
  .main { padding: 28px 16px 40px; }
  .gate-card { padding: 28px 18px; }
}

/* ---------- دستیار هوشمند (چت) ---------- */
.ai-fab {
  position: fixed;
  bottom: 22px;
  left: 22px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 1px solid var(--line-strong);
  background: linear-gradient(160deg, var(--gold) 0%, var(--gold-dim) 100%);
  color: #14110c;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 12px 28px -10px rgba(201,165,74,0.55);
  z-index: 40;
}
.ai-chat-panel {
  position: fixed;
  bottom: 90px;
  left: 22px;
  width: min(340px, calc(100vw - 44px));
  height: min(440px, calc(100vh - 160px));
  background: var(--bg-panel);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg);
  display: none;
  flex-direction: column;
  overflow: hidden;
  z-index: 41;
  box-shadow: 0 24px 60px -20px rgba(0,0,0,0.6);
}
.ai-chat-panel.open { display: flex; }
.ai-chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13.5px; font-weight: 700; color: var(--gold-bright);
}
.ai-chat-header button { background: none; border: none; color: var(--text-faint); cursor: pointer; font-size: 14px; }
.ai-chat-messages { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.ai-msg { max-width: 85%; padding: 9px 12px; border-radius: 12px; font-size: 12.8px; line-height: 1.7; white-space: pre-wrap; }
.ai-msg-user { align-self: flex-end; background: rgba(201,165,74,0.18); color: var(--text-primary); border-bottom-left-radius: 3px; }
.ai-msg-bot { align-self: flex-start; background: rgba(255,255,255,0.05); color: var(--text-muted); border-bottom-right-radius: 3px; }
.ai-chat-input-row { display: flex; border-top: 1px solid var(--line); }
.ai-chat-input-row input {
  flex: 1; background: none; border: none; padding: 12px 14px; color: var(--text-primary); font-size: 13px; font-family: inherit;
}
.ai-chat-input-row input:focus { outline: none; }
.ai-chat-input-row button {
  background: none; border: none; color: var(--gold-bright); font-weight: 700; padding: 0 16px; cursor: pointer; font-size: 13px;
}

/* رنگ‌بندی ملایم بر اساس نوع دستگاه */
.module-tile[data-domain="light"].is-on .tile-icon { background: rgba(255, 214, 102, 0.22); color: #ffd666; }
.module-tile[data-domain="climate"].is-on .tile-icon { background: rgba(102, 200, 255, 0.2); color: #7fd0ff; }
.module-tile[data-domain="cover"].is-on .tile-icon { background: rgba(120, 220, 180, 0.2); color: #7fe0c0; }
.module-tile[data-domain="lock"] .tile-icon { color: #e08a8a; }
.module-tile[data-domain="scene"] .tile-icon { background: rgba(201,165,74,0.18); color: var(--gold-bright); }
.module-tile[data-domain="sensor"], .module-tile[data-domain="binary_sensor"] { cursor: default; }
.module-tile[data-domain="sensor"] .tile-badge, .module-tile[data-domain="binary_sensor"] .tile-badge {
  background: rgba(255,255,255,0.07); color: var(--text-muted); font-variant-numeric: tabular-nums;
}

/* ---------- نشست‌های فعال (Security) ---------- */
.session-list { display: flex; flex-direction: column; gap: 10px; }
.session-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 14px;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  font-size: 12.5px;
}
.session-row .s-info { display: flex; flex-direction: column; gap: 3px; }
.session-row .s-device { font-weight: 600; color: var(--text-primary); display:flex; align-items:center; gap:8px; }
.session-row .s-meta { color: var(--text-faint); font-size: 11.5px; }
.session-row .s-current-badge {
  font-size: 10px; padding: 2px 8px; border-radius: 999px;
  background: rgba(111,191,139,0.18); color: var(--success);
}
.session-row .btn-sm.danger { flex-shrink: 0; }
