*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { overflow-x: clip; overscroll-behavior-x: none; }

:root {
  --purple: #6d28d9;
  --purple-dark: #5b21b6;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --green: #059669;
  --amber: #d97706;
  --blue: #2563eb;
  --red: #dc2626;
  --nav-h: 84px;
}

[data-theme="dark"] {
  --bg: #18181b;
  --surface: #27272a;
  --border: #3f3f46;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --green: #10b981;
  --amber: #f59e0b;
  --blue: #60a5fa;
  --red: #f87171;
}

[data-theme="dark"] .table a { color: #a78bfa; }
[data-theme="dark"] .table a:hover { color: #c4b5fd; }
[data-theme="dark"] .message-content a { color: #a78bfa; }
[data-theme="dark"] .attachment-file { color: #a78bfa; }

[data-theme="dark"] .status-received   { background: #27272a; color: #a1a1aa; }
[data-theme="dark"] .status-invoiced   { background: #1e1b4b; color: #a5b4fc; }
[data-theme="dark"] .status-configured { background: #1e40af; color: #bfdbfe; }
[data-theme="dark"] .status-licensed   { background: #2e1065; color: #c4b5fd; }
[data-theme="dark"] .status-shipped    { background: #134e4a; color: #5eead4; }
[data-theme="dark"] .status-delivered  { background: #064e3b; color: #6ee7b7; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
}

/* Nav */
.nav {
  display: flex;
  flex-direction: column;
  background: var(--purple-dark);
  color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1050;
  padding-top: env(safe-area-inset-top);
}
.nav-top {
  display: flex;
  align-items: center;
  padding: 0 16px;
  height: 44px;
  position: relative;
}
.nav-brand {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 16px;
  color: #fff;
  text-decoration: none;
  letter-spacing: 0.02em;
}
.nav-logo { height: 22px; width: auto; }
.nav-menu-btn {
  background: none;
  border: none;
  color: rgba(255,255,255,0.85);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}
.nav-menu-btn:hover { color: #fff; }
.nav-links {
  display: flex;
  justify-content: center;
  gap: 32px;
  padding: 8px 16px 10px;
  border-top: 1px solid rgba(255,255,255,0.1);
}
.nav-links a {
  color: rgba(255,255,255,0.9);
  text-decoration: none;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.03em;
  padding: 3px 10px;
  border-radius: 6px;
}
.nav-links a:hover { color: #fff; }
.nav-links a.active {
  color: #fff;
  background: rgba(255,255,255,0.18);
}

/* Drawer */
.drawer {
  position: fixed;
  top: 0; left: 0;
  width: 220px;
  height: 100vh;
  background: var(--surface);
  z-index: 1000;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  box-shadow: 4px 0 20px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
  padding: 24px 20px;
}
.drawer.open { transform: translateX(0); }
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 999;
  display: none;
}
.drawer-overlay.open { display: block; }
.drawer-top {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}
.drawer-logo { height: 28px; width: auto; }
.drawer-brand { font-weight: 700; font-size: 16px; color: var(--text); }
.drawer-logout {
  width: 100%;
  padding: 9px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.drawer-logout:hover { background: #fee2e2; color: var(--red); border-color: #fca5a5; }
.drawer-link { display: block; font-size: 14px; color: var(--text); text-decoration: none; padding: 7px 10px; border-radius: 6px; margin-bottom: 2px; }
.drawer-link:hover { background: var(--bg); }
.drawer-link.active { background: var(--bg); font-weight: 600; color: var(--purple); }
.drawer-admin-link { display: block; font-size: 13px; color: var(--red); text-decoration: none; padding: 6px 10px; border-radius: 6px; margin-top: 8px; border: 1px solid color-mix(in srgb, var(--red) 30%, transparent); }
.drawer-admin-link:hover { background: color-mix(in srgb, var(--red) 8%, transparent); }
.drawer-admin-link.active { font-weight: 600; }

/* Pinned-drawer breakpoint: 1550px chosen so that the orders list
 * (max-width 1250px + container padding) still fits beside the 220px drawer.
 * Below this, the drawer collapses to a hamburger-toggle to free up width. */
@media (min-width: 1550px) {
  .drawer {
    transform: translateX(0);
    box-shadow: none;
    border-right: 1px solid var(--border);
    transition: none;
    top: calc(var(--nav-h) + env(safe-area-inset-top));
    height: calc(100vh - var(--nav-h) - env(safe-area-inset-top));
    padding-top: 20px;
    overflow-y: auto;
  }
  .drawer-overlay { display: none !important; }
  .nav-menu-btn { display: none; }
  body { padding-left: 220px; }
}

/* Buttons */
.btn-primary, .btn-secondary, .btn-logout {
  display: inline-flex;
  align-items: center;
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
  transition: opacity 0.15s;
}
.btn-primary  { background: var(--purple); color: #fff; }
.btn-primary:hover { background: var(--purple-dark); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-logout { background: transparent; color: rgba(255,255,255,0.75); border-color: rgba(255,255,255,0.3); padding: 4px 10px; font-size: 13px; }
.btn-logout:hover { color: #fff; border-color: rgba(255,255,255,0.6); }
.btn-delete { background: transparent; color: var(--muted); border: none; font-size: 12px; cursor: pointer; padding: 0; }
.btn-delete:hover { color: var(--red); }

/* Container */
.container { max-width: 900px; margin: 32px auto; padding: 0 24px; }

/* Page header */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 28px; gap: 16px; }
.page-header h1 { font-size: 22px; font-weight: 700; }

/* Section */
.section { margin-bottom: 32px; }
.section h2 { font-size: 15px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 12px; }

/* Dashboard cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-top: 8px; }
.card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  text-decoration: none;
  color: var(--text);
  transition: box-shadow 0.15s, border-color 0.15s;
}
.card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.08); border-color: var(--purple); }
.card-icon { font-size: 28px; line-height: 1; }
.card-body h2 { font-size: 16px; font-weight: 600; margin-bottom: 4px; }
.card-body p { font-size: 13px; color: var(--muted); }

/* Table */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: 8px; overflow: hidden; border: 1px solid var(--border); }
.table th { text-align: left; padding: 10px 14px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; border-bottom: 1px solid var(--border); }
.table td { padding: 11px 14px; border-bottom: 1px solid var(--border); font-size: 14px; }
.table tr:last-child td { border-bottom: none; }
@media (hover: hover) { .table tr:hover td { background: var(--bg); } }
.fleet-row, .order-row { -webkit-tap-highlight-color: transparent; }
.table a { color: var(--purple); text-decoration: none; }
.table a:hover { text-decoration: underline; }

/* Badges */
.badge { display: inline-flex; align-items: center; padding: 3px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.badge-lg { padding: 5px 12px; font-size: 13px; }
.status-received   { background: #f1f5f9; color: #475569; }
.status-invoiced   { background: #e0e7ff; color: #4338ca; }
.status-configured { background: #dbeafe; color: var(--blue); }
.status-licensed   { background: #f3e8ff; color: #7c3aed; }
.status-shipped    { background: #ccfbf1; color: #0f766e; }
.status-delivered  { background: #d1fae5; color: #065f46; }

/* Table scroll wrapper */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Forms */
.form { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 24px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 600px) { .form-row { grid-template-columns: 1fr; } }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field label { font-size: 13px; font-weight: 600; color: var(--text); }
.field-hint { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field input, .field textarea, textarea {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 16px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  width: 100%;
  transition: border-color 0.15s;
}
.field input:focus, .field textarea:focus, textarea:focus {
  outline: none;
  border-color: var(--purple);
  background: var(--surface);
}
.form-actions { display: flex; gap: 10px; margin-top: 8px; }

/* Detail dl */
.dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 14px; }
.dl dt { color: var(--muted); font-weight: 500; }

/* Replace file disclosure */
.replace-form { margin-top: 10px; display: inline-block; }
.replace-form summary { font-size: 12px; color: var(--muted); cursor: pointer; list-style: none; }
.replace-form summary::-webkit-details-marker { display: none; }
.replace-form summary:hover { color: var(--text); }

/* Ship form details */
.ship-form { margin-top: 12px; }
.ship-form summary { display: inline-flex; align-items: center; padding: 7px 14px; background: var(--purple); color: #fff; border-radius: 6px; font-size: 14px; font-weight: 500; cursor: pointer; list-style: none; }
.ship-form summary::-webkit-details-marker { display: none; }
.ship-inner { margin-top: 16px; padding: 20px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }

/* Login */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 36px 40px; width: 360px; }
.login-logo { display: block; height: 56px; width: auto; margin: 0 auto 16px; }
.login-card h1 { font-size: 20px; font-weight: 700; margin-bottom: 28px; text-align: center; }
.login-card .field { margin-bottom: 16px; }
.login-card .btn-primary { width: 100%; justify-content: center; padding: 10px; font-size: 15px; margin-top: 8px; }

/* Fleet */
.fleet-row { cursor: pointer; }

/* Device detail overlay */
.device-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 500;
  background: rgba(0,0,0,0.5);
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.device-overlay.open { display: flex; }
.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
}
.device-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.device-card-close {
  background: none;
  border: none;
  font-size: 22px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0 2px;
}
.device-card-close:hover { color: var(--text); }

/* Learning / Messages */
.message-board { max-width: 720px; margin: 0 auto 24px; display: flex; flex-direction: column; gap: 16px; }
.message { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; position: relative; }
.message-actions { position: absolute; top: 10px; right: 12px; display: flex; gap: 4px; }
.msg-action-btn { background: none; border: none; cursor: pointer; padding: 2px 4px; opacity: 0.4; line-height: 1; display: flex; align-items: center; font-size: 13px; }
.msg-action-btn svg { width: 14px; height: 14px; }
.thread-delete-btn { padding: 6px 10px; color: var(--muted); }
.thread-delete-btn:hover { color: var(--red); border-color: #fca5a5; background: #fee2e2; }
.msg-action-btn:hover { opacity: 1; }
.message-meta { display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px; }
.message-author { font-weight: 600; font-size: 14px; }
.message-meta time { font-size: 11px; }
.message-content { font-size: 14px; line-height: 1.6; }
.message-content p { margin-bottom: 8px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content a { color: var(--purple); }
.message-content strong { font-weight: 600; }
.message-content code { font-size: 12px; }
.message-content ul, .message-content ol { padding-left: 1.5em; margin-bottom: 8px; }
.message-content li { margin-bottom: 2px; }
.message-content table { border-collapse: collapse; margin-bottom: 8px; font-size: 13px; }
.message-content th, .message-content td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
.message-content th { background: var(--bg); font-weight: 600; }
.attachment { margin-top: 10px; }
.message-content + .attachment { margin-top: 18px; }
.message-content blockquote { border-left: 3px solid var(--border); padding: 2px 10px; margin: 4px 0 8px; color: var(--muted); font-size: 13px; background: var(--bg); border-radius: 0 4px 4px 0; }
.message-content blockquote p { margin-bottom: 4px; }
.message-content blockquote strong { color: var(--text); }
.message-content hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.message-footer { margin-top: 10px; display: flex; gap: 6px; justify-content: flex-end; }
.like-btn { background: none; border: 1px solid var(--border); cursor: pointer; display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: 12px; font-size: 12px; color: var(--muted); line-height: 1; }
.like-btn:hover { background: var(--border); color: var(--text); }
.like-btn.liked { color: var(--purple); border-color: var(--purple); background: rgba(124, 58, 237, 0.08); }
.like-count { font-weight: 500; }
.attachment-image { max-width: 100%; max-height: 400px; border-radius: 6px; display: block; }
.attachment-thumb { width: 120px; height: 120px; object-fit: cover; border-radius: 6px; display: block; cursor: zoom-in; }
.attachment:has(.attachment-thumb) { display: inline-block; margin-top: 28px; margin-right: 6px; vertical-align: top; }

/* Lightbox */
.lightbox {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1100;
  background: rgba(0,0,0,0.85);
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}
.lightbox.open { display: flex; }
body:has(.lightbox.open) { overflow: hidden; }
.lightbox img {
  max-width: 92vw;
  max-height: 92vh;
  border-radius: 8px;
  object-fit: contain;
  cursor: default;
}
.lightbox-nav {
  display: none;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  border: none;
  cursor: pointer;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  font-size: 32px;
  line-height: 1;
  align-items: center;
  justify-content: center;
  padding: 0 0 4px 0;
}
.lightbox-nav:hover { background: rgba(0, 0, 0, 0.75); }
.lightbox-nav:disabled { opacity: 0.25; cursor: default; }
.lightbox-nav:disabled:hover { background: rgba(0, 0, 0, 0.5); }
.lightbox-prev { left: 20px; }
.lightbox-next { right: 20px; }
.attachment-audio { display: block; width: 100%; margin-bottom: 4px; }
.attachment-file { font-size: 13px; color: var(--purple); text-decoration: none; }
.attachment-file:hover { text-decoration: underline; }

.compose { max-width: 720px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; position: sticky; bottom: 16px; }
.compose-row { display: flex; align-items: flex-end; gap: 4px; }
.compose-row textarea { flex: 1; resize: none; border-color: transparent; background: var(--bg); overflow-y: hidden; min-height: 36px; line-height: 1.5; padding: 6px 8px; }
.compose-row textarea:focus { border-color: var(--purple); }
.compose-icon-btn { padding: 4px 6px; flex-shrink: 0; background: none; border: none; cursor: pointer; color: var(--muted); display: flex; align-items: center; }
.compose-icon-btn:hover { color: var(--purple); }
.compose-icon-btn.send { background: var(--purple); color: #fff; border-radius: 6px; padding: 5px 8px; }
.compose-icon-btn.send:hover { background: var(--purple-dark); color: #fff; }
.compose-icon-btn.send:disabled { opacity: 0.7; cursor: default; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Hide number input spinners */
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }

/* Utilities */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); background: #fee2e2; border-radius: 6px; padding: 10px 14px; margin-bottom: 16px; font-size: 14px; }
code { font-family: ui-monospace, "SF Mono", monospace; font-size: 13px; background: var(--bg); padding: 1px 5px; border-radius: 4px; }
h1 { font-size: 22px; font-weight: 700; margin-bottom: 24px; }

/* Footnotes */
.footnotes hr { margin-bottom: 10px; }

/* Settings sections */
.settings-section { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 24px; margin-bottom: 24px; }
.settings-section h3 { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 16px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }

/* Row action kebab menus (table-row dropdowns) */
.row-menu-wrap { display: inline-block; }
.row-menu-btn { background: none; border: 1px solid transparent; border-radius: 4px; cursor: pointer; color: var(--muted); padding: 1px 7px; font-size: 18px; line-height: 1; }
.row-menu-btn:hover { background: var(--border); color: var(--text); border-color: var(--border); }
.row-menu-btn[aria-expanded="true"] { background: var(--border); color: var(--text); border-color: var(--border); }
/* Positioned via JS (position: fixed) so it escapes .table's overflow:hidden. */
.row-menu { position: fixed; min-width: 170px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 4px 12px rgba(0,0,0,0.15); z-index: 1000; padding: 4px 0; text-align: left; }
.row-menu-item { width: 100%; background: none; border: none; cursor: pointer; color: var(--text); padding: 7px 12px; font-size: 13px; text-align: left; display: block; }
.row-menu-item:hover { background: var(--border); }
.row-menu-danger { color: #ef4444; }
.row-menu-danger:hover { background: rgba(239, 68, 68, 0.08); }
