/* ============ Base layout & variables ============ */
:root {
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --gap: 12px;
  --gap-lg: 18px;
  --sidebar-w: 240px;
  --sidebar-w-collapsed: 60px;
  --topbar-h: 60px;
  --tile-min-4: 180px;
  --tile-min-3: 220px;
  --tile-min-2: 280px;
  --font: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;

  --bg: #0f0f10;
  --bg-elev: #1a1a1c;
  --bg-elev-2: #232326;
  --text: #f2f2f5;
  --text-muted: #888;
  --accent: #7dd3fc;
  --accent-warm: #ffb347;
  --border: rgba(255,255,255,.06);
  --shadow: 0 2px 4px rgba(0,0,0,.3);
  --glow: none;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

body {
  display: grid;
  grid-template-columns: auto 1fr;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
a { color: inherit; text-decoration: none; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--bg-elev); padding: 2px 6px; border-radius: 4px; }

/* ============ Sidebar ============ */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width .2s ease;
  overflow: hidden;
}
.sidebar.collapsed { width: var(--sidebar-w-collapsed); }
.sidebar.kiosk { display: none; }

.sidebar-head {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; font-size: 15px; white-space: nowrap; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  transition: background .15s;
}
.icon-btn:hover { background: var(--bg-elev-2); }
.icon-btn i { width: 18px; height: 18px; }

.tabs-nav {
  flex: 1;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
}
.tab-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  transition: background .15s, color .15s;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
}
.tab-btn:hover { background: var(--bg-elev-2); color: var(--text); }
.tab-btn.active { background: var(--bg-elev-2); color: var(--accent); }
.tab-btn i, .tab-btn .lucide-icon { width: 20px; height: 20px; flex-shrink: 0; }

.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }

.theme-switch { display: flex; gap: 6px; }
.theme-btn {
  flex: 1; padding: 6px 0;
  border-radius: var(--radius-sm);
  background: var(--bg-elev-2);
  font-size: 12px; font-weight: 600; letter-spacing: 1px;
  color: var(--text-muted);
  transition: background .15s, color .15s;
}
.theme-btn:hover { color: var(--text); }
.theme-btn.active { background: var(--accent); color: var(--bg); }

.conn { display: flex; align-items: center; gap: 8px; padding: 6px 8px; font-size: 12px; color: var(--text-muted); }
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: #666; }
.conn.ok .dot { background: #4ade80; box-shadow: 0 0 8px rgba(74,222,128,.5); }
.conn.warn .dot { background: #fbbf24; box-shadow: 0 0 8px rgba(251,191,36,.4); }
.conn.bad .dot { background: #ef4444; }

/* ============ Main area ============ */
.main {
  overflow-y: auto;
  height: 100vh;
  height: 100dvh;
  padding: 0 22px 40px;
  padding-bottom: calc(40px + env(safe-area-inset-bottom, 0px));
}
.main.kiosk { padding: 0 14px 20px; }

.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky; top: 0; z-index: 10;
  background: linear-gradient(180deg, var(--bg) 75%, transparent);
  padding-bottom: 6px;
}
.page-title { margin: 0; font-size: 26px; font-weight: 700; letter-spacing: -0.5px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.tab-body { padding-top: 10px; display: flex; flex-direction: column; gap: 30px; }
.tab-section { display: flex; flex-direction: column; gap: var(--gap); }
.section-head { display: flex; align-items: baseline; gap: 10px; padding: 0 2px; }
.section-title { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--text-muted); font-weight: 600; }

.section-body { display: grid; gap: var(--gap); grid-auto-flow: dense; }
.layout-grid-4 { grid-template-columns: repeat(auto-fit, minmax(var(--tile-min-4), 1fr)); }
.layout-grid-3 { grid-template-columns: repeat(auto-fit, minmax(var(--tile-min-3), 1fr)); }
.layout-grid-2 { grid-template-columns: repeat(auto-fit, minmax(var(--tile-min-2), 1fr)); }
.layout-list   { grid-template-columns: 1fr; }
.layout-rooms  { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }

.widget {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  box-shadow: var(--shadow);
  min-height: 90px;
  transition: transform .15s, background .15s, box-shadow .2s, border-color .2s;
  position: relative;
  cursor: pointer;
  overflow: hidden;
}
.widget:hover { transform: translateY(-2px); background: var(--bg-elev-2); }
.widget[data-size="2x1"] { grid-column: span 2; }
.widget[data-size="2x2"] { grid-column: span 2; grid-row: span 2; min-height: 200px; }
.widget[data-size="1x2"] { grid-row: span 2; min-height: 200px; }
.widget[data-size="3x1"] { grid-column: span 3; }
.widget[data-size="3x2"] { grid-column: span 3; grid-row: span 2; min-height: 200px; }

/* ---- widget internals ---- */
.w-ico { width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center; color: var(--text-muted); margin-bottom: 10px; transition: color .15s; }
.w-ico i, .w-ico svg { width: 26px; height: 26px; }
.w-ico-img { width: 28px; height: 28px; object-fit: contain; }
.w-title { font-size: 13px; font-weight: 600; color: var(--text); line-height: 1.3; }
.w-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.w-value { font-size: 22px; font-weight: 700; line-height: 1; margin-top: 6px; }
.w-value .unit { font-size: 13px; font-weight: 500; color: var(--text-muted); margin-left: 3px; }
/* on-state: подсветка иконки + бордер */
.widget[data-state="on"], .widget[data-state="open"], .widget[data-state="opening"] {
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent-warm) 14%, var(--bg-elev)), var(--bg-elev));
  border-color: color-mix(in srgb, var(--accent-warm) 35%, var(--border));
}
.widget[data-state="on"] .w-ico,
.widget[data-state="open"] .w-ico,
.widget[data-state="opening"] .w-ico { color: var(--accent-warm); }
.widget[data-state="opening"] .w-ico,
.widget[data-state="closing"] .w-ico { animation: pulse 1s infinite; }
.widget[data-state="unavailable"] { opacity: 0.4; cursor: not-allowed; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.5; } }

