:root {
  color-scheme: light;
  --bg: #f4efe7;
  --surface: #fffaf2;
  --accent: #8a6a3f;
  --accent-strong: #1f3d2b;
  --text: #1f1c17;
  --muted: #6a5f51;
  --border: #e2d7c9;
  --shadow: 0 16px 40px rgba(26, 20, 12, 0.12);
  --font-display: "Palatino Linotype", "Book Antiqua", Palatino, serif;
  --font-body: "Optima", "Candara", "Segoe UI", sans-serif;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
}

* {
  box-sizing: border-box;
}

a {
  color: inherit;
}

body {
  margin: 0;
  font-family: var(--font-body);
  background: linear-gradient(140deg, #f5efe5 0%, #ebe0d2 50%, #f8f2ea 100%);
  background-attachment: fixed;
  color: var(--text);
  overflow-x: hidden;
}

.app-shell {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

.sidebar {
  padding: 32px 20px;
  background: linear-gradient(180deg, #1f3d2b 0%, #223326 40%, #1b2a20 100%);
  color: #f6efe2;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: var(--font-display);
}

.brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: rgba(246, 239, 226, 0.15);
  display: grid;
  place-items: center;
  font-weight: 700;
  letter-spacing: 1px;
}

.brand-name {
  font-size: 1.25rem;
  font-weight: 700;
}

.brand-subtitle {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  opacity: 0.7;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.nav-link {
  color: inherit;
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 10px;
  background: transparent;
  transition: background 0.2s ease, transform 0.2s ease;
}

.nav-link .tab-icon {
  display: none;
}

.nav-link:hover,
.nav-link.is-active {
  background: rgba(246, 239, 226, 0.15);
  transform: translateX(4px);
}

.sidebar-card {
  background: rgba(246, 239, 226, 0.08);
  border-radius: 14px;
  padding: 16px;
  line-height: 1.4;
  animation: floatIn 0.8s ease both;
}

.sidebar-title {
  font-family: var(--font-display);
  margin: 0 0 8px;
}

.sidebar-note {
  margin: 0;
  opacity: 0.8;
}

.content {
  position: relative;
  padding: 32px 36px 60px;
}

.content::before {
  content: "";
  position: absolute;
  top: -120px;
  right: -80px;
  width: 320px;
  height: 320px;
  background: radial-gradient(circle, rgba(138, 106, 63, 0.18), transparent 70%);
  pointer-events: none;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 28px;
  animation: rise 0.6s ease both;
}

.user-bar {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-bar .status {
  margin: 0;
}

.topbar h1 {
  font-family: var(--font-display);
  font-size: 2.4rem;
  margin: 0 0 6px;
}

.topbar p {
  margin: 0;
  color: var(--muted);
}

.topbar-meta {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 18px;
  min-width: 180px;
  box-shadow: var(--shadow);
}

.filter-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(90px, 1fr);
  gap: 8px;
  margin-top: 8px;
}

.filter-row input,
.filter-row select {
  margin: 0;
}

.meta-label {
  display: block;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}

.meta-value {
  font-family: var(--font-display);
  font-size: 1.2rem;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 18px;
  margin-bottom: 28px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 18px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 6px;
  animation: rise 0.6s ease both;
}

.kpi-card--compact {
  padding: 10px 12px;
  gap: 4px;
  max-width: 220px;
}

.kpi-card--compact .kpi-label {
  font-size: 0.78rem;
}

.kpi-card--compact .kpi-value {
  font-size: 1.25rem;
}

.kpi-card:nth-child(2) {
  animation-delay: 0.05s;
}

.kpi-card:nth-child(3) {
  animation-delay: 0.1s;
}

.kpi-card:nth-child(4) {
  animation-delay: 0.15s;
}

.kpi-label {
  font-size: 0.85rem;
  color: var(--muted);
}

.kpi-value {
  font-family: var(--font-display);
  font-size: 1.6rem;
  color: var(--accent-strong);
}

.kpi-danger {
  color: #b03030;
}

.kpi-warning {
  color: #b8860b;
}

.total-negative {
  color: #b03030;
}

.total-positive {
  color: #2e7d32;
}

.cxc-value {
  color: #b03030;
}

.cxp-value {
  color: #b8860b;
}

.kpi-note {
  font-size: 0.75rem;
  color: var(--muted);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 18px;
  margin-bottom: 28px;
}

.split-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 18px;
  margin-bottom: 28px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  box-shadow: var(--shadow);
  animation: rise 0.6s ease both;
}

.table-section {
  margin-top: 22px;
}

.form-grid .table-section,
.split-grid .table-section {
  margin-top: 0;
}

.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.collapse-toggle {
  white-space: nowrap;
}

.form-grid .card:nth-child(2) {
  animation-delay: 0.05s;
}

.form-grid .card:nth-child(3) {
  animation-delay: 0.1s;
}

.form-grid .card:nth-child(4) {
  animation-delay: 0.15s;
}

.form-grid .card:nth-child(5) {
  animation-delay: 0.2s;
}

.card-header h2 {
  font-family: var(--font-display);
  margin: 0 0 6px;
}

.card-header p {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

.card.is-collapsed .table-wrapper {
  display: none;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.92rem;
  color: var(--muted);
  margin-bottom: 12px;
}

.field-error {
  min-height: 16px;
  font-size: 0.8rem;
  color: #8b2b2b;
}

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

input,
select {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 0.95rem;
  background: #fffdf9;
}

textarea {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 0.95rem;
  background: #fffdf9;
  font-family: var(--font-body);
}

input:focus,
select:focus {
  outline: 2px solid rgba(138, 106, 63, 0.25);
  border-color: var(--accent);
}

textarea:focus {
  outline: 2px solid rgba(138, 106, 63, 0.25);
  border-color: var(--accent);
}

button {
  background: var(--accent-strong);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

button.secondary {
  background: transparent;
  color: var(--accent-strong);
  border: 1px solid var(--accent-strong);
  margin-top: 10px;
}

.link-button {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid var(--accent-strong);
  color: var(--accent-strong);
  background: transparent;
  font-weight: 600;
  font-size: 0.82rem;
}

button.small {
  padding: 6px 10px;
  font-size: 0.82rem;
}

button.danger {
  background: #8b2b2b;
  color: #fff;
  border: none;
}

.table-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

.attachment-list {
  display: grid;
  gap: 6px;
  margin: 6px 0 0;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  flex-wrap: wrap;
}

.attachment-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-top: 8px;
}

.attachment-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  background: #fffdf9;
  display: grid;
  gap: 8px;
}

.attachment-thumb {
  width: 100%;
  height: 120px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.attachment-file {
  width: 100%;
  height: 120px;
  border-radius: 10px;
  border: 1px dashed var(--border);
  display: grid;
  place-items: center;
  font-weight: 600;
  color: var(--muted);
  background: #f7f2e9;
}

.attachment-meta {
  display: grid;
  gap: 4px;
  font-size: 0.85rem;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}

.detail-label {
  margin: 0 0 4px;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}

.detail-value {
  margin: 0;
  font-weight: 600;
}

.flag-danger {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: #b03030;
  box-shadow: 0 0 0 1px rgba(176, 48, 48, 0.35);
}

.flag-success {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: #2e7d32;
  box-shadow: 0 0 0 1px rgba(46, 125, 50, 0.35);
}

.table-empty {
  text-align: center;
  color: var(--muted);
  padding: 18px 0;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 18, 15, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 60;
}

.modal.is-open {
  display: flex;
}

.modal-card {
  background: var(--surface);
  border-radius: 18px;
  padding: 20px;
  width: min(520px, 96vw);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  animation: rise 0.2s ease both;
}

.modal-doc {
  width: min(780px, 96vw);
}

.preview-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.preview-file-button {
  border: 1px solid var(--accent-strong);
  color: var(--accent-strong);
  background: transparent;
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 0.82rem;
  cursor: pointer;
}

.preview-file-button.is-active {
  background: var(--accent-strong);
  color: #fff;
}

.preview-frame {
  width: 100%;
  aspect-ratio: 8.5 / 11;
  background: #fffdf9;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  display: grid;
  place-items: center;
}

.preview-frame iframe,
.preview-frame img {
  width: 100%;
  height: 100%;
  border: 0;
  object-fit: contain;
  background: #fff;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.auth-actions {
  display: grid;
  gap: 10px;
}

.auth-actions .secondary {
  display: none;
}

#login-email-display {
  margin-top: 6px;
  font-size: 0.85rem;
  color: var(--muted);
}

button:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(31, 61, 43, 0.18);
}

.status {
  margin-top: 10px;
  font-size: 0.9rem;
  color: var(--muted);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  border: 1px solid var(--border);
  color: var(--muted);
  background: #fffdf7;
}

.status-pill.status-ok {
  color: #2e7d32;
  border-color: rgba(46, 125, 50, 0.4);
  background: rgba(46, 125, 50, 0.08);
}

.status-pill.status-debt {
  color: #b03030;
  border-color: rgba(176, 48, 48, 0.4);
  background: rgba(176, 48, 48, 0.08);
}

.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.auth-shell {
  width: min(440px, 92vw);
  padding: 32px 20px;
  flex: 1;
  display: grid;
  place-items: center;
}

.auth-footer {
  width: min(440px, 92vw);
  margin-bottom: 32px;
}

.auth-card {
  display: grid;
  gap: 14px;
}

.auth-brand {
  justify-content: center;
}

.output {
  background: #f7f2e9;
  padding: 12px;
  border-radius: 12px;
  min-height: 80px;
  border: 1px dashed var(--border);
  white-space: pre-wrap;
}

.content-footer {
  margin-top: 24px;
}

.table-wrapper {
  overflow-x: auto;
}

.is-hidden {
  display: none;
}

.suppliers-readonly th:last-child,
.suppliers-readonly td:last-child {
  display: none;
}


.agreements-readonly th:nth-child(5),
.agreements-readonly td:nth-child(5) {
  display: none;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th,
td {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}

th {
  font-family: var(--font-display);
  font-weight: 600;
  color: var(--accent-strong);
}

.table-empty {
  text-align: center;
  color: var(--muted);
  padding: 18px 0;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes floatIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 720px) {
  html,
  body {
    max-width: 100%;
    overflow-x: hidden;
  }

  .app-shell {
    grid-template-columns: 1fr;
    width: 100%;
  }

  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    height: auto;
    width: 100%;
    transform: none;
    z-index: 40;
    padding: 8px 12px calc(8px + var(--safe-bottom));
    border-top: 1px solid rgba(246, 239, 226, 0.2);
    box-shadow: 0 -12px 30px rgba(19, 14, 10, 0.35);
  }

  .brand,
  .sidebar-card {
    display: none;
  }

  .nav {
    flex-direction: row;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .nav-link {
    flex: 1 0 auto;
    text-align: center;
    font-size: 0.8rem;
    padding: 8px 10px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.08);
    white-space: nowrap;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    letter-spacing: 0.2px;
  }

  .nav-label {
    max-width: 7ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: block;
  }

  .nav-link:hover,
  .nav-link.is-active {
    transform: none;
    background: rgba(255, 255, 255, 0.18);
  }


  .nav-link .tab-icon {
    width: 16px;
    height: 16px;
    display: block;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
  }

  .content {
    padding: 16px 16px calc(90px + var(--safe-bottom));
    width: 100%;
    max-width: 100%;
  }

  .topbar {
    flex-direction: column;
    align-items: flex-start;
    position: relative;
    top: auto;
    z-index: 1;
    background: transparent;
    backdrop-filter: none;
    padding: 0;
    border-radius: 0;
    box-shadow: none;
  }

  body {
    font-family: "Avenir Next", "Helvetica Neue", "Optima", "Candara", sans-serif;
    letter-spacing: 0.2px;
  }

  .topbar-meta {
    width: 100%;
  }

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

  .kpi-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
  }

  .kpi-card {
    padding: 10px;
    border-radius: 12px;
    gap: 4px;
  }

  .kpi-label {
    font-size: 0.7rem;
  }

  .kpi-value {
    font-size: 1.1rem;
  }

  .form-grid,
  .split-grid {
    grid-template-columns: 1fr;
  }

  .card {
    padding: 16px;
    border-radius: 20px;
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .collapse-toggle {
    align-self: flex-start;
  }

  .table-wrapper {
    overflow-x: hidden;
  }

  th,
  td {
    padding: 8px 6px;
    font-size: 0.85rem;
  }

  table {
    border-collapse: separate;
    border-spacing: 0 10px;
  }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 10px;
  }

  tr {
    display: grid;
    gap: 6px;
    background: #fffdf7;
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 12px;
    box-shadow: 0 10px 20px rgba(26, 20, 12, 0.08);
  }

  td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    border-bottom: none;
    padding: 4px 0;
    gap: 12px;
  }

  td::before {
    content: attr(data-label);
    text-transform: uppercase;
    letter-spacing: 0.8px;
    font-size: 0.7rem;
    color: var(--muted);
  }


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

  .modal-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .preview-frame {
    min-height: 60vh;
  }

  .attachment-grid {
    grid-template-columns: 1fr 1fr;
  }

  .mobile-nav-toggle {
    display: none;
  }
}

@media (max-width: 520px) {
  .brand {
    gap: 8px;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
  }

  .topbar h1 {
    font-size: 2rem;
  }

  .attachment-grid {
    grid-template-columns: 1fr;
  }

  button,
  input,
  select {
    width: 100%;
  }

  .table-actions {
    flex-direction: column;
    align-items: flex-start;
  }
}

.mobile-nav-toggle {
  display: none;
  background: transparent;
  color: var(--accent-strong);
  border: 1px solid var(--accent-strong);
  margin-top: 0;
}

.user-bar .mobile-nav-toggle {
  margin-right: 6px;
}
