:root {
  --bar:#263847; /* Sidebar background color */
  --ink:#ffffff; 
  --pill:#0f172a; /* Active nav item background */
  --bg:#eef2f5; /* Page body/Main content background */
  --ok:#10b981; 
  --warn:#f59e0b; 
  --err:#ef4444;
  --sidebar-width:250px;
  --sidebar-collapsed-width:70px;
  --header-height: 56px; 
  --header-bg: #1f2a36; 
  --header-border: #334155; 
  --main-ink: #334155;

  /* Phone Variables */
  --phone-bg: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
  --phone-text: #f8fafc;
  --phone-accent: #34d399;
  --phone-muted: #94a3b8;
  --key-bg: rgba(255, 255, 255, 0.08);
}

/* -------------------------- DARK MODE VARIABLES -------------------------- */
body.dark-theme {
  --bar:#0f172a; /* Dark Sidebar background */
  --ink:#ffffff; 
  --pill:#1e293b; /* Active nav item background */
  --bg:#1e293b; /* Dark Page body/Main content background */
  --ok:#10b981; 
  --warn:#f59e0b; 
  --err:#ef4444;
  --header-bg: #0f172a; 
  --header-border: #334155;
  --main-ink: #f8fafc;
}

/* -------------------------- DARK MODE STYLES -------------------------- */
body.dark-theme .main-header {
  background: var(--header-bg);
  border-bottom-color: var(--header-border);
}

body.dark-theme .main-header .weather-display {
    color: var(--ink);
}

body.dark-theme .weather-tooltip {
    background: #1e2a36; 
    color: var(--ink);
    border: 1px solid var(--header-border);
}

body.dark-theme .sidebar {
    background: var(--bar);
}

body.dark-theme .user {
    background: #1e293b;
    box-shadow:inset 0 0 0 1px rgba(255,255,255,.12);
}
body.dark-theme .dropdown-menu {
    background: #1e293b;
    box-shadow:0 -10px 24px rgba(0,0,0,.5),inset 0 0 0 1px rgba(255,255,255,.1);
}
body.dark-theme .dropdown-menu a:hover {
    background: #334155;
}

body.dark-theme .modal-content {
    background-color: var(--bg);
}
body.dark-theme .modal-header {
    background: var(--header-bg);
}
body.dark-theme .close-modal:hover,
body.dark-theme .close-modal:focus {
    color: var(--ink);
}
/* -------------------------- END DARK MODE STYLES -------------------------- */

* {box-sizing:border-box}
html,body {height:100%;margin:0;font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,"Helvetica Neue",Arial}

/* -------------------------- CSS GRID LAYOUT FIX -------------------------- */
body {
  background:var(--bg);
  display:grid; 
  grid-template-columns: var(--sidebar-width) 1fr; 
  grid-template-rows: calc(var(--header-height) + env(safe-area-inset-top)) 1fr;
  padding-top:env(safe-area-inset-top);
  padding-bottom:env(safe-area-inset-bottom);
  padding-left:0;
  padding-right:0;
  overflow: hidden; 
}

/* -------------------------- MAIN HEADER -------------------------- */
.main-header {
  background: var(--header-bg); 
  color: #fdfcfc;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between; 
  padding: 0 16px;
  flex-shrink: 0;
  position: fixed; 
  top: 0;
  left: 0;
  right: 0;
  z-index: 1100; 
  border-bottom: 1px solid #ddd;
  padding-top:env(safe-area-inset-top);
  height: calc(var(--header-height) + env(safe-area-inset-top)); 
  grid-column: 1 / -1; 
  grid-row: 1 / 2;
  border-bottom-color: var(--header-border);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px; 
}

.header-logo {
    height: 36px; 
    width: auto;
    border-radius: 4px;
    cursor: pointer;
    display: block; 
}

#sidebarToggle {
  background: none;
  border: none;
  color: var(--ink);
  cursor: pointer;
  font-size: 24px;
  padding: 0;
  line-height: 1;
  display: none; 
  margin-right: 12px; 
  opacity: .85;
  transition: opacity .15s;
}
#sidebarToggle:hover {
  opacity: 1;
}

.main-header .weather-display {
  font-size: 14px;
  font-weight: 700;
  color: #fdfcfc; 
  position: relative;
}