.w-slider { width: 100%; margin-top: 10px; }
.w-media { display: flex; gap: 10px; align-items: center; }
.w-media img { width: 56px; height: 56px; border-radius: var(--radius-sm); object-fit: cover; }
.w-media .controls { display: flex; gap: 8px; margin-top: 8px; }
.w-media .controls button { background: var(--bg-elev-2); padding: 6px 10px; border-radius: var(--radius-sm); }

.w-iframe { width: 100%; border: 0; border-radius: var(--radius-sm); }
.w-camera { width: 100%; aspect-ratio: 16/9; background: #000; border-radius: var(--radius-sm); object-fit: cover; }
.w-note { font-size: 13px; line-height: 1.5; color: var(--text-muted); }
.w-note strong { color: var(--text); }
.w-clock { font-size: 32px; font-weight: 700; letter-spacing: -1px; font-variant-numeric: tabular-nums; }
.w-clock-date { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.w-clock-extra { font-size: 12px; color: var(--text); margin-top: 4px; opacity: 0.85; }

/* progress-bar */
.w-progress { width: 100%; height: 6px; background: var(--bg-elev-2); border-radius: 999px; overflow: hidden; margin-top: 8px; }
.w-progress-bar { height: 100%; background: var(--accent); border-radius: inherit; transition: width .3s ease; }
.w-progress[data-level="warn"] .w-progress-bar { background: #f59e0b; }
.w-progress[data-level="crit"] .w-progress-bar { background: #ef4444; }

/* sparkline */
.w-sparkbox { margin-top: 8px; line-height: 0; color: var(--accent); }
.w-spark { width: 100%; height: 28px; display: block; }

/* data-list */
.w-list-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.w-list-head .w-ico { margin-bottom: 0; width: 24px; height: 24px; }
.w-list-head .w-ico i, .w-list-head .w-ico svg { width: 18px; height: 18px; }
.w-list-head .w-title { font-size: 13px; }
.w-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.w-list-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 8px; background: var(--bg-elev-2); border-radius: var(--radius-sm); }
.w-list-main { flex: 1; min-width: 0; }
.w-list-title { font-size: 12px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-list-sub { font-size: 10px; color: var(--text-muted); }
.w-list-value { font-size: 12px; font-weight: 600; color: var(--text); font-variant-numeric: tabular-nums; }
.w-list-empty { font-size: 11px; color: var(--text-muted); padding: 4px 8px; }

/* error/stale states for data widgets */
.w-err { color: #ef4444; }
.w-stale { color: var(--text-muted); font-style: italic; }

.chip { display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: 999px; background: var(--bg-elev); border: 1px solid var(--border); font-size: 12px; color: var(--text-muted); }

.empty { display: flex; align-items: center; justify-content: center; min-height: 60vh; color: var(--text-muted); text-align: center; }

/* ============ Toasts ============ */
.toast-stack { position: fixed; bottom: 24px; right: 24px; display: flex; flex-direction: column; gap: 8px; z-index: 1000; pointer-events: none; }
.toast { background: var(--bg-elev-2); color: var(--text); padding: 10px 16px; border-radius: var(--radius-sm); box-shadow: 0 4px 12px rgba(0,0,0,.4); border-left: 3px solid var(--accent); font-size: 13px; min-width: 220px; animation: slideIn .25s ease; }
.toast-error { border-left-color: #ef4444; }
.toast-ok { border-left-color: #4ade80; }
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ============ Responsive ============ */
.mobile-menu-btn { display: none; }
.mobile-overlay { display: none; }

@media (max-width: 900px) {
  body { grid-template-columns: 1fr; }
  .sidebar { position: fixed; top: 0; left: 0; bottom: 0; z-index: 100; transform: translateX(-100%); transition: transform .2s; width: 260px; }
  .sidebar.mobile-open { transform: translateX(0); box-shadow: 0 0 30px rgba(0,0,0,.5); }
  .mobile-menu-btn {
    display: flex;
    position: fixed;
    top: 14px;
    left: 14px;
    z-index: 101;
    width: 32px;
    height: 32px;
    padding: 0;
    align-items: center;
    justify-content: center;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-muted);
  }
  .mobile-menu-btn i, .mobile-menu-btn svg { width: 18px; height: 18px; }
  .mobile-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 99;
  }
  .topbar { padding-left: 50px; }
  .layout-grid-4, .layout-grid-3 { grid-template-columns: repeat(2, 1fr); }
  .layout-rooms { grid-template-columns: 1fr; }
  .widget[data-size="2x1"], .widget[data-size="2x2"], .widget[data-size="3x1"], .widget[data-size="3x2"] { grid-column: span 2; }
  .page-title { font-size: 22px; }
}
@media (max-width: 480px) {
  .main {
    padding: 0 12px 30px;
    /* Яндекс/Chrome поисковая панель снизу — добавляем щедрый отступ */
    padding-bottom: calc(120px + env(safe-area-inset-bottom, 0px));
  }
  :root { --gap: 8px; }
  .tab-body { gap: 18px; }
  .layout-grid-4, .layout-grid-3, .layout-grid-2 { grid-template-columns: repeat(2, 1fr); }
  .widget[data-size="2x1"], .widget[data-size="2x2"], .widget[data-size="3x1"], .widget[data-size="3x2"] { grid-column: span 2; }
  /* компактные плитки: иконка слева + текст справа */
  .widget {
    padding: 10px 12px;
    min-height: 0;
    display: grid;
    grid-template-columns: 36px 1fr;
    grid-template-rows: auto auto;
    column-gap: 10px;
    row-gap: 0;
    align-items: center;
  }
  .w-ico {
    grid-row: 1 / 3;
    margin-bottom: 0;
    width: 36px; height: 36px;
  }
  .w-ico i, .w-ico svg { width: 24px; height: 24px; }
  .w-ico-img { width: 28px; height: 28px; }
  .w-title { grid-column: 2; font-size: 13px; }
  .w-sub { grid-column: 2; font-size: 11px; margin-top: 0; }
  .w-value { grid-column: 2; grid-row: 1; font-size: 18px; margin-top: 0; }
  /* clock/weather/camera/iframe/media/list — оставляем вертикальный layout */
  .widget.w-clock, .widget.w-weather, .widget.w-camera, .widget.w-iframe,
  .widget.w-media_player, .widget.w-note, .widget.w-data_list,
  .widget.w-sparkline, .widget.w-progress {
    display: block;
    padding: 14px;
  }
  .widget.w-weather .w-ico { margin-bottom: 8px; width: 28px; height: 28px; }
  .widget.w-clock { grid-column: span 2; }
  .widget.w-clock .w-clock { font-size: 28px; }
  .w-clock-extra { font-size: 11px; }
}
