/* Copied from Battery_management static CSS to unify styling across apps.
   Note: If there are differences, adjust selectors accordingly. */
/* Placeholder: the actual CSS from Battery_management/static/css/dashboard.css will be referenced.
   Since direct copying line-by-line isn't shown here, ensure this file exists and is served. */
:root, [data-theme="dark"] {
  --bg: #0b0f1a;
  --bg-grad-1: #0b0f1a;
  --bg-grad-2: #0f172a;
  --panel: rgba(255,255,255,0.06);
  --panel-border: rgba(255,255,255,0.12);
  --panel-hover: rgba(255,255,255,0.08);
  --text: #e6e8ef;
  --text-muted: #9aa4b2;
  --primary: #f97316;
  --primary-600: #ea580c;
  --primary-400: #fb923c;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #3b82f6;
  --ring: rgba(249, 115, 22, 0.45);
}

/* Light theme variables */
[data-theme="light"] {
  --bg: #f7f8fb;
  --bg-grad-1: #eef2f7;
  --bg-grad-2: #e5eaf3;
  --panel: rgba(255,255,255,0.85);
  --panel-border: rgba(0,0,0,0.08);
  --panel-hover: rgba(0,0,0,0.05);
  --text: #1f2937;
  --text-muted: #6b7280;
  --primary: #f97316;
  --primary-600: #ea580c;
  --primary-400: #fb923c;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #3b82f6;
  --ring: rgba(234, 88, 12, 0.35);
}

