:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1a1d21;
  --muted: #6b7280;
  --accent: #d97706;
  --accent-dark: #b45309;
  --accent-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --ok: #16a34a;
  --border: #e5e7eb;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.06);
  --nav-h: 64px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.icon { width: 20px; height: 20px; flex: none; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-brand {
  display: flex; align-items: center; gap: 8px;
  font-weight: 700; font-size: 17px; color: var(--text); text-decoration: none;
}
.brand-logo { height: 34px; width: auto; display: block; }
.topbar-user { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.topbar-user .icon-btn { width: 34px; height: 34px; }

/* ---------- Layout ---------- */
.view { padding: 16px; padding-bottom: calc(var(--nav-h) + 28px); max-width: 960px; margin: 0 auto; }

.bottomnav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
}
.bottomnav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 500;
}
.bottomnav a.active { color: var(--accent); }

/* ---------- Page header ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.page-title { font-size: 22px; font-weight: 700; margin: 0; display: flex; align-items: center; gap: 10px; min-width: 0; }
.page-title .icon { color: var(--accent); }
.page-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

.back-btn {
  display: inline-flex; align-items: center; gap: 4px;
  background: none; border: none; color: var(--muted); font-size: 14px; font-weight: 500;
  padding: 6px 8px 6px 4px; margin: -6px -8px 10px -4px; cursor: pointer; border-radius: 8px;
}
.back-btn:hover { background: var(--border); color: var(--text); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: none; border-radius: 10px; padding: 10px 16px;
  font-size: 14px; font-weight: 600; cursor: pointer;
  background: var(--accent); color: #fff;
  transition: background .15s;
}
.btn:hover { background: var(--accent-dark); }
.btn:disabled { opacity: .6; cursor: default; }
.btn.ghost { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--bg); }
.btn.danger { background: var(--danger); }
.btn.danger:hover { background: #b91c1c; }
.btn.sm { padding: 7px 12px; font-size: 13px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
  cursor: pointer; transition: all .15s;
}
.icon-btn:hover { color: var(--text); border-color: #d1d5db; }
.icon-btn.danger:hover { color: var(--danger); border-color: var(--danger-soft); background: var(--danger-soft); }
.icon-btn:disabled { opacity: .5; cursor: default; }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 18px); z-index: 25;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: #fff; border: none; cursor: pointer;
  box-shadow: 0 4px 14px rgba(217,119,6,.4);
  display: flex; align-items: center; justify-content: center;
}
.fab .icon { width: 24px; height: 24px; }
.fab:hover { background: var(--accent-dark); }

/* ---------- Search ---------- */
.search-box {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 0 12px; margin-bottom: 14px; color: var(--muted);
}
.search-box input { border: none; outline: none; padding: 10px 0; flex: 1; font-size: 15px; background: transparent; color: var(--text); }

/* ---------- Card / Table ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); font-weight: 600;
  padding: 10px 14px; border-bottom: 1px solid var(--border); background: #fafafa;
  white-space: nowrap;
}
tbody td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: #fafafa; }
td .cell-main { font-weight: 600; }
td .cell-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.row-actions .icon-btn { width: 32px; height: 32px; border-radius: 8px; }
.row-actions .icon-btn .icon { width: 16px; height: 16px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--accent-soft); color: var(--accent-dark);
  font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; white-space: nowrap;
}

/* ---------- Expand / Details ---------- */
.cell-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }
.expand-btn { width: 30px; height: 30px; border-radius: 8px; flex: none; }
.expand-btn .icon { width: 16px; height: 16px; transition: transform .15s ease; }
.expand-btn.open .icon { transform: rotate(180deg); }
.cell-details { margin-top: 8px; padding: 8px 12px; background: var(--bg); border-radius: 8px; }
.detail-line { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; color: var(--muted); padding: 2px 0; }
.detail-line .dv { color: var(--text); font-weight: 500; text-align: right; word-break: break-word; }

/* ---------- Offline banner ---------- */
.offline-banner {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: #fef3c7; color: #92400e;
  font-size: 13px; font-weight: 600;
  padding: 8px 14px;
  border-bottom: 1px solid #fde68a;
}
.offline-banner .icon { width: 16px; height: 16px; }
.badge.report { background: #dcfce7; color: #15803d; }
.badge.neutral { background: var(--bg); color: var(--muted); }

.empty {
  padding: 40px 20px; text-align: center; color: var(--muted);
}
.empty .icon { width: 40px; height: 40px; opacity: .4; margin-bottom: 10px; }

/* Site info card */
.info-card { padding: 16px; margin-bottom: 14px; }
.info-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.info-head .icon-btn { width: 34px; height: 34px; }
.info-card h2 { margin: 0 0 8px; font-size: 19px; }
.info-row { display: flex; gap: 8px; align-items: flex-start; color: var(--muted); font-size: 14px; margin-top: 6px; }
.info-row .icon { width: 16px; height: 16px; margin-top: 2px; color: var(--accent); }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 4px; margin-bottom: 14px; }
.tabs a {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 7px;
  padding: 9px 10px; border-radius: 9px; text-decoration: none;
  color: var(--muted); font-size: 14px; font-weight: 600;
}
.tabs a.active { background: var(--accent-soft); color: var(--accent-dark); }
.tabs .icon { width: 17px; height: 17px; }

