/* ============================================================
   PMI Billing Team — App Styles
   ============================================================ */

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

:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --border: #d1d5db;
  --border-light: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --secondary: #374151;
  --accent: #0ea5e9;
  --danger: #dc2626;
  --row-hover: #f9fafb;
  --row-selected: #eff6ff;
  --drawer-w: 480px;
  --topbar-h: 52px;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.1);
}

body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg); color: var(--text); font-size: 13px; height: 100dvh; overflow: hidden; }

/* ---- TOPBAR ---- */
.topbar {
  height: var(--topbar-h); background: #1a3d2b; border-bottom: 1px solid #14301f;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; gap: 12px; box-shadow: var(--shadow); position: relative; z-index: 100;
}
.topbar-logo-img { height: 32px; width: auto; object-fit: contain; flex-shrink: 0; }
.topbar-title {
  font-size: 13px; color: rgba(255,255,255,.75); margin-left: 10px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px;
}
.topbar-left { display: flex; align-items: center; gap: 4px; }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; }

/* ---- BUTTONS ---- */
.btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 5px 12px; border-radius: var(--radius); font-size: 12px; font-weight: 500;
  border: 1px solid transparent; cursor: pointer; white-space: nowrap; transition: background .15s;
}
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); color: var(--secondary); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-sm { padding: 3px 8px; font-size: 11px; }

/* ---- SEARCH ---- */
.search-box {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 5px 10px;
  font-size: 12px; width: 220px; outline: none;
}
.search-box:focus { border-color: var(--accent); }

/* ---- LAYOUT ---- */
.layout {
  display: flex; height: calc(100dvh - var(--topbar-h)); overflow: hidden;
}

/* ---- GRID PANEL ---- */
.grid-panel {
  flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; position: relative;
  overflow: hidden;
}
.table-wrap {
  flex: 1 1 auto; overflow: auto; position: relative;
}

table { border-collapse: collapse; width: 100%; table-layout: auto; }

