/* ── app shell ── */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--header-h) 1fr;
  grid-template-areas:
    "sidebar header"
    "sidebar main";
  min-height: 100vh;
}

/* ── sidebar ── */
.sidebar {
  grid-area: sidebar;
  background: linear-gradient(180deg, var(--bg-1) 0%, #080b14 100%);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 100;
  box-shadow: -2px 0 20px rgba(0,0,0,.35);
}

.sidebar-brand {
  height: var(--header-h);
  padding: 0 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: .75rem;
  background: linear-gradient(135deg, rgba(0,180,216,.05) 0%, transparent 60%);
  flex-shrink: 0;
}

/* ── sidebar user section ── */
.sidebar-user {
  padding: .85rem 1.25rem .9rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: .7rem;
  background: rgba(0,0,0,.1);
}
.sidebar-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--teal) 0%, var(--gold-dim) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .9rem;
  font-weight: 800;
  color: #000;
  box-shadow: 0 0 12px rgba(0,180,216,.3);
}
.sidebar-user-name { font-size: .86rem; font-weight: 600; color: var(--text-1); line-height: 1.3; }
.sidebar-user-role { font-size: .68rem; color: var(--teal); opacity: .75; }

.brand-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, rgba(0,180,216,.12) 0%, rgba(240,165,0,.08) 100%);
  border: 1px solid rgba(0,180,216,.28);
  border-radius: var(--r-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 0 14px rgba(0,180,216,.18), inset 0 1px 0 rgba(255,255,255,.06);
}
.brand-icon svg { width: 20px; height: 20px; color: var(--teal); }

.brand-text {
  display: flex;
  flex-direction: column;
}
.brand-title { font-size: .95rem; font-weight: 700; color: var(--text-1); }
.brand-sub   { font-size: .7rem; color: var(--text-3); letter-spacing: .05em; }

/* nav */
.sidebar-nav {
  flex: 1;
  padding: .75rem .75rem;
}

.nav-section-label {
  font-size: .68rem;
  font-weight: 600;
  color: var(--text-3);
  letter-spacing: .1em;
  text-transform: uppercase;
  padding: 1rem .5rem .4rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .65rem .85rem;
  border-radius: var(--r-sm);
  color: var(--text-2);
  font-size: .88rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--t-fast), color var(--t-fast);
  text-decoration: none;
  margin-bottom: 2px;
  position: relative;
  overflow: hidden;
}

.nav-item svg { width: 17px; height: 17px; flex-shrink: 0; opacity: .75; transition: opacity var(--t-fast); }

.nav-item:hover {
  background: var(--bg-3);
  color: var(--text-1);
}
.nav-item:hover svg { opacity: 1; }

.nav-item.active {
  background: rgba(0,180,216,.1);
  color: var(--teal);
  border: 1px solid rgba(0,180,216,.2);
  box-shadow: inset 0 0 0 1px rgba(0,180,216,.1), 0 2px 10px rgba(0,180,216,.07);
}
.nav-item.active svg { color: var(--teal); opacity: 1; filter: drop-shadow(0 0 4px rgba(0,180,216,.5)); }
.nav-item.active::before {
  content: '';
  position: absolute;
  right: 0; top: 20%; bottom: 20%;
  width: 3px;
  background: linear-gradient(180deg, var(--teal) 0%, rgba(0,180,216,.4) 100%);
  border-radius: 99px;
  box-shadow: 0 0 8px var(--teal);
}

