:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --text: #1d2730;
  --muted: #637281;
  --line: #d8e0e7;
  --primary: #126e82;
  --primary-dark: #0b5363;
  --accent: #b56d2a;
  --success: #23784d;
  --danger: #a33a3a;
  --warning: #a57310;
  --shadow: 0 12px 28px rgba(29, 39, 48, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
  font-size: 15px;
  overflow-x: hidden;
}

button,
input,
select,
textarea {
  font: inherit;
  min-width: 0;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  min-height: 40px;
  padding: 0 14px;
  white-space: nowrap;
}

button,
.button-link {
  text-decoration: none;
}

button:hover {
  background: var(--primary-dark);
}

button.secondary {
  background: #e4edf1;
  color: var(--primary-dark);
}

button.secondary:hover {
  background: #d2e3ea;
}

button.ghost {
  background: transparent;
  color: var(--primary-dark);
  border: 1px solid var(--line);
}

button.ghost:hover {
  background: var(--surface-2);
}

button.danger {
  background: #f7e1e1;
  color: var(--danger);
  border: 1px solid #d89c9c;
}

button.danger:hover {
  background: #efd0d0;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 14px;
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  white-space: nowrap;
}

.button-link:hover {
  background: var(--primary-dark);
}

.hidden {
  display: none !important;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 20px clamp(16px, 4vw, 44px);
  background: #18252d;
  color: #fff;
}

.app-header h1 {
  margin: 0;
  font-size: 24px;
  line-height: 1.15;
}

.app-header p {
  margin: 5px 0 0;
  color: #c5d2dc;
}

.session-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  white-space: nowrap;
}

main {
  width: min(1240px, calc(100% - 32px));
  margin: 22px auto 48px;
  min-width: 0;
}

.login-grid,
.work-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
  min-width: 0;
}

.panel h2,
.toolbar h2 {
  margin: 0 0 14px;
  font-size: 18px;
}

form {
  display: grid;
  gap: 13px;
}

label {
  position: relative;
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  min-height: 40px;
  padding: 8px 10px;
}

textarea {
  resize: vertical;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.tab {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
}

.tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.tab-panel {
  display: grid;
  gap: 18px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
  min-width: 0;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  min-width: 0;
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--surface);
  -webkit-overflow-scrolling: touch;
}

.segmented button {
  flex: 0 0 auto;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  min-height: 36px;
  border-right: 1px solid var(--line);
}

.segmented button:last-child {
  border-right: 0;
}

.segmented button.active {
  background: var(--primary);
  color: #fff;
}

.compact-segmented {
  width: fit-content;
}

.nested-fields {
  display: grid;
  gap: 13px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafb;
}

.search-results {
  position: absolute;
  z-index: 20;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  max-height: 260px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 14px 30px rgba(29, 39, 48, 0.16);
  padding: 6px;
}

.filter-search {
  position: relative;
}

.filter-search .search-results {
  top: calc(100% + 6px);
}

.search-result {
  display: grid;
  width: 100%;
  min-height: 0;
  padding: 9px 10px;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  text-align: left;
}

.search-result:hover {
  background: #eef6f8;
  color: var(--text);
}

.search-result strong {
  font-size: 14px;
}

.search-result span,
.empty-result {
  color: var(--muted);
  font-size: 12px;
}

.empty-result {
  padding: 10px;
}

.selected-product {
  padding: 8px 10px;
  border-radius: 6px;
  background: #ecf8f0;
  color: var(--success);
  font-size: 13px;
  font-weight: 700;
}

.resolved-product {
  display: grid;
  gap: 5px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafb;
}

.resolved-product span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.resolved-product strong {
  color: var(--text);
  font-size: 15px;
}

.resolved-product.ok {
  border-color: #9acbab;
  background: #ecf8f0;
}

.resolved-product.danger {
  border-color: #d89c9c;
  background: #fff1f1;
}

.camera-panel {
  display: grid;
  gap: 12px;
  align-content: start;
}

video {
  width: 100%;
  min-height: 280px;
  border-radius: 8px;
  background: #111b21;
  object-fit: cover;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  border-radius: 999px;
  padding: 4px 10px;
  background: #eef2f5;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.pill.ok {
  background: #e2f2e8;
  color: var(--success);
}