/* -------------------------- WEATHER TOOLTIP -------------------------- */
.weather-tooltip {
    position: absolute;
    top: 100%; 
    left: 0;
    z-index: 2000;
    background: #1e2a36; 
    color: #ffffff;
    border: 1px solid #334155;
    padding: 8px 12px;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    font-size: 12px;
    font-weight: 400;
    line-height: 1.4;
    min-width: 150px;
    display: none; 
    margin-top: 5px; 
    white-space: nowrap; 
    pointer-events: none; 
}

.weather-display:hover .weather-tooltip {
    display: block; 
}

.tooltip-line {
    display: flex;
    justify-content: space-between;
    gap: 20px;
}
.tooltip-line strong {
    font-weight: 700;
}
.tooltip-line:not(:last-child) {
    margin-bottom: 2px;
}

/* -------------------------- HEADER CONTROLS -------------------------- */
.main-header .right-controls {
  display: flex;
  align-items: center;
  gap: 16px; 
}

.header-icon {
    font-size: 22px;
    cursor: pointer;
    opacity: .85;
    transition: opacity .15s;
    color: var(--ink);
}
.header-icon:hover {
    opacity: 1;
}

/* -------------------------- NOTIFICATION STYLES -------------------------- */
.notification-wrapper {
    position: relative;
    display: inline-flex;
    align-items: center;
    cursor: pointer;
}
.notif-badge {
    position: absolute;
    top: -4px;
    right: -6px;
    background: var(--err);
    color: white;
    font-size: 10px;
    font-weight: bold;
    border-radius: 10px;
    padding: 2px 5px;
    display: none; /* Hidden by default */
    min-width: 18px;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
.toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.toast {
    background: #1e293b;
    color: white;
    padding: 14px 18px;
    border-radius: 8px;
    box-shadow: 0 6px 16px rgba(0,0,0,0.4);
    border-left: 4px solid var(--ok);
    animation: slideInRight 0.3s ease forwards;
    font-size: 14px;
    line-height: 1.4;
    max-width: 300px;
    word-wrap: break-word;
    transition: opacity 0.3s ease;
    cursor: pointer;
}

.toast:hover {
    background: #334155; 
}
.toast strong {
    color: var(--ok);
    display: block;
    margin-bottom: 4px;
}
@keyframes slideInRight {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

.garage-control {
  display: flex; 
  align-items: center;
}
.garage-btn {
  display:flex;flex-direction:column;align-items:center;justify-content:center;
  width:70px;height:40px;border:none;border-radius:18px; 
  font-size:10px;font-weight:700;color:#fff; 
  cursor:pointer;transition:transform .1s, background .3s;
  box-shadow:0 4px 12px rgba(0,0,0,.3);
  padding: 0;
}

.garage-unk {background:#ccc!important;}
.garage-closed {background:var(--ok)!important;} 
.garage-open {background:var(--err)!important;}   

/* --- Header User Avatar Styles --- */
.main-header .user {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    position: relative;
}

.main-header .user .pic {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px; 
    height: 36px;
    background: #0f172a; 
    color: #fff;
    border-radius: 50%; 
    font-size: 14px;
    font-weight: 700;
    opacity: 1; 
    flex-shrink: 0;
}

/* -------------------------- SIDEBAR -------------------------- */
.sidebar {
  background:var(--bar);color:var(--ink);
  display:flex;flex-direction:column;gap:16px;
  padding:8px 12px;
  position:fixed; 
  top:calc(var(--header-height) + env(safe-area-inset-top)); 
  left:0;
  bottom:0;
  width:var(--sidebar-width);
  z-index:1000; 
  overflow-y:auto;
  padding-top:8px; 
  padding-bottom:calc(8px + env(safe-area-inset-bottom));
  transition:width .3s ease; 
  grid-column: 1 / 2;
  grid-row: 2 / 3;
}

.brand {
  font-weight:400;
  font-size:20px;
  cursor:pointer;
  transition:font-size .3s ease;
  padding: 0; 
  height: auto; 
}

.collapse-header {
  display:flex;justify-content:space-between;align-items:center;width:100%;margin-bottom:8px;
}

.nav {
  flex-direction:column;
  gap:4px;
  align-items:stretch;
  flex-wrap:nowrap;
}

.nav a {
  color:var(--ink);text-decoration:none;font-weight:600;
  padding:10px 12px;border-radius:10px;font-size:14px;
  opacity:.9;display:flex;align-items:center;gap:8px;
  transition:transform .06s,background .2s,opacity .2s;
}
.nav a:hover {opacity:1;transform:translateX(2px)}
.nav a.active {background:var(--pill);box-shadow:inset 0 0 0 1px rgba(255,255,255,.08)}

.nav .icon {width:22px;text-align:center;flex-shrink:0;}
.nav .label {flex:1;}

.nav .dropdown {position:relative;margin-top:8px;}
.admin-trigger {color:var(--ink);text-decoration:none;font-weight:600;
  padding:10px 12px;border-radius:10px;font-size:14px;
  opacity:.9;display:flex;align-items:center;gap:8px;}
.nav .dropdown.open .admin-trigger {
  background:var(--pill);box-shadow:inset 0 0 0 1px rgba(255,255,255,.08);
}
.admin-menu {
  position:relative;top:auto;left:0;right:0;margin-top:4px;
  min-width:210px;background:rgba(0,0,0,.15);color:#fff;
  border-radius:8px;padding:4px 0;
  display:none;flex-direction:column;z-index:1200;
}
.dropdown.open .admin-menu {display:flex;}
.admin-menu a {
  padding:8px 14px;color:#fff;text-decoration:none;font-weight:600;font-size:14px;
}
.admin-menu a:hover {background:#1e293b}

/* User elements */
.sidebar .bottom-group {
  margin-top:auto;
  display:flex;flex-direction:column;align-items:stretch;gap:10px;
}
.user {
  display:flex;align-items:center;gap:8px;background:#1f2a36;
  padding:10px;border-radius:10px;box-shadow:inset 0 0 0 1px rgba(255,255,255,.06);
  cursor:pointer;font-size:14px;position:relative;
  transition:background .3s ease,padding .3s ease,box-shadow .3s ease;
  white-space: nowrap; 
}
.user .pic {font-size:17px;opacity:.9}
.user .name {font-weight:700}
.dropdown-menu {
  position:absolute;bottom:100%;left:0;margin-bottom:6px;
  min-width:180px;background:#0f172a;color:#fff;
  border-radius:12px;padding:6px 0;
  box-shadow:0 -10px 24px rgba(0,0,0,.35),inset 0 0 0 1px rgba(255,255,255,.06);
  display:none;flex-direction:column;z-index:1200;
}
.user.open .dropdown-menu {display:flex;}
.dropdown-menu a {
    color: #fff;
    text-decoration: none;
    padding: 8px 14px;
    font-size: 14px;
    font-weight: 600;
}
.dropdown-menu a:hover {
    background: #1e293b;
}

/* -------------------------- MAIN CONTENT -------------------------- */
#main-container {
  grid-column: 2 / 3;
  grid-row: 2 / 3;
  display: flex;
  flex-direction: column;
  height: 100%; 
  overflow-y: auto; 
  position: static;
  margin: 0; 
  transition: none;
  z-index: 1; 
}

.sidebar-spacer { display: none; }

#main-container iframe {
  flex: 1;
  width: 100%;
  height: 100%; /* Important so it fills the container */
  border: 0;
  background: var(--bg); 
  -webkit-overflow-scrolling: touch;
  margin-top: 0; 
}

/* -------------------------- SETUP MODAL STYLES -------------------------- */
.modal {
    display: none; 
    position: fixed; 
    z-index: 2500; 
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%; 
    overflow: hidden; 
    background-color: rgba(0,0,0,0.5); 
    backdrop-filter: blur(4px);
}

.modal-content {
    background-color: #fefefe;
    margin: 5vh auto; 
    padding: 0;
    border: 1px solid #888;
    width: 90%;
    max-width: 800px; 
    height: 80vh; 
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    padding: 12px 16px;
    background: #1f2a36;
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
}

.close-modal {
    color: #aaa;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
    line-height: 1;
}

.close-modal:hover,
.close-modal:focus {
    color: #fff;
    text-decoration: none;
    cursor: pointer;
}

.modal-body {
    flex: 1;
    width: 100%;
    height: 100%;
    position: relative;
}

.modal-body iframe {
    width: 100%;
    height: 100%;
    border: none;
}

/* -------------------------- PHONE MODAL STYLES -------------------------- */
#phoneModal .modal-body {
    padding: 0;
    background: var(--phone-bg);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.phone-interface {
    width: 100%; height: 100%;
    display: flex; flex-direction: column;
    color: var(--phone-text);
    font-family: -apple-system, sans-serif;
}
.phone-tabs {
    display: flex; background: rgba(0,0,0,0.2);
    margin: 10px 15px; 
    padding: 4px; border-radius: 12px;
    flex-shrink: 0;
}
.p-tab {
    flex: 1; text-align: center; padding: 8px;
    color: var(--phone-muted); cursor: pointer;
    border-radius: 8px; font-size: 13px; font-weight: 600;
    transition: all 0.2s;
}
.p-tab.active { background: rgba(255,255,255,0.1); color: var(--phone-text); }

.p-view { 
    display: none; 
    flex: 1; 
    flex-direction: column; 
    overflow-y: hidden;
    padding-bottom: 10px;
}
.p-view.active { display: flex; }

.p-display {
    flex: 0 0 auto; 
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    min-height: 90px; 
    margin-bottom: 5px;
}
#p-input {
    font-size: 34px; 
    background: transparent; border: none;
    color: var(--phone-text); text-align: center; width: 80%;
    outline: none; pointer-events: none;
}
#p-status {
    height: 18px; font-size: 12px; color: var(--phone-accent); margin-top: 2px;
}