/* nav badge */
.nav-badge {
  margin-inline-start: auto;
  background: linear-gradient(135deg, #f43f5e 0%, #dc2626 100%);
  color: #fff;
  font-size: .64rem;
  font-weight: 800;
  padding: .15rem .45rem;
  border-radius: 99px;
  min-width: 20px;
  min-height: 18px;
  text-align: center;
  line-height: 1.5;
  letter-spacing: .01em;
  box-shadow: 0 0 10px rgba(244,63,94,.5), 0 2px 6px rgba(0,0,0,.35);
  border: 1px solid rgba(255,255,255,.12);
}

/* nav submenu */
.nav-sub-chevron {
  margin-inline-start: auto;
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  opacity: .45;
  transition: transform .22s ease, opacity var(--t-fast);
}
#machines-toggle.open .nav-sub-chevron { transform: rotate(180deg); opacity: .75; }
#machines-toggle { cursor: pointer; user-select: none; }

.nav-sub {
  position: relative;
  margin-right: 1.5rem;
  margin-bottom: 2px;
  padding-right: .5rem;
  max-height: 0;
  overflow: hidden;
  opacity: 0;
  transition: max-height .26s cubic-bezier(.4,0,.2,1), opacity .2s ease;
}
.nav-sub.open {
  max-height: 240px;
  opacity: 1;
}
.nav-sub::before {
  content: '';
  position: absolute;
  right: 0; top: 4px; bottom: 4px;
  width: 1.5px;
  background: linear-gradient(180deg, rgba(0,180,216,.4) 0%, rgba(0,180,216,.08) 100%);
  border-radius: 99px;
}
.nav-sub-item {
  display: flex;
  align-items: center;
  gap: .55rem;
  padding: .42rem .75rem;
  border-radius: var(--r-sm);
  color: var(--text-3);
  font-size: .82rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--t-fast), color var(--t-fast);
  text-decoration: none;
  margin-bottom: 1px;
}
.nav-sub-item svg { width: 14px; height: 14px; flex-shrink: 0; opacity: .65; transition: opacity var(--t-fast); }
.nav-sub-item:hover { background: var(--bg-3); color: var(--text-2); }
.nav-sub-item:hover svg { opacity: 1; }
.nav-sub-item.active { color: var(--teal); background: rgba(0,180,216,.07); }
.nav-sub-item.active svg { opacity: 1; color: var(--teal); }

[data-theme="light"] .nav-sub::before {
  background: linear-gradient(180deg, rgba(82,183,136,.5) 0%, rgba(82,183,136,.1) 100%);
}
[data-theme="light"] .nav-sub-item { color: rgba(255,255,255,.5); }
[data-theme="light"] .nav-sub-item:hover { background: rgba(255,255,255,.08); color: rgba(255,255,255,.85); }
[data-theme="light"] .nav-sub-item.active { color: #fff; background: rgba(255,255,255,.12); }

/* sidebar panel links */
.sidebar-panels {
  padding: .5rem .75rem;
  border-top: 1px solid var(--border);
}
.sidebar-section-label {
  font-size: .68rem;
  font-weight: 600;
  color: var(--text-3);
  padding: .5rem .5rem .25rem;
  letter-spacing: .04em;
  text-transform: uppercase;
}
.panel-link {
  font-size: .8rem;
  opacity: .8;
}
.panel-link:hover { opacity: 1; }

/* sidebar footer */
.sidebar-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
  font-size: .78rem;
  color: var(--text-3);
  display: flex;
  align-items: center;
  gap: .5rem;
}
.sidebar-footer svg { width: 14px; height: 14px; }

/* ── header ── */
.header {
  grid-area: header;
  background: rgba(10,13,20,.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: none;
  box-shadow: 0 1px 0 var(--border), 0 4px 20px rgba(0,0,0,.2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  position: sticky;
  top: 0;
  z-index: 90;
}
.header::after {
  content: '';
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, var(--teal-dim) 35%, var(--gold-dim) 65%, transparent 100%);
  opacity: .3;
}

.header-title {
  display: flex;
  align-items: center;
  gap: .75rem;
}

.header-title h1 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-1);
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: .4rem;
  font-size: .8rem;
  color: var(--text-3);
}
.breadcrumb a { color: var(--text-3); }
.breadcrumb a:hover { color: var(--text-2); }
.breadcrumb .sep { color: var(--border-light); }
.breadcrumb .current { color: var(--text-2); }

.header-actions { display: flex; align-items: center; gap: .75rem; margin-inline-start: auto; }
/* RTL: higher order = further LEFT. theme + owner always leftmost, page controls stay right of them */
.header-actions .theme-btn  { order: 10; }
.header-actions .header-user { order: 9; }