/* ---------- Filter bar ---------- */
.filter-bar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.filter-bar select {
  flex: 1; min-width: 130px;
  padding: 9px 10px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-size: 14px; color: var(--text);
}

/* ---------- Sum bar ---------- */
.sum-bar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--accent-soft); color: var(--accent-dark);
  border-radius: 10px; padding: 10px 14px; margin-top: 12px;
  font-size: 14px; font-weight: 600;
}
.sum-bar .icon { width: 18px; height: 18px; }

/* ---------- Documents grid ---------- */
.doc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 12px; }
.doc-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  overflow: hidden; position: relative; text-decoration: none; color: var(--text);
  display: block;
}
.doc-thumb {
  width: 100%; aspect-ratio: 1; object-fit: cover; display: block; background: var(--bg);
}
.doc-thumb-icon {
  width: 100%; aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  background: var(--bg); color: var(--muted);
}
.doc-thumb-icon .icon { width: 32px; height: 32px; }
.doc-name-row { display: flex; align-items: center; justify-content: space-between; gap: 4px; padding: 6px 8px 0; }
.doc-name {
  font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--muted);
}
.doc-card .expand-btn { width: 24px; height: 24px; border-radius: 6px; }
.doc-card .expand-btn .icon { width: 14px; height: 14px; }
.doc-card .cell-details { margin: 0; border-radius: 0; padding: 4px 8px 8px; }
.doc-card .detail-line { font-size: 11px; }
.doc-del {
  position: absolute; top: 6px; right: 6px;
  width: 28px; height: 28px; border-radius: 8px; border: none; cursor: pointer;
  background: rgba(0,0,0,.55); color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.doc-del .icon { width: 14px; height: 14px; }
.doc-del:hover { background: var(--danger); }

.upload-row { display: flex; gap: 8px; margin-bottom: 14px; }
.upload-row .btn { flex: 1; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--surface); width: 100%; max-width: 480px;
  border-radius: 18px 18px 0 0;
  max-height: 92vh; display: flex; flex-direction: column;
  animation: slideUp .18s ease-out;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px 0;
}
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-body { padding: 14px 18px; overflow-y: auto; }
.modal-foot {
  display: flex; gap: 10px; justify-content: flex-end;
  padding: 12px 18px calc(16px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
.modal-foot .btn { flex: 1; }
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; padding: 20px; }
  .modal { border-radius: 16px; }
}

/* ---------- Forms ---------- */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 11px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  font-size: 15px; font-family: inherit; color: var(--text); background: var(--surface);
  outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field textarea { resize: vertical; min-height: 70px; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

.checkbox-field {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; cursor: pointer; margin-bottom: 14px;
}
.checkbox-field input { width: 20px; height: 20px; accent-color: var(--accent); }
.checkbox-field span { font-size: 14px; font-weight: 600; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card {
  width: 100%; max-width: 380px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  box-shadow: var(--shadow); padding: 28px 24px;
}
.login-logo {
  margin: 0 auto 16px; max-width: 180px; text-align: center;
}
.login-logo img { max-width: 100%; height: auto; display: inline-block; }
.login-card h1 { text-align: center; font-size: 20px; margin: 0 0 4px; }
.login-card .sub { text-align: center; color: var(--muted); font-size: 13px; margin: 0 0 20px; }
.login-err {
  background: var(--danger-soft); color: var(--danger);
  font-size: 13px; font-weight: 500; border-radius: 10px;
  padding: 10px 12px; margin-bottom: 12px;
}
.login-card .btn { width: 100%; padding: 12px; }

/* ---------- Toast ---------- */
#toast-root { position: fixed; top: 14px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 380px); }
.toast {
  display: flex; align-items: center; gap: 9px;
  background: #1f2937; color: #fff;
  border-radius: 12px; padding: 11px 14px;
  font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow);
  animation: toastIn .2s ease-out;
}
@keyframes toastIn { from { transform: translateY(-12px); opacity: 0; } to { transform: none; opacity: 1; } }
.toast .icon { width: 17px; height: 17px; color: #4ade80; }
.toast.err .icon { color: #f87171; }

/* ---------- Mobile table -> cards ---------- */
@media (max-width: 640px) {
  .table-wrap table, .table-wrap thead, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; width: 100%; }
  .table-wrap thead { display: none; }
  .table-wrap tr {
    border: 1px solid var(--border); border-radius: 12px;
    margin-bottom: 10px; background: var(--surface);
    box-shadow: var(--shadow); overflow: hidden;
  }
  .table-wrap td {
    display: flex; justify-content: space-between; align-items: center; gap: 10px;
    border-bottom: 1px solid var(--border); padding: 10px 14px;
  }
  .table-wrap td:last-child { border-bottom: none; }
  .table-wrap td::before {
    content: attr(data-label);
    font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
    color: var(--muted); font-weight: 600; flex: none;
  }
  .table-wrap td.cell-none::before { content: none; }
  .table-wrap td.cell-actions { justify-content: flex-end; }
  .table-wrap td[data-expand-target] { display: block; }
  .table-wrap td[data-expand-target] .cell-head { width: 100%; }
}
