:root {
  --bg-base: #0c0f0d;
  --bg-surface: #131815;
  --bg-elevated: #19201c;
  --bg-input: #0e1310;
  --border: #28332c;
  --border-strong: #3e4f44;
  --text-primary: #e2e8e3;
  --text-secondary: #98a69c;
  --text-muted: #647369;
  --accent-olive: #91a87a;
  --accent-amber: #d9a441;
  --income: #4da876;
  --income-bg: rgba(77, 168, 118, 0.12);
  --expense: #cf635a;
  --expense-bg: rgba(207, 99, 90, 0.12);
  --cashless: #5fa3c7;
  --cashless-bg: rgba(95, 163, 199, 0.14);
  --font-sans: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "SFMono-Regular", "Consolas", "Liberation Mono", monospace;
}

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

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background-color: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.tactical-grid-bg {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image:
    linear-gradient(to right, rgba(145, 168, 122, 0.04) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(145, 168, 122, 0.04) 1px, transparent 1px);
  background-size: 28px 28px;
}

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.hidden {
  display: none !important;
}

.text-right {
  text-align: right;
}

.muted-text {
  color: var(--text-secondary);
  font-size: 12px;
}

/* Panels & Reticles */
.tactical-panel {
  position: relative;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.corner-reticle {
  position: absolute;
  width: 8px;
  height: 8px;
  border-color: var(--accent-olive);
  border-style: solid;
  pointer-events: none;
  opacity: 0.7;
}

.corner-reticle.tl { top: -1px; left: -1px; border-width: 2px 0 0 2px; }
.corner-reticle.tr { top: -1px; right: -1px; border-width: 2px 2px 0 0; }
.corner-reticle.bl { bottom: -1px; left: -1px; border-width: 0 0 2px 2px; }
.corner-reticle.br { bottom: -1px; right: -1px; border-width: 0 2px 2px 0; }

.radar-icon {
  color: var(--accent-olive);
  flex-shrink: 0;
}

/* Login Page */
.login-shell {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(20px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(20px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.login-card {
  width: 100%;
  max-width: 440px;
  padding: 28px;
}

.login-header {
  border-bottom: 1px solid var(--border);
  padding-bottom: 16px;
  margin-bottom: 20px;
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sys-kicker {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  color: var(--accent-olive);
  text-transform: uppercase;
}

.sys-title,
.sys-brand {
  margin: 2px 0 0;
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--text-primary);
}

.telemetry-strip {
  margin-top: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  color: var(--text-secondary);
}

.status-dot-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-mono);
}

.status-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
}

.status-dot.ok {
  background: var(--income);
}

.login-footer {
  margin-top: 20px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--text-muted);
}

/* Dashboard Layout */
.app-shell {
  position: relative;
  z-index: 1;
  max-width: 1440px;
  margin: 0 auto;
  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(20px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar {
  padding: 12px 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar-left,
.topbar-center,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sensor-pill,
.clock-pill,
.operator-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-secondary);
}

.role-tag {
  padding: 1px 6px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  border: 1px solid;
}

.role-admin {
  color: var(--accent-amber);
  border-color: rgba(217, 164, 65, 0.45);
  background: rgba(217, 164, 65, 0.1);
}

.role-viewer {
  color: var(--accent-olive);
  border-color: rgba(145, 168, 122, 0.45);
  background: rgba(145, 168, 122, 0.1);
}

/* Tabs */
.view-tabs {
  display: flex;
  gap: 8px;
}

.tab-btn {
  background: var(--bg-surface);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  padding: 9px 16px;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.06em;
  cursor: pointer;
  touch-action: manipulation;
}

.tab-btn.active {
  color: var(--text-primary);
  border-color: var(--accent-olive);
  background: var(--bg-elevated);
}

/* Summary Cards */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.metric-card {
  padding: 16px 18px;
}

.metric-label {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
}

.metric-value {
  margin-top: 8px;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.metric-card.income .metric-value { color: var(--income); }
.metric-card.expense .metric-value { color: var(--expense); }
.metric-card.cashless .metric-value { color: var(--cashless); }
.metric-card.balance .metric-value.positive { color: var(--income); }
.metric-card.balance .metric-value.negative { color: var(--expense); }

.metric-meta {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
}

/* Forms & Filters */
.toolbar-panel {
  padding: 14px 16px;
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 140px;
}

.search-group {
  flex: 1 1 240px;
}

.filter-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
}

.tactical-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

label,
.field-caption {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text-secondary);
}

