/* MERLIN Mobile — Universal responsive for Dashboard, Wizard, Admin Portal, Client Portal */

/* ═══ TABLET (max 900px) ═══ */
@media (max-width: 900px) {
  /* Dashboard/Wizard */
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: -260px; width: 260px; transition: left 0.25s; z-index: 100; box-shadow: 0 0 32px rgba(0,0,0,0.5); }
  .sidebar.open { left: 0; }
  .mobile-menu-btn { display: flex; }
  .topbar { padding: 16px 16px 16px 64px; }
  .content { padding: 20px 16px 32px; }
  .hero-cards, .energy-summary, .status-grid, .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .user-dropdown { right: 16px; top: 64px; }
  .modal-content, .modal-card { width: 95vw; max-width: 95vw; max-height: 90vh; margin: 5vh auto; }
  .climate-list, .lighting-list, .security-list, .media-list { grid-template-columns: 1fr; }
  .history-controls { flex-direction: column; gap: 8px; }
  .history-search-wrap { width: 100%; }

  /* Client Portal */
  .main-content { padding: 16px; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .plans-grid { grid-template-columns: 1fr; gap: 12px; }
  .nav-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; }

  /* Admin */
  .main { margin-left: 0 !important; padding: 16px; }
}

/* ═══ PHONE (max 600px) ═══ */
@media (max-width: 600px) {
  body { font-size: 14px; }

  /* ── Dashboard/Wizard topbar ── */
  .topbar { padding: 12px 16px 12px 56px; min-height: 56px; }
  .topbar-greeting h1, .topbar h1 { font-size: 17px; }
  .topbar-date { font-size: 10px; }
  .topbar-actions { gap: 6px; }
  .icon-btn { width: 34px; height: 34px; font-size: 16px; }
  .user-badge { width: 32px; height: 32px; font-size: 13px; }
  .mobile-menu-btn { top: 12px; left: 12px; width: 36px; height: 36px; }
  .content { padding: 16px 12px 80px; }
  .view-header h2 { font-size: 20px; }
  .view-subtitle { font-size: 12px; }

  /* ── Hero cards ── */
  .hero-cards { grid-template-columns: 1fr; gap: 8px; }
  .hero-card { padding: 14px 16px; }
  .hero-card-value { font-size: 28px; }
  .hero-card-label { font-size: 10px; }

  /* ── Rooms ── */
  .rooms-grid, .cards-grid { grid-template-columns: 1fr; gap: 8px; }
  .room-card { padding: 14px; }
  .room-name { font-size: 14px; }
  .room-pills { gap: 4px; }
  .room-pill { font-size: 11px; padding: 3px 8px; }
  .section-header { flex-direction: column; align-items: flex-start; gap: 10px; }
  .tab-switch { width: 100%; }
  .tab-btn { flex: 1; font-size: 12px; padding: 6px 8px; }

  /* ── Energy ── */
  .energy-summary { grid-template-columns: 1fr; gap: 8px; }
  .metric-card, .stat-card { padding: 14px; }
  .metric-value, .stat-value { font-size: 24px; }
  .metric-label, .stat-label { font-size: 10px; }
  .energy-period-tabs { gap: 0; }
  .energy-tab { font-size: 12px; padding: 8px 12px; }
  .chart-tall, .chart-card { min-height: 200px; }
  .chart-label { font-size: 8px; }
  .chart-bar-val { font-size: 8px; }

  /* ── Climate ── */
  .climate-card { padding: 14px; }
  .climate-card-title { font-size: 14px; }
  .climate-target-value { font-size: 28px; }
  .climate-target-btn { width: 36px; height: 36px; font-size: 18px; }
  .climate-modes { gap: 4px; flex-wrap: wrap; }
  .climate-mode-btn { padding: 8px 10px; font-size: 11px; min-width: 56px; }

  /* ── Lighting ── */
  .light-card { padding: 14px; }
  .light-card-title { font-size: 14px; }
  .brightness-value { font-size: 12px; min-width: 36px; }

  /* ── Security ── */
  .security-row { padding: 10px 12px; }
  .security-row-name { font-size: 13px; }
  .security-row-icon { width: 32px; height: 32px; font-size: 15px; }
  .alarm-card { padding: 14px; }
  .alarm-modes { flex-direction: column; gap: 6px; }

  /* ── AI Chat ── */
  .ai-welcome-title { font-size: 18px; }
  .ai-welcome-sub { font-size: 12px; padding: 0 8px; }
  .ai-suggestions { flex-wrap: wrap; gap: 6px; justify-content: center; }
  .ai-suggestion { font-size: 12px; padding: 6px 12px; }
  .ai-input-bar { padding: 8px 12px; gap: 6px; }
  .ai-input-bar input { font-size: 14px; padding: 8px 12px; }
  .ai-msg-bubble { font-size: 13px; max-width: 85%; }
  .ai-token-bar-wrap { font-size: 11px; padding: 6px 12px; }

  /* ── Status ── */
  .status-grid { grid-template-columns: 1fr; gap: 8px; }
  .info-row { padding: 10px 0; }
  .info-label, .info-value { font-size: 12px; }

  /* ── Account ── */
  .plan-card { padding: 16px; }
  .plan-card-name { font-size: 22px; }
  .plan-card-actions { flex-direction: column; gap: 8px; }
  .plan-card-actions .btn-primary, .plan-card-actions .btn-secondary { width: 100%; text-align: center; justify-content: center; }
  .benefits-list li { font-size: 13px; }

  /* ── Settings ── */
  .settings-row { flex-direction: column; gap: 10px; align-items: flex-start; }

  /* ── History ── */
  .history-entity-list { max-height: 200px; }
  .history-entity-btn { font-size: 12px; padding: 6px 10px; }

  /* ── Modals — bottom sheet style ── */
  .modal-content, .modal-card { width: 100vw; max-width: 100vw; max-height: 100vh; border-radius: 16px 16px 0 0; margin: auto 0 0; position: fixed; bottom: 0; left: 0; right: 0; }
  .modal-body { max-height: 60vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .modal-small { width: 100vw; max-width: 100vw; }
  .icon-grid { grid-template-columns: repeat(5, 1fr); gap: 6px; }
  .icon-pick { width: 44px; height: 44px; font-size: 18px; }

  /* ── Device rows ── */
  .device-row { padding: 10px 12px; gap: 8px; }
  .device-icon { width: 32px; height: 32px; font-size: 14px; }
  .device-name { font-size: 13px; }
  .device-state { font-size: 11px; }
  .cover-controls { gap: 4px; }
  .cover-btn { width: 32px; height: 32px; font-size: 14px; }
  .toggle { min-width: 48px; min-height: 28px; }
  .user-dropdown { left: 12px; right: 12px; width: auto; max-width: none; }
  .status-bar { font-size: 11px; padding: 8px 12px; }
  .card { padding: 14px; border-radius: 12px; }
  .card-title { font-size: 14px; }
  .card-subtitle { font-size: 11px; }

  /* ── Wizard specific ── */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-wrap table { min-width: 600px; }
  .filter-bar { width: 100%; }
  .filter-bar select { width: 100%; }
  .view-header { flex-direction: column; align-items: flex-start; gap: 10px; }
  .view-header .btn-primary { width: 100%; justify-content: center; }
  .config-card { margin-bottom: 12px; }
  .config-card-header { flex-direction: column; gap: 8px; }
  .config-card-body { padding: 14px; }
  .form-row { flex-direction: column; gap: 8px; }
  .form-group input, .form-group select { font-size: 14px; }

  /* Wizard automations */
  .preset-grid { grid-template-columns: 1fr; }
  .preset-card { padding: 14px; }
  .auto-builder { padding: 14px; }
  .preset-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; }
  .preset-tab { white-space: nowrap; font-size: 12px; }
  .block-card { padding: 10px; }

  /* Wizard integrations */
  .integration-card { flex-direction: column; gap: 10px; }
  .int-actions { justify-content: flex-start; }
  .hacs-panel { padding: 14px; }
  .hacs-repo-card { flex-direction: column; gap: 10px; align-items: flex-start; }

  /* Wizard logs */
  .logs-filters { flex-direction: column; gap: 8px; }
  .logs-filters select { width: 100%; }
  .log-entry { gap: 8px; }
  .log-time { font-size: 10px; }
  .system-log-wrap { font-size: 11px; }

  /* ── Client Portal ── */
  .topbar { padding: 0 12px; height: 48px; }
  .topbar-left img { width: 24px; height: 24px; }
  .topbar-left h2 { font-size: 14px; }
  .topbar-right { gap: 8px; }
  .topbar-user { font-size: 11px; display: none; }
  .btn-logout { font-size: 11px; padding: 5px 10px; }
  .main-content { padding: 12px; }
  .nav-tabs { gap: 0; overflow-x: auto; }
  .nav-tab { font-size: 12px; padding: 10px 12px; white-space: nowrap; }
  .stats-row { grid-template-columns: 1fr; gap: 8px; }
  .stat-card { padding: 14px; }
  .stat-value { font-size: 24px; }
  .device-card { padding: 16px; }
  .device-info { grid-template-columns: 1fr; gap: 8px; }
  .device-info-item { padding: 6px 0; }
  .plans-grid { grid-template-columns: 1fr; gap: 12px; }
  .plan-card { padding: 20px; }
  .plan-price { font-size: 24px; }
  .plan-features { font-size: 12px; }
  .profile-form { gap: 10px; }
  .profile-form input { font-size: 14px; }
  .profile-actions { flex-direction: column; gap: 8px; }
  .usage-bar-header { font-size: 11px; }
  .activate-input-row { flex-direction: column; gap: 8px; }
  .activate-input-row input { width: 100%; }
  .activate-input-row button { width: 100%; }

  /* Client Portal auth */
  .auth-container { padding: 12px; }
  .auth-box { padding: 20px; max-width: 100%; }
  .auth-logo img { width: 48px; height: 48px; }
  .auth-logo h1 { font-size: 1.3rem; }
  .auth-tabs { gap: 4px; }

  /* ── Admin Portal ── */
  .sidebar { width: 220px; left: -230px; }
  .sidebar.open { left: 0; }
  .main { margin-left: 0 !important; padding: 16px 12px; }
  .section-title { font-size: 18px; }
  .stats { grid-template-columns: 1fr; gap: 8px; }
  .stat-card .label { font-size: 10px; }
  table { font-size: 12px; }
  table th, table td { padding: 8px 6px; }
  .modal-overlay .modal-card { width: 95vw; }
  .modal-card { padding: 16px; }
}