.p-from-wrap { 
    width: 100%; padding: 0 30px; 
    margin-bottom: 15px; 
    text-align: center; 
    flex-shrink: 0; 
}
.p-from-select {
    background-color: #1e293b; 
    border: 1px solid rgba(255,255,255,0.1);
    color: #34d399; 
    padding: 8px 15px;
    border-radius: 15px; font-size: 13px; outline: none;
}
.p-from-select option {
    background-color: #1e293b;
    color: #ffffff;
}

.p-grid {
    display: grid; grid-template-columns: repeat(3, 1fr);
    gap: 10px; 
    padding: 0 30px; 
    justify-items: center;
    margin-top: auto; 
    margin-bottom: 10px;
}
.p-key {
    width: 60px; height: 60px; 
    border-radius: 50%;
    background: var(--key-bg);
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    cursor: pointer; transition: background 0.2s;
    user-select: none;
}
.p-key:hover { background: rgba(255,255,255,0.15); }
.p-key:active { transform: scale(0.95); }
.pk-num { font-size: 24px; line-height: 1; }
.pk-sub { font-size: 8px; opacity: 0.6; text-transform: uppercase; }

.p-call-btn {
    grid-column: 2; 
    width: 64px; height: 64px; 
    border-radius: 50%; background: var(--phone-accent);
    color: #064e3b; border: none; font-size: 28px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; margin-top: 5px;
    box-shadow: 0 0 15px rgba(52, 211, 153, 0.4);
}
.p-call-btn:active { transform: scale(0.95); }

