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

:root {
  --bg: #0b0f19;
  --bg2: #111827;
  --card: #1a2235;
  --card-hover: #243049;
  --text: #f1f5f9;
  --text2: #94a3b8;
  --text3: #64748b;
  --accent: #6366f1;
  --accent2: #818cf8;
  --green: #10b981;
  --red: #ef4444;
  --orange: #f59e0b;
  --purple: #a855f7;
  --border: #1e293b;
  --r: 14px;
  --rs: 10px;
}

html, body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* Top Bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  height: 60px;
}

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

.sophie-avatar-small {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid var(--accent);
  box-shadow: 0 0 15px rgba(99, 102, 241, 0.3);
}

.sophie-avatar-small img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.topbar h1 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.topbar-sub {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.brand-i {
  color: #ff8c00;
  font-weight: 800;
}

.brand-channel {
  color: #ffffff;
  font-weight: 700;
}

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

.status-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  background: var(--card);
  border-radius: 16px;
  font-size: 11px;
  color: var(--text3);
}

.status-pill .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--orange);
}

.status-pill.online .dot { background: var(--green); }
.status-pill.offline .dot { background: var(--red); }

.clock {
  font-size: 14px;
  font-weight: 500;
  color: var(--text2);
  font-variant-numeric: tabular-nums;
  min-width: 55px;
}

/* Main Grid */
.grid {
  display: grid;
  grid-template-columns: 260px 1fr 1fr;
  grid-template-rows: 3fr 2fr;
  grid-template-areas:
    "sophie camera   activity"
    "sophie visitors recordings";
  gap: 12px;
  padding: 12px;
  height: calc(100vh - 60px);
}

/* Panels */
.panel {
  background: var(--card);
  border-radius: var(--r);
  border: 1px solid var(--border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.panel-header h2 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
}

/* Sophie Panel */
.sophie-panel {
  grid-area: sophie;
  background: linear-gradient(180deg, var(--card) 0%, #151d2e 100%);
  min-height: 0;
}

.sophie-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 16px;
  gap: 12px;
}

.sophie-video-wrap {
  flex: 1;
  border-radius: var(--rs);
  overflow: hidden;
  background: #000;
  position: relative;
}

.sophie-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.sophie-speech {
  background: rgba(99, 102, 241, 0.1);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: var(--rs);
  padding: 12px 14px;
  min-height: 60px;
}

.sophie-speech p {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
}

.sophie-state-label {
  text-align: center;
  font-size: 11px;
  color: var(--green);
  text-transform: uppercase;
  letter-spacing: 1.5px;
  font-weight: 600;
}

/* Camera Panel */
.camera-panel {
  grid-area: camera;
  position: relative;
}

.camera-feed-wrap {
  flex: 1;
  background: #000;
  cursor: pointer;
  overflow: hidden;
  position: relative;
}

.camera-feed-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.panel-actions {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
}

.live-dot {
  color: var(--red);
  font-size: 11px;
  font-weight: 600;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* Activity Panel */
.activity-panel {
  grid-area: activity;
}

.activity-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.activity-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
}

.activity-row:last-child { border-bottom: none; }
.activity-icon { font-size: 14px; flex-shrink: 0; }
.activity-text { flex: 1; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.activity-time { color: var(--text2); font-size: 11px; flex-shrink: 0; }

/* Recordings Panel */
.recordings-panel {
  grid-area: recordings;
}

.recordings-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.recording-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  cursor: default;
}

.recording-row:last-child { border-bottom: none; }
.recording-icon { font-size: 16px; flex-shrink: 0; margin-top: 2px; }
.recording-info { flex: 1; min-width: 0; }
.recording-meta { font-size: 11px; color: var(--text2); margin-bottom: 2px; }
.recording-transcript { font-size: 12px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Visitors Panel */
.visitors-panel {
  grid-area: visitors;
}

/* Feed Toggle */
.panel-header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#feed-toggle {
  color: #ef4444;
  font-weight: 600;
  font-size: 11px;
  letter-spacing: 0.5px;
}

#feed-toggle.feed-off-btn {
  color: var(--text2);
}

.feed-off {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--bg2);
  color: var(--text2);
  font-size: 14px;
}

.feed-off span { font-size: 36px; opacity: 0.5; }
.feed-off p { margin: 0; }

.camera-loading {
  padding: 20px;
  text-align: center;
  color: var(--text3);
  font-size: 13px;
}

/* Visitors List */

.visitor-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.visitor-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: var(--rs);
  cursor: pointer;
  transition: background 0.15s;
}

.visitor-row:hover { background: var(--bg2); }

.visitor-row img {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--bg2);
}

.visitor-row-info { flex: 1; min-width: 0; }

.visitor-row-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.visitor-row-meta {
  font-size: 11px;
  color: var(--text3);
  display: flex;
  gap: 6px;
  align-items: center;
}