input,
select,
textarea {
  width: 100%;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  padding: 9px 11px;
  font-family: inherit;
  font-size: 13px;
  outline: none;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent-olive);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.05em;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-primary);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  touch-action: manipulation;
}

.btn:hover:not(:disabled) {
  border-color: var(--accent-olive);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: #2f4232;
  border-color: var(--accent-olive);
  color: #eef4ec;
}

.btn-primary:hover:not(:disabled) {
  background: #3a523d;
}

.btn-secondary {
  background: #202823;
  border-color: var(--border-strong);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-secondary);
}

.btn-danger {
  background: rgba(207, 99, 90, 0.14);
  border-color: rgba(207, 99, 90, 0.55);
  color: #f0a7a1;
}

.btn-sm {
  padding: 5px 9px;
  font-size: 11px;
}

.btn-block {
  width: 100%;
}

/* Alerts */
.alert-box {
  padding: 9px 12px;
  border: 1px solid var(--border);
  font-size: 12px;
  background: var(--bg-elevated);
}

.alert-box.error {
  border-color: rgba(207, 99, 90, 0.6);
  background: var(--expense-bg);
  color: #f2b4ae;
}

.alert-box.success {
  border-color: rgba(77, 168, 118, 0.6);
  background: var(--income-bg);
  color: #a8e0be;
}

/* Tables */
.table-panel {
  padding: 0;
}

.panel-pad {
  padding: 18px;
}

.panel-header-row {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-title {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 14px;
  letter-spacing: 0.06em;
}

.section-sub {
  margin: 6px 0 16px;
  font-size: 12px;
  color: var(--text-secondary);
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.tactical-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 780px;
}

.tactical-table th {
  text-align: left;
  padding: 11px 14px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border-strong);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text-secondary);
  font-weight: 600;
}

.tactical-table td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.tactical-table tbody tr:hover {
  background: rgba(145, 168, 122, 0.04);
}

.empty-cell {
  text-align: center;
  padding: 28px 14px !important;
  color: var(--text-muted);
}

.item-title {
  font-weight: 600;
  color: var(--text-primary);
}

.item-desc {
  margin-top: 3px;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-break: break-word;
}

.badge {
  display: inline-block;
  padding: 2px 7px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  border: 1px solid;
}

.badge-income {
  color: var(--income);
  background: var(--income-bg);
  border-color: rgba(77, 168, 118, 0.45);
}

.badge-expense {
  color: var(--expense);
  background: var(--expense-bg);
  border-color: rgba(207, 99, 90, 0.45);
}

.badge-cashless {
  color: var(--cashless);
  background: var(--cashless-bg);
  border-color: rgba(95, 163, 199, 0.5);
}

.badge-inactive {
  color: var(--text-muted);
  background: rgba(100, 115, 105, 0.14);
  border-color: var(--border-strong);
}

.amount-cell {
  font-weight: 700;
  white-space: nowrap;
}

.amount-income { color: var(--income); }
.amount-expense { color: var(--expense); }
.amount-cashless { color: var(--cashless); }

.att-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.att-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 11px;
  cursor: pointer;
  max-width: 220px;
  touch-action: manipulation;
}

.att-chip:hover {
  border-color: var(--accent-amber);
}

.att-chip-kind {
  color: var(--accent-amber);
  font-weight: 700;
}