/* ── header user (avatar + name injected by nav.js) ── */
.header-user {
  display: flex;
  align-items: center;
  gap: .5rem;
}
.header-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--teal) 0%, var(--teal-dim) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .82rem;
  font-weight: 800;
  color: #000;
  box-shadow: 0 0 10px rgba(0,180,216,.3);
  flex-shrink: 0;
  cursor: default;
  user-select: none;
}
.header-user-name {
  font-size: .82rem;
  font-weight: 600;
  color: var(--text-2);
  white-space: nowrap;
}
[data-theme="light"] .header-user-name { color: rgba(255,255,255,.9); }
[data-theme="light"] .header-avatar {
  background: linear-gradient(135deg, rgba(82,183,136,.95) 0%, rgba(45,106,79,.95) 100%);
  color: #fff;
  box-shadow: 0 0 10px rgba(82,183,136,.4);
}

@media (max-width: 600px) {
  .header-user-name { display: none; }
  .header-title { display: none; }
}

/* ── main content ── */
.main-content {
  grid-area: main;
  padding: 1.75rem;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  isolation: isolate;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 1.75rem;
  gap: 1rem;
}

.page-header-left { flex: 1; }
.page-title { font-size: 1.4rem; font-weight: 700; color: var(--text-1); margin-bottom: .25rem; }
.page-subtitle { font-size: .85rem; color: var(--text-3); }

/* ── stat cards grid ── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.75rem;
}

/* ── content grid ── */
.content-grid {
  display: grid;
  gap: 1.5rem;
}

.content-grid.cols-2 { grid-template-columns: 1fr 1fr; }
.content-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }

@media (max-width: 900px) {
  .content-grid.cols-2,
  .content-grid.cols-3 { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
}

/* ── light mode overrides — forest green theme ── */
[data-theme="light"] .sidebar {
  background: linear-gradient(180deg, rgba(27,67,50,.88) 0%, rgba(18,43,33,.92) 100%);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-left: 1px solid rgba(82,183,136,.15);
  box-shadow: -2px 0 32px rgba(0,0,0,.2), inset -1px 0 0 rgba(255,255,255,.04);
}
[data-theme="light"] .sidebar-brand {
  border-bottom-color: rgba(255,255,255,.1);
  background: rgba(255,255,255,.04);
}
[data-theme="light"] .sidebar-user {
  border-bottom-color: rgba(255,255,255,.1);
  background: rgba(0,0,0,.15);
}
[data-theme="light"] .sidebar-user-name { color: #fff; }
[data-theme="light"] .sidebar-user-role { color: rgba(255,255,255,.55); opacity: 1; }
[data-theme="light"] .brand-icon {
  background: linear-gradient(135deg, rgba(82,183,136,.18) 0%, rgba(45,106,79,.1) 100%);
  border-color: rgba(82,183,136,.35);
  box-shadow: 0 0 14px rgba(82,183,136,.2), inset 0 1px 0 rgba(255,255,255,.08);
}
[data-theme="light"] .brand-icon svg { color: #52B788; }
[data-theme="light"] .brand-title { color: #fff; }
[data-theme="light"] .brand-sub   { color: rgba(255,255,255,.45); }
[data-theme="light"] .nav-section-label { color: rgba(255,255,255,.3); }
[data-theme="light"] .nav-item { color: rgba(255,255,255,.7); }
[data-theme="light"] .nav-item:hover {
  background: rgba(255,255,255,.1);
  color: #fff;
}
[data-theme="light"] .nav-item:hover svg { opacity: 1; filter: none; }
[data-theme="light"] .nav-item.active {
  background: rgba(255,255,255,.15);
  color: #fff;
  border-color: rgba(255,255,255,.2);
  box-shadow: none;
}
[data-theme="light"] .nav-item.active svg { opacity: 1; filter: none; color: #fff; }
[data-theme="light"] .nav-item.active::before {
  background: linear-gradient(180deg, #52B788 0%, rgba(82,183,136,.4) 100%);
  box-shadow: 0 0 8px rgba(82,183,136,.4);
}
[data-theme="light"] .nav-badge { background: #ef4444; }
[data-theme="light"] .sidebar-footer {
  border-top-color: rgba(255,255,255,.1);
  color: rgba(255,255,255,.35);
}

[data-theme="light"] .header {
  background: rgba(27,67,50,.42);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 1px 0 rgba(82,183,136,.18), 0 4px 24px rgba(0,0,0,.2);
  border-bottom: 1px solid rgba(82,183,136,.22);
}
[data-theme="light"] .header::after {
  background: linear-gradient(90deg, transparent 0%, rgba(82,183,136,.5) 35%, rgba(152,210,178,.5) 65%, transparent 100%);
  opacity: .5;
}

/* page-level text on dark bg → white */
[data-theme="light"] .page-title    { color: #fff; }
[data-theme="light"] .page-subtitle { color: rgba(255,255,255,.65); }
[data-theme="light"] .header-title h1 { color: #fff; }
[data-theme="light"] .breadcrumb      { color: rgba(255,255,255,.5); }
[data-theme="light"] .breadcrumb a    { color: rgba(255,255,255,.5); }
[data-theme="light"] .breadcrumb a:hover  { color: rgba(255,255,255,.85); }
[data-theme="light"] .breadcrumb .current { color: rgba(255,255,255,.75); }
[data-theme="light"] .breadcrumb .sep     { color: rgba(255,255,255,.25); }
[data-theme="light"] .header-actions .btn-ghost {
  color: rgba(255,255,255,.75);
  border-color: transparent;
}
[data-theme="light"] .header-actions .btn-ghost:hover {
  background: rgba(255,255,255,.1);
  color: #fff;
}

/* ── hamburger (hidden on desktop) ── */
.menu-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: .4rem;
  border-radius: var(--r-sm);
  color: var(--text-2);
  align-items: center;
  justify-content: center;
  transition: background var(--t-fast), color var(--t-fast);
  flex-shrink: 0;
}
.menu-toggle:hover { background: var(--bg-3); color: var(--text-1); }
.menu-toggle svg { width: 20px; height: 20px; display: block; }
[data-theme="light"] .menu-toggle { color: rgba(255,255,255,.75); }
[data-theme="light"] .menu-toggle:hover { background: rgba(255,255,255,.1); color: #fff; }

/* ── sidebar overlay (mobile) ── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 199;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

/* ── tablet ── */
@media (max-width: 1024px) {
  :root { --sidebar-w: 240px; }
}

@media (max-width: 900px) {
  :root { --sidebar-w: 220px; }
}

/* ── sidebar close button (mobile only) ── */
.sidebar-close-btn {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: .4rem;
  border-radius: var(--r-sm);
  color: var(--text-3);
  align-items: center;
  justify-content: center;
  transition: background var(--t-fast), color var(--t-fast);
  margin-inline-start: auto;
  flex-shrink: 0;
}
.sidebar-close-btn svg { width: 18px; height: 18px; display: block; }
.sidebar-close-btn:hover { background: rgba(255,255,255,.1); color: var(--text-1); }
[data-theme="light"] .sidebar-close-btn { color: rgba(255,255,255,.55); }
[data-theme="light"] .sidebar-close-btn:hover { color: #fff; background: rgba(255,255,255,.12); }

/* ── mobile ── */
@media (max-width: 768px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: var(--header-h) 1fr;
    grid-template-areas: "header" "main";
  }

  .sidebar {
    position: fixed;
    right: 0;
    top: 0;
    bottom: 0;
    width: var(--sidebar-w);
    height: 100dvh;
    transform: translateX(110%);
    transition: transform .28s cubic-bezier(.4,0,.2,1);
    z-index: 200;
  }

  body.sidebar-open .sidebar { transform: translateX(0); }
  body.sidebar-open .sidebar-overlay { display: block; }

  .menu-toggle { display: flex; }
  .sidebar-close-btn { display: flex; }

  .main-content { padding: 1.25rem 1rem; }
  .header { padding: 0 1rem; }
  .header-actions { gap: .4rem; }

  .page-header { margin-bottom: 1.25rem; flex-wrap: wrap; gap: .65rem; }
  .page-title { font-size: 1.2rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .content-grid.cols-2,
  .content-grid.cols-3 { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .main-content { padding: 1rem .75rem; }
  .page-title { font-size: 1.1rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: .65rem; }
}