.p-cancel-btn {
    grid-column: 2; 
    width: 64px; height: 64px;
    border-radius: 50%; 
    background: #ef4444; 
    color: #fff; border: none; font-size: 28px;
    display: none; 
    align-items: center; justify-content: center;
    cursor: pointer; margin-top: 5px;
    box-shadow: 0 0 15px rgba(239, 68, 68, 0.4);
}
.p-cancel-btn:active { transform: scale(0.95); }

.p-contact-list { padding: 15px; overflow-y: auto; flex: 1; }
.p-contact {
    display: flex; align-items: center;
    background: rgba(255,255,255,0.05);
    padding: 12px; margin-bottom: 8px;
    border-radius: 12px; cursor: pointer;
    position: relative;
}
.p-contact:hover { background: rgba(255,255,255,0.1); }
.pc-avatar {
    width: 36px; height: 36px; border-radius: 50%; background: #3b82f6;
    display: flex; align-items: center; justify-content: center;
    margin-right: 12px; font-weight: bold; color: #fff;
}
.pc-info { display: flex; flex-direction: column; flex: 1; overflow: hidden;}
.pc-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pc-num { font-size: 12px; opacity: 0.7; }

.p-contact-header {
    display: flex; flex-direction: column;
    padding: 10px 15px; border-bottom: 1px solid rgba(255,255,255,0.1);
    gap: 8px;
}
.p-add-row { display: flex; justify-content: space-between; align-items: center; }
.p-search-input { 
    background: rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.1);
    color: white; padding: 8px 12px; border-radius: 8px; outline: none; font-size: 13px;
}
.p-add-btn {
    background: var(--phone-accent); color: #064e3b; border: none;
    padding: 6px 12px; border-radius: 8px; font-weight: 700; font-size: 12px;
    cursor: pointer;
}
.pc-actions {
    margin-left: auto; display: flex; gap: 8px;
}
.pc-action-btn {
    background: none; border: none; cursor: pointer; opacity: 0.6; font-size: 16px; padding: 4px;
}
.pc-action-btn:hover { opacity: 1; transform: scale(1.1); }