.att-chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.pagination-bar {
  padding: 10px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-controls {
  display: flex;
  gap: 8px;
}

/* Users tab */
.users-layout {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(6, 8, 7, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-card {
  width: 100%;
  max-height: 92vh;
  overflow-y: auto;
  padding: 20px;
}

.modal-sm { max-width: 420px; }
.modal-lg { max-width: 680px; }
.modal-xl { max-width: 960px; }

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding-bottom: 12px;
  margin-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

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

.preview-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.preview-footer {
  margin-top: 12px;
}

.preview-body {
  background: var(--bg-input);
  border: 1px solid var(--border);
  min-height: 360px;
  max-height: 72vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}

.preview-body img {
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
}

.preview-body iframe {
  width: 100%;
  height: 70vh;
  border: none;
}

.pdf-preview-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.pdf-mobile-hint {
  display: none;
  padding: 8px 12px;
  font-size: 11px;
  color: var(--text-secondary);
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
}

.existing-attachments {
  border: 1px solid var(--border);
  background: var(--bg-input);
  padding: 10px;
}

.att-manage-list {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.att-manage-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
}

.att-manage-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

@media (max-width: 960px) {
  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .users-layout,
  .form-grid-2 {
    grid-template-columns: 1fr;
  }

  .topbar-center {
    display: none;
  }
}

@media (max-width: 768px) {
  .app-shell {
    padding:
      max(10px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(16px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
    gap: 10px;
  }

  .topbar {
    padding: 10px 12px;
    gap: 10px;
  }

  .topbar-left {
    width: 100%;
  }

  .topbar-right {
    width: 100%;
    justify-content: space-between;
  }

  .sys-kicker {
    font-size: 9px;
    letter-spacing: 0.06em;
  }

  .sys-title,
  .sys-brand {
    font-size: 16px;
  }

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

  .tab-btn {
    min-height: 42px;
    padding: 9px 10px;
    font-size: 11px;
    text-align: center;
  }

  .btn,
  input,
  select {
    min-height: 42px;
  }

  input,
  select,
  textarea {
    font-size: 16px;
  }

  .btn-sm {
    min-height: 36px;
    padding: 6px 10px;
  }

  .toolbar-panel {
    padding: 12px;
  }

  .filter-bar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    align-items: end;
  }

  .filter-group {
    min-width: 0;
  }

  .search-group,
  .filter-actions {
    grid-column: 1 / -1;
  }

  .filter-actions {
    margin-left: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  #new-entry-btn {
    grid-column: 1 / -1;
  }

  /* Responsive table -> stacked tactical cards */
  .table-wrap {
    overflow-x: visible;
  }

  .tactical-table {
    min-width: 0;
    display: block;
  }

  .tactical-table thead {
    display: none;
  }

  .tactical-table tbody {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 10px;
  }

  .tactical-table tbody tr {
    display: block;
    background: var(--bg-elevated);
    border: 1px solid var(--border-strong);
    padding: 10px 12px;
  }

  .tactical-table tbody tr:hover {
    background: var(--bg-elevated);
  }

  .tactical-table td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 10px;
    padding: 6px 0;
    border-bottom: 1px dashed var(--border);
    text-align: left;
  }

  .tactical-table td::before {
    content: attr(data-label);
    font-family: var(--font-mono);
    font-size: 10px;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    flex-shrink: 0;
    padding-top: 2px;
  }

  .tactical-table td:last-child {
    border-bottom: none;
  }

  .tactical-table td.col-item {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }

  .tactical-table td.col-att {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .tactical-table td.col-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    padding-top: 8px;
  }

  .tactical-table td.col-actions .row-actions {
    width: 100%;
    justify-content: stretch;
  }

  .tactical-table td.col-actions .row-actions .btn {
    flex: 1 1 0;
  }

  .tactical-table td.empty-cell {
    display: block;
    text-align: center;
    border-bottom: none;
  }

  .tactical-table td.empty-cell::before {
    content: none;
  }

  .att-chip {
    max-width: 100%;
  }

  .pagination-bar {
    flex-direction: column;
    gap: 8px;
    align-items: stretch;
  }

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

  .pdf-mobile-hint {
    display: block;
  }
}

@media (max-width: 640px) {
  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .metric-card {
    padding: 12px;
  }

  .metric-value {
    font-size: 20px;
    word-break: break-word;
  }

  .modal-backdrop {
    padding: 0;
    align-items: flex-end;
  }

  .modal-card {
    max-width: 100%;
    max-height: 94dvh;
    border-left: none;
    border-right: none;
    border-bottom: none;
    padding: 16px 14px max(16px, env(safe-area-inset-bottom));
  }

  .modal-header {
    flex-wrap: wrap;
  }

  .preview-header-actions {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .preview-close-btn {
    grid-column: 1 / -1;
    min-height: 44px;
    font-size: 13px;
  }

  .preview-body {
    min-height: 220px;
    max-height: 56dvh;
  }

  .preview-body img {
    max-height: 54dvh;
  }

  .preview-body iframe {
    height: 50dvh;
  }

  .preview-footer .btn {
    min-height: 46px;
    font-size: 14px;
  }

  .att-manage-row {
    flex-direction: column;
    align-items: stretch;
  }

  .att-manage-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .att-manage-actions .btn-sm {
    padding: 6px 4px;
    font-size: 10px;
  }

  .modal-footer {
    flex-direction: column-reverse;
  }

  .modal-footer .btn {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 20px 16px;
  }

  .telemetry-strip,
  .login-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  .filter-bar {
    grid-template-columns: 1fr;
  }
}