.pill.warn {
  background: #fff3d7;
  color: var(--warning);
}

.pill.danger {
  background: #f7e1e1;
  color: var(--danger);
}

.inline-search {
  display: grid;
  grid-template-columns: minmax(180px, 280px) auto;
  gap: 8px;
}

.table-wrap {
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}

.table-wrap.compact {
  max-height: 420px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 11px 12px;
  text-align: left;
  vertical-align: middle;
}

th {
  background: #eef2f5;
  color: #465662;
  font-size: 12px;
  text-transform: uppercase;
}

tbody tr:hover {
  background: #f8fafb;
}

td.numeric {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.row-action {
  color: var(--primary-dark);
  background: transparent;
  border: 1px solid var(--line);
  min-height: 32px;
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

.row-actions button {
  min-height: 32px;
  padding: 0 10px;
}

.details-panel {
  box-shadow: none;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.dashboard-filter {
  display: grid;
  gap: 12px;
  box-shadow: none;
}

.filter-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.filter-head h3 {
  margin: 0 0 4px;
  font-size: 16px;
}

.active-filter {
  background: var(--primary) !important;
  color: #fff !important;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  min-height: 32px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  padding: 0 10px;
  background: #eef2f5;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
}

.chip:hover {
  background: #e2e9ef;
}

.chip strong {
  font-size: 16px;
  line-height: 1;
}

.color-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
}

.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.metric span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: 26px;
}

.chart-area {
  display: grid;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: center;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  max-width: 100%;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.chart-legend-item span:last-child {
  max-width: min(220px, 72vw);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chart-scroll {
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 4px;
  -webkit-overflow-scrolling: touch;
}

#usageChart {
  width: auto;
  height: auto;
  max-width: none;
  display: block;
  shape-rendering: geometricPrecision;
  text-rendering: geometricPrecision;
}

.chart-axis {
  stroke: var(--line);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
  fill: none;
}

.chart-label {
  fill: var(--muted);
  font-size: 12px;
}

.chart-title {
  fill: var(--text);
  font-size: 13px;
  font-weight: 700;
}

.chart-value {
  font-size: 11px;
  font-weight: 700;
}

.admin-qty-input {
  min-width: 90px;
}

.admin-reason-input {
  min-width: 180px;
}

.admin-search {
  min-width: min(320px, 100%);
}

.pager {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  background: #18252d;
  color: #fff;
  border-radius: 8px;
  padding: 12px 14px;
  box-shadow: 0 14px 35px rgba(0, 0, 0, 0.22);
  z-index: 30;
}

.toast.error {
  background: #8e3030;
}

.toast.ok {
  background: #1e6843;
}

@media (max-width: 820px) {
  .app-header,
  .toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .session-bar {
    width: 100%;
    justify-content: space-between;
  }

  .login-grid,
  .work-grid,
  .metrics {
    grid-template-columns: 1fr;
  }

  .inline-search {
    grid-template-columns: 1fr;
    width: 100%;
  }

  .toolbar-actions {
    width: 100%;
    justify-content: stretch;
  }

  .toolbar-actions > * {
    width: 100%;
  }

  .toolbar-actions .segmented {
    width: 100%;
  }

  .toolbar-actions .segmented button {
    flex: 1 0 auto;
  }

  .filter-head {
    align-items: stretch;
    flex-direction: column;
  }

  .compact-segmented {
    width: 100%;
  }

  video {
    min-height: 220px;
  }
}

@media (max-width: 560px) {
  main {
    width: min(100% - 20px, 1240px);
    margin-top: 14px;
  }

  .app-header {
    gap: 12px;
    padding: 16px 12px;
  }

  .app-header h1 {
    font-size: 21px;
  }

  .panel,
  .chart-area {
    padding: 12px;
  }

  .tabs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .tab,
  .button-row button,
  .button-link {
    width: 100%;
  }

  .segmented button {
    padding: 0 10px;
  }

  .metric strong {
    font-size: 22px;
  }

  .chart-label {
    font-size: 11px;
  }
}
