:root {
  --bg: #f7f9f2;
  --surface: #ffffff;
  --surface-muted: #f8faf5;
  --text: #172033;
  --text-muted: #68758a;
  --border: #e3e8ef;
  --primary: #83b624;
  --primary-hover: #6a941d;
  --primary-soft: #f1f8e4;
  --danger: #c03744;
  --open-amount: #a84b52;
  --shadow: 0 16px 42px rgba(22, 32, 51, 0.08);
  --radius: 14px;
  --radius-sm: 9px;
  --sidebar-width: 270px;
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body {
  min-width: 320px;
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system,
    BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.5;
}
button, input, select, textarea { font: inherit; }
button { border: 0; cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid rgba(131, 182, 36, 0.24);
  outline-offset: 2px;
}
.hidden { display: none !important; }

#loginScreen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at top right, #e5f1c9 0, transparent 32%),
    radial-gradient(circle at bottom left, #eef7dc 0, transparent 28%),
    var(--bg);
}
.login-card {
  width: min(100%, 420px);
  padding: 36px;
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid rgba(227, 232, 239, 0.9);
  border-radius: 20px;
  box-shadow: var(--shadow);
}
.app-name {
  margin-bottom: 12px;
  color: var(--text);
  font-size: 24px;
  font-weight: 750;
  letter-spacing: -0.02em;
}
.form-stack { display: grid; gap: 16px; }
.field { display: grid; gap: 7px; }
.field label { color: #465269; font-size: 13px; font-weight: 650; }
.field input, .field textarea {
  width: 100%;
  color: var(--text);
  background: var(--surface);
  border: 1px solid #cfd8e5;
  border-radius: var(--radius-sm);
}
.field input { height: 44px; padding: 0 12px; }
.field textarea { min-height: 96px; padding: 11px 12px; resize: vertical; }
.field input:focus, .field textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(131, 182, 36, 0.12);
  outline: none;
}
.login-submit {
  width: 100%;
  height: 46px;
  margin-top: 4px;
  color: white;
  background: var(--primary);
  border-radius: var(--radius-sm);
  font-weight: 650;
}
.login-submit:hover { background: var(--primary-hover); }
#loginError { min-height: 22px; margin: 16px 0 0; color: var(--danger); font-size: 14px; }
#appScreen { display: none; min-height: 100vh; }

.topbar {
  position: sticky;
  z-index: 10;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 68px;
  padding: 0 32px;
  background: rgba(255, 255, 255, 0.88);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}
.topbar .app-name { margin: 0; }
.user-menu { display: flex; align-items: center; gap: 12px; }
.user-copy { display: grid; gap: 1px; text-align: right; }
.user-name { font-size: 14px; font-weight: 650; }
.user-role { color: var(--text-muted); font-size: 12px; }
#logoutButton { color: #9f2d38; }
#logoutButton:hover { color: #7f202b; background: #fbecee; }
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid #cfd8e5;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
}
.button:hover:not(:disabled) { background: var(--surface-muted); border-color: #aab8ca; }
.button-quiet { border-color: transparent; background: transparent; }
.button-quiet:hover:not(:disabled) { color: var(--primary); background: var(--primary-soft); border-color: transparent; }
.button-primary { color: white; background: var(--primary); border-color: var(--primary); }
.button-primary:hover:not(:disabled) { color: white; background: var(--primary-hover); border-color: var(--primary-hover); }
.button:disabled { cursor: not-allowed; opacity: 0.45; }

.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: calc(100vh - 68px);
}
.navigation-sidebar {
  position: sticky;
  top: 68px;
  align-self: start;
  height: calc(100vh - 68px);
  padding: 24px 16px;
  overflow-y: auto;
  background: #fbfcf8;
  border-right: 1px solid var(--border);
}
.company-list, .region-list {
  display: grid;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.company-item { display: grid; gap: 3px; }
.company-button, .region-button { width: 100%; border-radius: var(--radius-sm); text-align: left; }
.company-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
  padding: 0 12px;
  color: var(--text);
  background: transparent;
  font-weight: 750;
}
.company-button:hover:not(:disabled) { background: var(--primary-soft); }
.company-button.active { color: #456f0a; background: var(--primary-soft); }
.company-button:disabled { cursor: not-allowed; opacity: 0.52; }
.company-chevron { color: var(--text-muted); font-size: 13px; }
.region-list { padding: 2px 0 7px 16px; }
.region-button { min-height: 38px; padding: 0 12px; color: #526078; background: transparent; font-size: 13px; }
.region-button:hover:not(:disabled) { color: var(--text); background: #eef5df; }
.region-button.active { color: #456f0a; background: #e8f3d5; font-weight: 700; }
.region-button:disabled { cursor: not-allowed; opacity: 0.55; }

main { width: min(100%, 1560px); margin: 0 auto; padding: 32px; }
.selection-prompt {
  display: grid;
  min-height: 300px;
  place-items: center;
  padding: 32px;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 3px 10px rgba(22, 32, 51, 0.025);
}
.selection-prompt h1 { margin: 0; font-size: 26px; letter-spacing: -0.04em; }
.selection-prompt p { max-width: 480px; margin: 8px auto 0; color: var(--text-muted); }
.selection-icon { display: grid; width: 52px; height: 52px; margin: 0 auto 14px; place-items: center; color: #527c12; background: var(--primary-soft); border-radius: 50%; font-size: 24px; }
.selection-breadcrumb { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; margin-bottom: 22px; color: var(--text-muted); font-size: 13px; }
.selection-breadcrumb strong { color: var(--text); }
.filter-card { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; padding: 18px; margin-bottom: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 3px 10px rgba(22, 32, 51, 0.025); }
.filters { display: flex; flex: 1; flex-wrap: wrap; gap: 14px; }
.filter-search { flex: 1 1 300px; }
.filter-region { flex: 0 1 220px; }
.filter-actions { display: flex; gap: 10px; }
.region-total-card { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px 20px; margin-bottom: 18px; background: #fbecee; border: 1px solid #efcfd3; border-radius: var(--radius); }
.region-total-label { color: var(--open-amount); font-size: 13px; font-weight: 700; }
#regionOpenTotal { color: var(--open-amount); font-size: 20px; font-variant-numeric: tabular-nums; }
.data-card { overflow: hidden; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 3px 10px rgba(22, 32, 51, 0.025); }
.table-container { width: 100%; max-width: 100%; overflow-x: auto; }
table { width: 100%; min-width: 1020px; border-collapse: collapse; }
th, td { padding: 15px 18px; text-align: left; white-space: nowrap; }
th { color: var(--text-muted); background: var(--surface-muted); border-bottom: 1px solid var(--border); font-size: 11px; font-weight: 750; letter-spacing: 0.07em; text-transform: uppercase; }
td { border-bottom: 1px solid #edf0f4; color: #344055; font-size: 14px; }
tbody tr:last-child td { border-bottom: 0; }
#customersBody tr:hover { background: #f7fbea; }
.sort-button { display: inline-flex; align-items: center; gap: 5px; padding: 0; color: inherit; background: transparent; font-size: inherit; font-weight: inherit; letter-spacing: inherit; text-align: left; text-transform: inherit; }
.sort-button:hover { color: var(--primary); }
.sort-indicator { width: 10px; color: #a8b3c2; font-size: 12px; }
.sort-button[aria-sort="ascending"] .sort-indicator, .sort-button[aria-sort="descending"] .sort-indicator { color: var(--primary); }
.customer-name { color: var(--text); font-weight: 650; }
.muted-cell { color: var(--text-muted); }
.status-message { display: flex; align-items: center; gap: 9px; margin-bottom: 14px; color: var(--text-muted); font-size: 14px; }
.status-message:empty { display: none; }
.status-message.error { color: var(--danger); }
.status-message.empty { justify-content: center; min-height: 130px; margin: 0; color: var(--text-muted); }
.spinner { width: 15px; height: 15px; border: 2px solid #cbd5e1; border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.pagination { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px; border-top: 1px solid var(--border); }
.pagination-info { color: var(--text-muted); font-size: 13px; }
.pagination-buttons { display: flex; gap: 8px; }
#customerFileView { display: none; }
.back-link { margin-bottom: 20px; }
.customer-file-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 380px); gap: 28px; align-items: start; min-width: 0; }
.customer-file-left { display: grid; gap: 28px; min-width: 0; width: 100%; }
.customer-file-left > section, .customer-file-left .section-heading-row, .customer-file-left .data-card { min-width: 0; max-width: 100%; }
.customer-summary { padding: 25px; margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 3px 10px rgba(22, 32, 51, 0.025); }
.customer-summary-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
.customer-summary h1 { margin: 0; font-size: 26px; letter-spacing: -0.04em; }
.eyebrow { margin: 0 0 5px; color: var(--text-muted); font-size: 11px; font-weight: 750; letter-spacing: 0.07em; text-transform: uppercase; }
.customer-number { margin: 5px 0 0; color: var(--text-muted); font-size: 14px; }
.region-badge, .dunning-level { display: inline-flex; align-items: center; justify-content: center; color: #334155; background: #f1f5e8; border: 1px solid #dce8c3; border-radius: 7px; font-size: 12px; font-weight: 700; }
.region-badge { min-height: 29px; padding: 0 10px; border-radius: 99px; font-weight: 650; }
.customer-details { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }
.customer-detail { display: grid; gap: 5px; padding: 14px; background: var(--surface-muted); border: 1px solid #edf0f4; border-radius: var(--radius-sm); }
.customer-detail-label { color: var(--text-muted); font-size: 11px; font-weight: 750; letter-spacing: 0.07em; text-transform: uppercase; }
.customer-detail-value { overflow-wrap: anywhere; color: var(--text); font-size: 14px; font-weight: 600; }
.section-heading-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 14px; }
.section-heading { margin-bottom: 0; }
.section-heading p { margin: 5px 0 0; color: var(--text-muted); font-size: 14px; }
.section-title { margin: 0; color: var(--text); font-size: 27px; letter-spacing: -0.04em; }
.amount { font-variant-numeric: tabular-nums; text-align: right; }
.open-amount { color: var(--open-amount) !important; font-weight: 700; }
#regionOpenTotal.open-amount { color: var(--open-amount) !important; }
.dunning-level { min-width: 26px; height: 26px; }

.comments-panel { position: sticky; top: 92px; display: flex; flex-direction: column; width: 100%; min-width: 0; min-height: calc(100vh - 124px); max-height: calc(100vh - 124px); overflow: hidden; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 3px 10px rgba(22, 32, 51, 0.025); }
.comments-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 20px 20px 14px; }
.comments-title { margin: 0; font-size: 22px; letter-spacing: -0.035em; }
.comments-count { display: inline-flex; min-width: 27px; height: 27px; align-items: center; justify-content: center; padding: 0 8px; color: #527c12; background: var(--primary-soft); border-radius: 99px; font-size: 12px; font-weight: 750; }
.comment-form { display: grid; gap: 8px; padding: 0 20px 14px; border-bottom: 1px solid var(--border); }
.comment-form textarea { min-height: 88px; }
.comment-form-actions { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 8px; }
.comment-file-label { display: inline-flex; align-items: center; gap: 7px; min-height: 38px; padding: 0 10px; color: #526078; background: var(--surface-muted); border: 1px solid #cfd8e5; border-radius: var(--radius-sm); cursor: pointer; font-size: 13px; font-weight: 650; }
.comment-file-label:hover { color: var(--text); border-color: #aab8ca; }
.comment-file-input { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
.selected-files { display: grid; gap: 4px; color: var(--text-muted); font-size: 12px; }
.selected-file { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.comment-form-message { min-height: 0; margin: 0; color: var(--danger); font-size: 12px; }
.comment-form-message:empty { display: none; }
.comment-form-message.success { color: #527c12; }
.comments-message { margin: 0; padding: 18px 20px 0; }
.comments-list { display: grid; align-content: start; gap: 12px; min-width: 0; min-height: 0; padding: 16px 20px 20px; overflow-x: hidden; overflow-y: auto; }
.comment-card { min-width: 0; max-width: 100%; padding: 14px; background: var(--surface-muted); border: 1px solid #edf0f4; border-radius: var(--radius-sm); overflow: hidden; }
.comment-meta { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; min-width: 0; margin-bottom: 9px; }
.comment-author { color: var(--text); font-size: 13px; font-weight: 750; }
.comment-date { flex: 0 1 auto; min-width: 0; color: var(--text-muted); font-size: 11px; overflow-wrap: anywhere; text-align: right; }
.comment-text { margin: 0; color: #344055; font-size: 14px; line-height: 1.55; overflow-wrap: anywhere; word-break: break-word; }
.comment-attachments { display: grid; gap: 7px; margin-top: 12px; padding-top: 10px; border-top: 1px solid #e5eaf0; }
.comment-attachments-label { color: var(--text-muted); font-size: 11px; font-weight: 750; letter-spacing: 0.07em; text-transform: uppercase; }
.attachment-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px; min-width: 0; }
.attachment-name { min-width: 0; overflow-wrap: anywhere; color: #456f0a; font-size: 12px; font-weight: 650; line-height: 1.35; }
.attachment-actions { display: flex; flex: 0 0 auto; flex-wrap: wrap; justify-content: flex-end; gap: 4px; }
.attachment-action { padding: 2px 5px; color: var(--text-muted); background: transparent; border-radius: 5px; font-size: 11px; }
.attachment-action:hover:not(:disabled) { color: var(--primary-hover); background: var(--primary-soft); }
.attachment-action:disabled { cursor: wait; opacity: 0.55; }

@media (max-width: 1100px) {
  .customer-file-grid { grid-template-columns: minmax(0, 1fr) minmax(300px, 360px); gap: 20px; }
}
@media (max-width: 900px) {
  .app-layout { display: block; }
  .navigation-sidebar { position: static; height: auto; padding: 16px; border-right: 0; border-bottom: 1px solid var(--border); }
  .company-list { display: flex; gap: 8px; overflow-x: auto; }
  .company-item { min-width: 170px; }
  .region-list { padding-left: 0; }
  .customer-file-grid { grid-template-columns: 1fr; }
  .comments-panel { position: static; min-height: 520px; max-height: none; }
}
@media (max-width: 760px) {
  .topbar { padding: 14px 18px; }
  .user-copy, .logout-label { display: none; }
  main { padding: 22px 16px; }
  .filter-card, .filters { align-items: stretch; flex-direction: column; }
  .filter-search, .filter-region { flex-basis: auto; }
  .filter-actions { flex-direction: column; }
  .filter-card .button { width: 100%; }
  .customer-summary { padding: 18px; }
  .customer-summary-top, .pagination, .region-total-card, .section-heading-row { align-items: stretch; flex-direction: column; }
  .section-heading-row .button { width: 100%; }
  .pagination-buttons { display: grid; grid-template-columns: 1fr 1fr; }
  .pagination-buttons .button { width: 100%; }
  .comment-form-actions { align-items: center; justify-content: flex-end; }
  .comment-file-label, .comment-form-actions .button { width: auto; }
  .comments-header, .comment-form, .comments-message, .comments-list { padding-left: 16px; padding-right: 16px; }
  .comment-meta { flex-direction: column; gap: 3px; }
  .comment-date { text-align: left; }
}