/* ═══ VERY SMALL PHONES (max 380px) ═══ */
@media (max-width: 380px) {
  .content { padding: 12px 8px 80px; }
  .hero-card-value { font-size: 24px; }
  .room-card { padding: 12px; }
  .topbar-greeting h1 { font-size: 15px; }
  .climate-target-value { font-size: 24px; }
  .ai-suggestions { gap: 4px; }
  .ai-suggestion { font-size: 11px; padding: 5px 10px; }
  .icon-grid { grid-template-columns: repeat(4, 1fr); }
  .auth-box { padding: 16px; }
  .plan-card { padding: 14px; }
}

/* ═══ LANDSCAPE PHONE ═══ */
@media (max-height: 500px) and (orientation: landscape) {
  .content { padding: 12px 16px 24px; }
  .hero-cards { grid-template-columns: repeat(3, 1fr); }
  .rooms-grid { grid-template-columns: repeat(2, 1fr); }
  .modal-content, .modal-card { max-height: 95vh; }
  .modal-body { max-height: 50vh; }
}

/* ═══ PWA / STANDALONE ═══ */
@media (display-mode: standalone) {
  body { padding-top: env(safe-area-inset-top); }
  .sidebar { padding-top: env(safe-area-inset-top); }
  .topbar { padding-top: calc(12px + env(safe-area-inset-top)); }
  .status-bar { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  .ai-input-bar { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
}

/* ═══ TOUCH DEVICES ═══ */
@media (hover: none) and (pointer: coarse) {
  .nav-item { padding: 12px 14px; min-height: 44px; }
  .toggle { min-width: 51px; min-height: 31px; }
  .climate-target-btn { min-width: 44px; min-height: 44px; }
  .cover-btn { min-width: 40px; min-height: 40px; }
  .icon-pick { min-width: 44px; min-height: 44px; }
  .room-card:hover, .nav-item:hover, .card:hover { transform: none; }
  .brightness-slider::-webkit-slider-thumb { width: 28px; height: 28px; }
  .modal-body, .history-entity-list, .ai-messages { -webkit-overflow-scrolling: touch; scroll-behavior: smooth; }
  /* Client portal */
  .nav-tab { min-height: 44px; }
  .btn-primary, .btn-secondary, .btn-save { min-height: 44px; }
  .plan-btn { min-height: 44px; }
}