/* Adjust overlays for light */
[data-theme="light"] body::before { opacity: 0.08; }
[data-theme="light"] body::after { background: #ffffff; opacity: 0.6; }

html { overflow-x: hidden; height: 100%; }
body {
  background: radial-gradient(1200px 600px at 10% -10%, rgba(249,115,22,0.10) 0%, rgba(249,115,22,0) 60%),
              radial-gradient(900px 500px at 100% 10%, rgba(59,130,246,0.10) 0%, rgba(59,130,246,0) 60%),
              linear-gradient(180deg, var(--bg-grad-1) 0%, var(--bg-grad-2) 100%);
  background-attachment: fixed;
  color: var(--text);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  overflow-x: hidden;
  min-height: 100%;
  display: flex;
  flex-direction: column;
}
.glass-card { background: var(--panel); border: 1px solid var(--panel-border); border-radius: 16px; backdrop-filter: blur(12px); }
.card-header-gradient { padding: 12px 16px; background: linear-gradient(135deg, var(--primary) 0%, var(--primary-600) 100%); color: #fff; border-top-left-radius: 16px; border-top-right-radius: 16px; }
.stat-card { box-shadow: 0 8px 30px rgba(0,0,0,0.35); }
.stat-label { color: var(--text-muted); font-size: 0.9rem; }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-icon { width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center; }
.progress-custom { height: 8px; background: var(--panel-hover); border-radius: 8px; overflow: hidden; }
.progress-bar-custom { height: 100%; background: linear-gradient(90deg, var(--primary), var(--primary-400)); color: #fff; font-size: 0.75rem; text-align: right; padding-right: 6px; }

.theme-toggle { position: fixed; top: 20px; right: 20px; width: 50px; height: 50px; background: var(--panel); border: 1px solid var(--panel-border); border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; z-index: 1000; transition: all 0.3s ease; }
.theme-toggle:hover { transform: rotate(180deg); box-shadow: 0 10px 40px rgba(249, 115, 22, 0.25); }
.theme-toggle i { color: var(--primary); font-size: 20px; }

/* Light theme global overrides */
[data-theme="light"] body { background: linear-gradient(180deg, var(--bg-grad-1) 0%, var(--bg-grad-2) 100%) !important; color: var(--text) !important; }
[data-theme="light"] .theme-toggle { background: #fff; border-color: var(--border-color); box-shadow: 0 2px 8px rgba(0,0,0,0.10); }
[data-theme="light"] .main-wrapper,
[data-theme="light"] .content-wrapper { background: transparent !important; }
[data-theme="light"] .card,
[data-theme="light"] .stat-card,
[data-theme="light"] .info-card { background: var(--card-bg) !important; border-color: var(--border-color) !important; color: var(--text) !important; }
[data-theme="light"] .table-dark { --bs-table-bg: #f8fafc; --bs-table-striped-bg: #f1f5f9; --bs-table-hover-bg: #e2e8f0; --bs-table-color: #1a202c; --bs-table-border-color: #cbd5e1; color: #1a202c !important; }
[data-theme="light"] .table-dark thead th { background: #e2e8f0 !important; color: #374151 !important; border-color: #cbd5e1 !important; }
[data-theme="light"] code { background: rgba(0,0,0,0.06); color: #b45309; padding: .1em .3em; border-radius: 4px; }
[data-theme="light"] .badge.bg-secondary { background-color: #e2e8f0 !important; color: #374151 !important; }
[data-theme="light"] .badge.bg-dark { background-color: #374151 !important; color: #f9fafb !important; }
[data-theme="light"] .text-muted { color: var(--text-muted) !important; }
[data-theme="light"] .text-warning { color: #b45309 !important; }
[data-theme="light"] .form-control { background: #fff; border-color: var(--border-color); color: var(--text); }
[data-theme="light"] .form-control:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); }

/* Sidebar Toggle Button (Inside Sidebar) */
.sidebar-toggle-btn {
  width: 32px;
  height: 32px;
  background: rgba(249, 115, 22, 0.1);
  border: 1px solid rgba(249, 115, 22, 0.3);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: var(--primary);
  font-size: 16px;
  padding: 0;
  margin-left: auto;
  flex-shrink: 0;
}

.sidebar-toggle-btn:hover {
  background: rgba(249, 115, 22, 0.2);
  border-color: var(--primary);
  transform: scale(1.05);
}

.sidebar-toggle-btn i {
  transition: transform 0.3s ease;
}

.sidebar.collapsed .sidebar-toggle-btn i {
  transform: rotate(180deg);
}

/* Hide on mobile */
@media (max-width: 768px) {
  .sidebar-toggle-btn {
    display: none;
  }
}

.list-unstyled a {
  text-decoration: none !important;
}

/* Back Button - same style as theme toggle */
.back-button { position: fixed; top: 20px; left: 20px; width: 50px; height: 50px; background: var(--panel); border: 1px solid var(--panel-border); border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; z-index: 1000; transition: all 0.3s ease; text-decoration: none; }
.back-button:hover { transform: translateX(-3px); box-shadow: 0 10px 40px rgba(249, 115, 22, 0.25); border-color: var(--primary); }
.back-button i { color: var(--primary); font-size: 20px; }

.user-menu { position: fixed; top: 20px; left: 20px; background: var(--panel); border: 1px solid var(--panel-border); border-radius: 12px; padding: 10px 12px; }
.user-menu .user-info { display:flex; align-items:center; gap:8px; color: var(--text); }
.user-menu .user-dropdown { display:flex; gap:12px; margin-top:8px; }

/* Ensure media scales and doesn't overflow */
img, svg, canvas, video { max-width: 100%; height: auto; display: block; }

/* Tables and pre/code blocks responsiveness */
.table { width: 100%; max-width: 100%; }
.table-responsive, .table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
pre, code, .code-block { max-width: 100%; overflow-x: auto; display: block; }

/* Container sizing */
.container, .container-fluid { width: 100%; max-width: 100%; box-sizing: border-box; }

/* Media queries for responsive sidebar */
@media (max-width: 992px) {
  .sidebar.expand {
    flex-basis: 220px;
  }
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    width: 200px;
    transform: translateX(-100%);
    z-index: 1000;
  }

  .sidebar.sidebar-open {
    transform: translateX(0);
  }

  .page-content {
    flex: 1 1 auto;
    min-width: 0;
  }
}

/* Background pattern overlay for pages that need it */
body::before {
  content: "";
  position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background-image: url('../images/background_pattern.png');
  background-size: cover; background-position: center; background-repeat: no-repeat;
  opacity: 0.25; z-index: -2; pointer-events: none;
}

/* Dark overlay */
body::after {
  content: "";
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: var(--bg); opacity: 0.35; z-index: -1; pointer-events: none;
}

/* Login page styles */
body.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; overflow-x: hidden; }
.login-container { background: var(--panel); backdrop-filter: blur(12px); border: 1px solid var(--panel-border); border-radius: 16px; box-shadow: 0 8px 30px rgba(0,0,0,0.35); overflow: hidden; width: 100%; max-width: 420px; z-index: 1; margin: auto; }
.login-header { background: linear-gradient(135deg, var(--primary) 0%, var(--primary-600) 100%); color: #fff; padding: 35px 25px; text-align: center; }
.header-icon { width: 70px; height: 70px; background: rgba(255, 255, 255, 0.2); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 15px; backdrop-filter: blur(10px); }
.header-icon i { font-size: 32px; color: white; }
.login-header h1 { font-size: 26px; margin-bottom: 8px; font-weight: 700; }
.login-header p { font-size: 14px; opacity: 0.95; }
.login-body { padding: 35px 30px; background: var(--bg); }
.form-group { margin-bottom: 22px; }
.form-group label { display: flex; align-items: center; margin-bottom: 8px; color: var(--text); font-weight: 500; font-size: 14px; }
.form-group label i { margin-right: 8px; color: var(--primary); width: 16px; text-align: center; }
.form-group input[type="text"],
.form-group input[type="password"],
.form-group input[type="email"] { width: 100%; padding: 13px 16px; border: 2px solid var(--panel-border); border-radius: 8px; font-size: 14px; background: var(--panel); color: var(--text); transition: all 0.3s; }
.form-group input[type="text"]:focus,
.form-group input[type="password"]:focus,
.form-group input[type="email"]:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); background: var(--bg); }
.btn-login { width: 100%; padding: 14px; background: linear-gradient(135deg, var(--primary) 0%, var(--primary-600) 100%); color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: 600; cursor: pointer; transition: transform 0.2s, box-shadow 0.2s; display: flex; align-items: center; justify-content: center; gap: 8px; }
.btn-login:hover { transform: translateY(-2px); box-shadow: 0 10px 40px rgba(249, 115, 22, 0.25); }
.btn-login:active { transform: translateY(0); }
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 20px; font-size: 14px; border: 1px solid; }
.alert-success { background: rgba(16, 185, 129, 0.15); color: var(--success); border-color: rgba(16, 185, 129, 0.3); }
.alert-danger { background: rgba(239, 68, 68, 0.15); color: var(--danger); border-color: rgba(239, 68, 68, 0.3); }
.alert-warning { background: rgba(245, 158, 11, 0.15); color: var(--warning); border-color: rgba(245, 158, 11, 0.3); }
.alert-info { background: rgba(59, 130, 246, 0.15); color: var(--info); border-color: rgba(59, 130, 246, 0.3); }
.login-footer { text-align: center; padding: 20px; background: var(--panel-hover); color: var(--text-muted); font-size: 12px; }

/* Sidebar */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 70px;
  min-width: 70px;
  background: var(--panel);
  border-right: 1px solid var(--panel-border);
  backdrop-filter: blur(10px);
  z-index: 100;
  overflow-y: auto;
  overflow-x: hidden;
  transition: all 0.25s ease-in-out;
  scrollbar-width: thin;
  scrollbar-color: var(--panel-border) transparent;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}

.sidebar.expand {
  width: 240px;
  min-width: 240px;
}

.sidebar::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
  background: var(--panel-border);
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
  background: var(--primary);
}

/* Hide text and logo when not expanded */
.sidebar:not(.expand) .brand-text,
.sidebar:not(.expand) .brand-logo,
.sidebar:not(.expand) .nav a span {
  display: none;
}

.sidebar .brand {
  height: 64px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 10px;
  border-bottom: 1px solid var(--panel-border);
  color: var(--text);
  white-space: nowrap;
  overflow: visible;
  position: relative;
}

.sidebar .brand-logo {
  height: 24px;
  width: auto;
  object-fit: contain;
  transition: all 0.3s ease;
  flex-shrink: 0;
}

.sidebar .brand-text {
  transition: opacity 0.2s ease;
  white-space: nowrap;
  flex-grow: 1;
}

/* Center toggle button when collapsed */
.sidebar:not(.expand) .brand {
  padding: 0;
  justify-content: center;
}

.sidebar:not(.expand) .sidebar-toggle-btn {
  margin-left: 0;
}


.sidebar .nav {
  list-style: none;
  margin: 0;
  padding: 12px;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
}

.sidebar .nav a {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  color: var(--text);
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 4px;
  transition: all 0.2s;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  position: relative;
}

.sidebar:not(.expand) .nav a {
  justify-content: center;
  padding: 10px 8px;
}

.sidebar .nav a:hover {
  background: var(--panel-hover);
}

.sidebar .nav a i {
  width: 20px;
  text-align: center;
  flex-shrink: 0;
}

/* Sidebar footer with logout button */
.sidebar-footer {
  border-top: 1px solid var(--panel-border);
  padding: 12px;
  margin-top: auto;
}

.sidebar-footer .sidebar-link {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  color: var(--danger);
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 8px;
  transition: all 0.2s;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
}

.sidebar:not(.expand) .sidebar-footer .sidebar-link {
  justify-content: center;
  padding: 10px 8px;
}

.sidebar:not(.expand) .sidebar-footer .sidebar-link span {
  display: none;
}

.sidebar-footer .sidebar-link:hover {
  background: rgba(239, 68, 68, 0.1);
}

.sidebar-footer .sidebar-link i {
  width: 20px;
  text-align: center;
  flex-shrink: 0;
  font-size: 18px;
}

.sidebar.collapsed .nav a i {
  font-size: 20px;
}

.sidebar .nav a span {
  flex-grow: 1;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.sidebar.collapsed .nav a span {
  opacity: 0;
  transform: translateX(-20px);
  position: absolute;
  pointer-events: none;
}

/* Tooltip for collapsed sidebar */
.sidebar.collapsed .nav a {
  position: relative;
}

.sidebar.collapsed .nav a:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-left: 10px;
  padding: 6px 12px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  font-size: 14px;
  white-space: nowrap;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  pointer-events: none;
}

.content-with-sidebar {
  margin-left: 240px !important;
  max-width: calc(100vw - 240px) !important;
  overflow-x: hidden;
  padding-right: 12px;
  padding-left: 12px;
  box-sizing: border-box;
  transition: margin-left 0.3s ease, max-width 0.3s ease;
}

/* Collapsed content adjustment - removed, using flex instead */
/* Content now expands automatically with flex layout */

/* Prevent horizontal scroll */
body { overflow-x: hidden; max-width: 100vw; margin: 0; padding: 0; }
.container-fluid {
  max-width: 100%;
  width: 100%;
  padding-right: 15px;
  padding-left: 15px;
  box-sizing: border-box;
  transition: width 0.3s ease;
}
.row { margin-left: 0; margin-right: 0; }
.col, [class*="col-"] { padding-left: 12px; padding-right: 12px; box-sizing: border-box; }

/* Glass cards responsive */
.glass-card { max-width: 100%; box-sizing: border-box; }
.stat-card { max-width: 100%; box-sizing: border-box; }

/* Table responsive wrapper */
.table-responsive { overflow-x: auto; margin-left: 0; margin-right: 0; }

/* Theme toggle overrides to avoid overlap with sidebar */
.theme-toggle { right: 20px; top: 20px; z-index: 1000; transition: all 0.3s; }
.theme-toggle:hover { transform: rotate(180deg); box-shadow: 0 10px 40px rgba(249, 115, 22, 0.25); }
.theme-toggle i { color: var(--primary); font-size: 20px; }

/* Card styles for profile and settings */
.card { background: var(--panel); border: 1px solid var(--panel-border); border-radius: 12px; color: var(--text); }
.card-header { background: linear-gradient(135deg, var(--primary) 0%, var(--primary-600) 100%); color: #fff; border-bottom: 1px solid var(--panel-border); padding: 12px 16px; border-top-left-radius: 12px; border-top-right-radius: 12px; }
.card-body { padding: 20px; background: var(--panel); }
.card h5, .card h6 { color: var(--text); }
.form-control { background: var(--panel); border: 1px solid var(--panel-border); color: var(--text); }
.form-control:focus { background: var(--bg); border-color: var(--primary); color: var(--text); box-shadow: 0 0 0 3px var(--ring); }
.form-control:disabled { background: var(--panel-hover); color: var(--text-muted); }
.form-label { color: var(--text); font-weight: 500; }
.form-select { background: var(--panel); border: 1px solid var(--panel-border); color: var(--text); }
.form-select:focus { background: var(--bg); border-color: var(--primary); color: var(--text); box-shadow: 0 0 0 3px var(--ring); }
.text-muted { color: var(--text-muted) !important; }
.list-group-item { background: var(--panel); border-color: var(--panel-border); color: var(--text); }
.badge { font-weight: 500; }
.btn-primary { background: linear-gradient(135deg, var(--primary) 0%, var(--primary-600) 100%); border: none; }
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(249, 115, 22, 0.3); }
.btn-secondary { background: var(--panel); border: 1px solid var(--panel-border); color: var(--text); }
.btn-secondary:hover { background: var(--panel-hover); color: var(--text); }
.btn-success { background: linear-gradient(135deg, var(--success) 0%, #059669 100%); border: none; color: #fff; }
.btn-success:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(16, 185, 129, 0.3); }
.btn-outline-secondary { color: var(--text); border-color: var(--panel-border); }
.btn-outline-secondary:hover { background: var(--panel); color: var(--text); border-color: var(--panel-border); }
.btn-outline-danger { color: var(--danger); border-color: var(--danger); }
.btn-outline-danger:hover { background: var(--danger); color: #fff; }
.table { color: var(--text); }
.table-hover tbody tr:hover { background: var(--panel-hover); }
.form-check-input { background-color: var(--panel); border-color: var(--panel-border); }
.form-check-input:checked { background-color: var(--primary); border-color: var(--primary); }
.form-check-label { color: var(--text); }

* { box-sizing: border-box; }

/* Cards grid helper for pages with multiple cards */
.cards-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; align-items: stretch; }

/* Prevent cards from exceeding container width */
.card, .glass-card, .stat-card { width: 100%; max-width: 100%; overflow: hidden; }
.card-body, .card-header, .card-footer { max-width: 100%; overflow-wrap: anywhere; }

/* Images inside cards scale properly */
.card img { max-width: 100%; height: auto; display: block; }

/* Ensure Bootstrap rows wrap instead of forcing overflow */
.row { display: flex; flex-wrap: wrap; margin-left: 0; margin-right: 0; }

/* Prevent nested rows from adding overflow due to negative margins */
.row { margin-left: 0; margin-right: 0; }
.col, [class*="col-"] { padding-left: 12px; padding-right: 12px; box-sizing: border-box; }

/* Flash messages and alerts should wrap */
.alert { word-wrap: break-word; overflow-wrap: anywhere; }

/* App shell layout */
.app-shell {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100vw;
  max-width: 100vw;
  overflow-x: hidden;
  min-height: 100vh;
}

.main-wrapper {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;
  transition: all 0.3s ease;
  width: 100%;
  margin-left: 70px;
}

.sidebar.expand ~ .main-wrapper {
  margin-left: 240px;
}

.main-wrapper.content-with-sidebar {
  /* Let flex handle the width automatically */
  margin-left: 70px !important;
  max-width: calc(100vw - 70px) !important;
  width: auto !important;
}

.sidebar.expand ~ .main-wrapper.content-with-sidebar {
  margin-left: 240px !important;
  max-width: calc(100vw - 240px) !important;
}


.page-content {
  flex: 1 1 auto;
  min-width: 0;
  width: 100%;
}

/* Media queries */
@media (max-width: 992px) {
  .sidebar.expand {
    width: 220px;
    min-width: 220px;
  }

  .sidebar.expand ~ .main-wrapper {
    margin-left: 220px;
  }

  .sidebar.expand ~ .main-wrapper.content-with-sidebar {
    margin-left: 220px !important;
    max-width: calc(100vw - 220px) !important;
  }
}

/* Footer styles */
.app-footer {
  background: var(--panel);
  border-top: 1px solid var(--panel-border);
  padding: 1.5rem 2rem;
  margin-top: auto;
  backdrop-filter: blur(12px);
}

.app-footer .footer-content {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.app-footer .footer-content p {
  margin: 0;
}

/* Mobile sidebar - fixed overlay */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    width: 200px;
    transform: translateX(-100%);
    z-index: 1000;
  }

  .sidebar.sidebar-open {
    transform: translateX(0);
  }

  .page-content {
    flex: 1 1 auto;
    min-width: 0;
  }
}

/* Improve touch targets */
@media (max-width: 768px) {
  .sidebar .nav a { padding: 12px 14px; font-size: 16px; }
  .theme-toggle { top: 72px; right: 16px; }
}

/* Ensure tables scroll smoothly on small screens */
.table-responsive { -webkit-overflow-scrolling: touch; }

/* Reduce padding for containers on small screens */
@media (max-width: 576px) {
  .container-fluid { padding-left: 10px; padding-right: 10px; }
  .card-body { padding: 16px; }
  .card-header { padding: 10px 14px; }
}