.visitor-type-badge {
  font-size: 9px;
  padding: 1px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.visitor-type-badge.doorbell { background: rgba(239,68,68,0.15); color: var(--red); }
.visitor-type-badge.motion { background: rgba(245,158,11,0.15); color: var(--orange); }

.empty-state {
  text-align: center;
  padding: 30px;
  color: var(--text3);
  font-size: 13px;
}

/* HA Widgets */
.ha-widgets {
  flex: 1;
  padding: 10px;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-content: flex-start;
}

.ha-widget {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg2);
  border-radius: var(--rs);
  border: 1px solid var(--border);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.ha-widget:hover { border-color: var(--accent); }
.ha-widget.on { border-color: var(--green); background: rgba(16, 185, 129, 0.08); }
.ha-widget.locked { border-color: var(--green); }
.ha-widget.unlocked { border-color: var(--red); }

.ha-widget-icon { font-size: 16px; }
.ha-widget-name { color: var(--text2); }
.ha-widget-state { font-weight: 600; font-size: 11px; margin-left: auto; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--rs);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  color: white;
}

.btn:active { transform: scale(0.96); }

.btn-success { background: var(--green); }
.btn-success:hover { background: #34d399; }
.btn-secondary { background: var(--card-hover); border: 1px solid var(--border); color: var(--text); }
.btn-ghost { background: transparent; color: var(--text3); }
.btn-ghost:hover { background: var(--card-hover); color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 11px; }
.btn-xl { padding: 16px 36px; font-size: 18px; border-radius: 14px; }
.btn-block { width: 100%; }

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(10px);
}

.doorbell-alert {
  text-align: center;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.doorbell-ring-anim {
  position: absolute;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  border: 3px solid var(--orange);
  left: 50%;
  top: 30%;
  transform: translate(-50%, -50%);
  animation: ringPulse 1s infinite;
  pointer-events: none;
}

@keyframes ringPulse {
  0% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(2.5); opacity: 0; }
}

.doorbell-icon { font-size: 64px; margin-bottom: 12px; animation: shake 0.4s infinite; }

@keyframes shake {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(12deg); }
  75% { transform: rotate(-12deg); }
}

.doorbell-text { font-size: 28px; font-weight: 700; margin-bottom: 8px; }

.doorbell-status {
  font-size: 14px;
  color: var(--green);
  margin-bottom: 12px;
  animation: pulse 1.5s infinite;
}

.doorbell-snapshot {
  margin: 16px auto;
  width: 300px;
  height: 200px;
  border-radius: var(--r);
  overflow: hidden;
  background: var(--card);
}

.doorbell-snapshot img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.doorbell-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 20px;
}

/* Camera Fullscreen */
.camera-fullscreen {
  position: relative;
  width: 90vw;
  height: 80vh;
  background: #000;
  border-radius: var(--r);
  overflow: hidden;
}

.camera-fullscreen img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.camera-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  border: none;
  color: white;
  font-size: 18px;
  cursor: pointer;
  z-index: 10;
}

.camera-fullscreen-label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px 16px;
  background: linear-gradient(transparent, rgba(0,0,0,0.7));
  font-size: 14px;
  color: white;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 10px 18px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--rs);
  font-size: 13px;
  z-index: 200;
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
  animation: toastIn 0.2s ease;
}

@keyframes toastIn {
  from { transform: translateY(15px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.toast.success { border-left: 3px solid var(--green); }
.toast.error { border-left: 3px solid var(--red); }
.toast.warning { border-left: 3px solid var(--orange); }

/* Sophie Toggle */
.sophie-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px;
  background: var(--card);
  border-radius: 16px;
  cursor: pointer;
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  transition: all 0.2s;
}

.sophie-toggle.active {
  border-color: var(--green);
  background: rgba(16, 185, 129, 0.1);
}

.sophie-toggle.inactive {
  border-color: var(--text3);
  opacity: 0.6;
}

.sophie-toggle-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  transition: background 0.2s;
}

.sophie-toggle.active .sophie-toggle-dot { background: var(--green); }
.sophie-toggle.inactive .sophie-toggle-dot { background: var(--text3); }

/* Sophie bottom bar */
.sophie-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Sophie disabled state */
.sophie-panel.disabled {
  opacity: 0.5;
  filter: grayscale(0.5);
}

/* Empty state */
.empty-state {
  padding: 20px;
  text-align: center;
  color: var(--text2);
  font-size: 13px;
}

/* Persona Editor Modal */
.persona-editor {
  background: var(--card);
  border-radius: var(--r);
  border: 1px solid var(--border);
  width: 560px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  animation: slideUp 0.2s ease;
}

.persona-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.persona-header h2 {
  font-size: 16px;
  font-weight: 600;
}

.persona-form {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.persona-form label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-top: 4px;
}

.persona-form input,
.persona-form textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--rs);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
}

.persona-form input:focus,
.persona-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.persona-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  justify-content: flex-end;
}

.hidden { display: none !important; }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Tablet responsive */
@media (max-width: 1024px) {
  .grid {
    grid-template-columns: 200px 1fr;
    grid-template-rows: auto auto auto;
    grid-template-areas:
      "sophie camera"
      "sophie activity"
      "visitors recordings";
  }
}

@media (max-width: 768px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    grid-template-areas:
      "sophie"
      "camera"
      "visitors"
      "activity"
      "recordings";
    overflow-y: auto;
  }
  .sophie-panel { max-height: 300px; }
}