thead tr { background: #f8fafc; position: sticky; top: 0; z-index: 10; }
thead th {
  padding: 9px 10px; text-align: left; font-size: 11px; font-weight: 600;
  color: var(--text-muted); border-bottom: 2px solid var(--border); white-space: nowrap;
  cursor: pointer; user-select: none;
}
thead th:hover { background: #f1f5f9; }
thead th .sort-icon { opacity: .4; margin-left: 3px; font-size: 10px; }
thead th.sorted .sort-icon { opacity: 1; color: var(--primary); }

tbody tr {
  border-bottom: 1px solid var(--border-light); cursor: pointer; transition: background .1s;
}
tbody tr:hover { background: var(--row-hover); }
tbody tr.selected { background: var(--row-selected); }

tbody td {
  padding: 7px 10px; vertical-align: middle; max-width: 240px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
tbody td.bool-cell { text-align: center; }

.attach-badge {
  display: inline-flex; align-items: center; gap: 3px;
  background: var(--accent); color: #fff; border-radius: 10px;
  padding: 1px 7px; font-size: 10px; font-weight: 600;
}

.grid-footer {
  padding: 6px 12px; font-size: 11px; color: var(--text-muted);
  border-top: 1px solid var(--border); background: var(--surface);
}

/* ---- LOADING ---- */
.loading-overlay {
  position: absolute; inset: 0; background: rgba(255,255,255,.85);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  z-index: 50; gap: 12px;
}
.spinner {
  width: 32px; height: 32px; border: 3px solid var(--border);
  border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- DRAWER ---- */
.drawer {
  width: 0; flex-shrink: 0; background: var(--surface);
  border-left: 1px solid var(--border); display: flex; flex-direction: column;
  overflow: hidden; transition: width .2s ease; box-shadow: var(--shadow-lg);
}
.drawer.open { width: var(--drawer-w); }

.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
  flex-shrink: 0; background: var(--surface);
}
.drawer-title { font-weight: 600; font-size: 14px; }
.drawer-header-actions { display: flex; align-items: center; gap: 6px; }
.drawer-close {
  background: none; border: none; font-size: 20px; cursor: pointer;
  color: var(--text-muted); line-height: 1; padding: 2px 6px; border-radius: 4px;
}
.drawer-close:hover { background: var(--bg); }

/* Color strip */
.color-strip {
  display: flex; gap: 6px; padding: 8px 14px;
  border-bottom: 1px solid var(--border-light); flex-shrink: 0;
}
.color-swatch {
  width: 22px; height: 22px; border-radius: 4px; cursor: pointer;
  border: 2px solid transparent; transition: transform .1s;
}
.color-swatch:hover { transform: scale(1.2); }

/* Fields */
.drawer-fields {
  flex: 0 0 auto; overflow-y: auto; padding: 12px 14px; max-height: 40%;
  border-bottom: 1px solid var(--border-light);
}
.field-row { display: flex; flex-direction: column; gap: 3px; margin-bottom: 10px; }
.field-label { font-size: 10px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .4px; }
.field-input, .field-textarea {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 5px 8px;
  font-size: 12px; font-family: inherit; width: 100%; background: var(--surface); color: var(--text);
  transition: border-color .15s;
}
.field-input:focus, .field-textarea:focus { outline: none; border-color: var(--accent); }
.field-textarea { resize: vertical; min-height: 60px; }
.field-checkbox-row { display: flex; align-items: center; gap: 8px; }
.field-checkbox { width: 16px; height: 16px; cursor: pointer; accent-color: var(--primary); }

/* ---- ATTACHMENTS ---- */
.drawer-section {
  flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; overflow: hidden;
}
.drawer-section-header {
  display: flex; align-items: center; gap: 8px; padding: 10px 14px;
  border-bottom: 1px solid var(--border-light); flex-shrink: 0;
  font-weight: 600; font-size: 12px;
}
.upload-label { cursor: pointer; }

.upload-progress { padding: 6px 14px; font-size: 11px; color: var(--accent); flex-shrink: 0; }

.attachments-list {
  flex: 1 1 auto; overflow-y: auto; padding: 8px 14px; display: flex; flex-direction: column; gap: 6px;
}
.no-attach { color: var(--text-muted); font-size: 11px; }

.attach-item {
  display: flex; align-items: center; gap: 8px; padding: 7px 10px;
  border: 1px solid var(--border-light); border-radius: var(--radius);
  background: var(--bg); transition: background .1s;
}
.attach-item:hover { background: #f1f5f9; }
.attach-icon { font-size: 18px; flex-shrink: 0; }
.attach-info { flex: 1 1 auto; min-width: 0; }
.attach-name { font-size: 12px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-meta { font-size: 10px; color: var(--text-muted); }
.attach-actions { display: flex; gap: 4px; flex-shrink: 0; }

/* ---- PREVIEW PANEL ---- */
.preview-panel {
  flex-shrink: 0; display: flex; flex-direction: column;
  border-top: 2px solid var(--primary); background: #1a1a2e;
  height: 50%;
}
.preview-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; background: #16213e; flex-shrink: 0; gap: 8px;
}
.preview-filename { font-size: 11px; color: #a0aec0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview-body { flex: 1 1 auto; overflow: auto; display: flex; align-items: center; justify-content: center; }
.preview-body iframe { width: 100%; height: 100%; border: none; }
.preview-body img { max-width: 100%; max-height: 100%; object-fit: contain; }
.preview-unsupported { color: #718096; font-size: 12px; text-align: center; padding: 20px; }

/* ---- STATUS ---- */
.status-msg { font-size: 11px; color: var(--text-muted); }
.status-msg.error { color: var(--danger); }
.status-msg.ok { color: #16a34a; }

/* ---- BACK BUTTON (navigation, not action) ---- */
.btn-back-nav {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: var(--radius); font-size: 12px; font-weight: 500;
  background: rgba(255,255,255,.12); color: rgba(255,255,255,.9);
  border: 1px solid rgba(255,255,255,.25); cursor: pointer; white-space: nowrap;
  transition: background .15s;
}
.btn-back-nav:hover { background: rgba(255,255,255,.22); }
.btn-back-nav .back-arrow { font-size: 14px; line-height: 1; }

/* ---- SPLASH NEW SHEET BUTTON ---- */
.btn-new-sheet-splash {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 20px; border-radius: 8px; font-size: 13px; font-weight: 600;
  background: #1a3d2b; color: #fff; border: none; cursor: pointer;
  box-shadow: 0 2px 8px rgba(26,61,43,.3); transition: background .15s, box-shadow .15s;
}
.btn-new-sheet-splash:hover { background: #14301f; box-shadow: 0 4px 12px rgba(26,61,43,.4); }
.btn-new-sheet-splash .plus-icon { font-size: 18px; line-height: 1; font-weight: 300; }

/* ---- SPLASH ---- */
#splash-view {
  height: calc(100dvh - var(--topbar-h)); overflow-y: auto; background: var(--bg);
}
.splash-container {
  max-width: 1100px; margin: 0 auto; padding: 40px 24px;
}
.splash-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 28px;
}
.splash-heading {
  font-size: 22px; font-weight: 700; color: var(--text);
}
.splash-loading {
  display: flex; align-items: center; gap: 12px; color: var(--text-muted);
  font-size: 13px; padding: 20px 0;
}
.sheet-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px;
}
.sheet-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 20px; cursor: pointer; transition: box-shadow .15s, border-color .15s;
  display: flex; flex-direction: column; gap: 8px;
}
.sheet-card:hover {
  box-shadow: var(--shadow-lg); border-color: var(--accent);
}
.sheet-card-icon {
  font-size: 28px; margin-bottom: 4px;
}
.sheet-card-name {
  font-weight: 600; font-size: 14px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sheet-card-meta {
  font-size: 11px; color: var(--text-muted);
}
.sheet-card-badge {
  display: inline-block; background: #1a3d2b; color: #fff;
  font-size: 9px; font-weight: 700; padding: 2px 7px; border-radius: 10px;
  letter-spacing: .4px; align-self: flex-start; margin-top: 4px;
}

/* ---- CHECKBOX COL ---- */
.cb-cell { width: 36px; text-align: center; padding: 0 4px !important; }
.cb-cell input[type=checkbox] { accent-color: var(--primary); cursor: pointer; width: 14px; height: 14px; }

/* ---- FILTER ---- */
.th-filter-btn {
  background: none; border: none; cursor: pointer; font-size: 10px;
  color: var(--text-muted); padding: 0 2px; margin-left: 2px; opacity: .5;
  vertical-align: middle;
}
.th-filter-btn:hover, .th-filter-btn.active { opacity: 1; color: var(--primary); }
thead th.filtered { background: #eff6ff; }

.filter-dropdown {
  position: fixed; z-index: 1000; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); min-width: 200px; max-width: 280px;
}
.filter-search-wrap {
  display: flex; align-items: center; gap: 4px; padding: 6px 8px;
  border-bottom: 1px solid var(--border-light);
}
.filter-search {
  flex: 1; border: 1px solid var(--border); border-radius: 4px;
  padding: 4px 6px; font-size: 11px; outline: none;
}
.filter-search:focus { border-color: var(--accent); }
.filter-clear-btn {
  background: none; border: none; cursor: pointer; color: var(--danger);
  font-size: 12px; padding: 2px 4px; border-radius: 3px;
}
.filter-clear-btn:hover { background: #fee2e2; }
.filter-values {
  max-height: 200px; overflow-y: auto; padding: 4px 0;
}
.filter-val-row {
  display: flex; align-items: center; gap: 6px; padding: 4px 10px;
  font-size: 11px; cursor: pointer;
}
.filter-val-row:hover { background: var(--bg); }
.filter-val-row input { accent-color: var(--primary); cursor: pointer; }

/* ---- MODALS ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; z-index: 500;
}
.modal {
  background: var(--surface); border-radius: 8px; box-shadow: var(--shadow-lg);
  width: 90%; max-width: 560px; max-height: 85dvh;
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.modal-title { font-weight: 600; font-size: 14px; }
.modal-body { flex: 1 1 auto; overflow-y: auto; padding: 14px 16px; }
.modal-footer {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 12px 16px; border-top: 1px solid var(--border); flex-shrink: 0;
}

/* ---- ADD COL TH ---- */
th.th-add-col {
  position: sticky; right: 0; background: #f8fafc;
  border-left: 1px solid var(--border); width: 36px; text-align: center;
  cursor: pointer; font-size: 16px; color: var(--text-muted); z-index: 11;
}
th.th-add-col:hover { background: #e0f2fe; color: var(--primary); }

/* ---- COL RESIZE HANDLE ---- */
thead th { position: relative; }
.col-resize-handle {
  position: absolute; right: 0; top: 0; bottom: 0; width: 5px;
  cursor: col-resize; z-index: 5;
}
.col-resize-handle:hover { background: var(--accent); opacity: .4; }

/* ---- ROW WRAP ---- */
tbody tr.wrap td { white-space: normal; word-break: break-word; }

/* ---- ATTACH ICON BADGE ---- */
.attach-icon-badge {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--border); color: var(--text-muted); border-radius: 4px;
  font-size: 9px; font-weight: 700; width: 32px; height: 26px; flex-shrink: 0;
  letter-spacing: .3px;
}
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #9ca3af; }

/* ---- READ-ONLY FIELD DISPLAY ---- */
.field-value {
  border: 1px solid var(--border-light); border-radius: var(--radius); padding: 5px 8px;
  font-size: 12px; width: 100%; background: var(--bg); color: var(--text);
  min-height: 15px; white-space: pre-wrap; word-break: break-word;
}

/* ---- COMMENTS ---- */
.comments-section {
  flex-shrink: 0; max-height: 45%; display: flex; flex-direction: column;
  border-top: 1px solid var(--border-light); overflow: hidden;
}
.comments-list {
  flex: 1 1 auto; overflow-y: auto; padding: 8px 14px; display: flex; flex-direction: column; gap: 8px;
}
.no-comments { color: var(--text-muted); font-size: 11px; }
.comment-item {
  padding: 7px 10px; border: 1px solid var(--border-light); border-radius: var(--radius);
  background: var(--bg);
}
.comment-author { font-size: 11px; font-weight: 600; color: var(--text); }
.comment-time { font-size: 10px; color: var(--text-muted); margin-left: 6px; }
.comment-text { font-size: 12px; margin-top: 3px; white-space: pre-wrap; word-break: break-word; }
.comment-form {
  flex-shrink: 0; padding: 10px 14px; border-top: 1px solid var(--border-light);
  display: flex; flex-direction: column; gap: 6px;
}
.comment-form input, .comment-form textarea {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 5px 8px;
  font-size: 12px; font-family: inherit; width: 100%;
}
.comment-form textarea { resize: vertical; min-height: 50px; }