#p-contact-form {
    display: none; flex-direction: column; gap: 10px; padding: 15px;
    background: var(--phone-bg); height: 100%; overflow-y: auto;
}
#p-contact-form.active { display: flex; }

.pf-input {
    background: rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.1);
    color: white; padding: 10px; border-radius: 8px; outline: none; font-size: 13px;
}
.pf-row { display: flex; gap: 10px; }
.pf-btn {
    padding: 12px; border-radius: 8px; border: none; font-weight: bold; cursor: pointer; flex: 1;
}
.pf-save { background: var(--phone-accent); color: #064e3b; }
.pf-cancel { background: rgba(255,255,255,0.1); color: #fff; }

/* -------------------------- COLLAPSED SIDEBAR (DESKTOP) -------------------------- */
@media (min-width:769px){
  body.sidebar-collapsed {
    grid-template-columns: var(--sidebar-collapsed-width) 1fr;
  }
  body.sidebar-collapsed .sidebar {width:var(--sidebar-collapsed-width);overflow:hidden;}
  
  .brand {
    text-indent:-9999px; 
    overflow: hidden; 
    white-space: nowrap;
    font-size: 0; 
  }

  .brand::before {
      content:"⛺"; 
      font-size:26px;
      display:block;
      text-indent:0;
      text-align:center;
  }

  body.sidebar-collapsed .nav a {
    justify-content:center;padding:10px;
  }
  body.sidebar-collapsed .nav .label {display:none;}
  body.sidebar-collapsed .nav .caret {display:none;}

  body.sidebar-collapsed .admin-trigger {justify-content:center;padding:10px;}
  body.sidebar-collapsed .admin-trigger .label {display:none;}
  
  .sidebar .bottom-group {
    display: none;
  }
  
  .main-header .user .dropdown-menu {
      top: calc(var(--header-height) + env(safe-area-inset-top) - 6px); 
      bottom: auto; 
      left: auto;
      right: 16px; 
      margin-bottom: 0;
      box-shadow:0 10px 24px rgba(0,0,0,.35),inset 0 0 0 1px rgba(255,255,255,.06);
  }
  .main-header .user {
      background: none; 
      padding: 0;
      box-shadow: none;
  }
  .main-header .user .pic {
      font-size: 14px; 
  }
  .main-header .user .name,
  .main-header .user .caret {
      display: none; 
  }
}

/* -------------------------- MOBILE LAYOUT -------------------------- */
@media (max-width:768px){
  body {
    display: flex; 
    flex-direction:column;
    padding-top:0;
    padding-bottom:0;
    position: relative; 
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  } 
  
  .sidebar {
      top:0;left:0;bottom:0;width:var(--sidebar-width); 
      flex-direction:column;
      height:100vh;
      padding-top:calc(8px + env(safe-area-inset-top));
      margin-left: calc(0px - var(--sidebar-width)); 
      transition: margin-left .3s ease; 
      bottom:0; 
      z-index: 1050; 
      position: fixed; 
  }
  body.sidebar-open .sidebar {margin-left: 0;}

  .collapse-header {margin-bottom:8px;} 
  #sidebarToggle {display:block!important;} 
  body.sidebar-open .nav {display:flex!important;} 
  
  .sidebar .bottom-group {
      margin-top: auto;
      display:flex;
      flex-direction:column;
      margin-left:0;
      align-items:stretch;
      padding-top:0;
  }
  .user {margin-top:0;}
  .user .caret {display:inline-block;}
  
  .main-header {
    display:flex;
    position: static; 
    background: #334155; 
  } 

  .main-header .user .dropdown-menu {
      top: calc(var(--header-height) + env(safe-area-inset-top) - 6px); 
      bottom: auto; 
      left: auto;
      right: 8px; 
      margin-bottom: 0;
      box-shadow:0 10px 24px rgba(0,0,0,.35),inset 0 0 0 1px rgba(255,255,255,.06);
  }
  
  #main-container {
    position: static; 
    margin-left:0; 
    margin-top:0; 
    height:calc(100vh - var(--header-height) - env(safe-area-inset-top));
    overflow: auto;
    flex: 1;
  }

  .garage-btn {
    width:50px; 
    height:30px;
    border-radius:15px;
    font-size:9px;
  }
}