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

:root {
  --primary: #0165ed;
  --primary-dark: #014fb8;
  --danger: #dc2626;
  --bg: #f8fafc;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --radius: 8px;
}

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

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* NAV */
nav {
  background: var(--primary);
  color: #fff;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 100;
}
nav.hidden { display: none; }
.nav-top { display: contents; }
.nav-toggle { display: none; }
.nav-brand { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.nav-links { display: flex; align-items: center; gap: 16px; }
.nav-links a { color: rgba(255,255,255,0.85); text-decoration: none; font-size: 0.9rem; white-space: nowrap; }
.nav-links a:hover, .nav-links a.active { color: #fff; }
.nav-links a.active { font-weight: 700; }
#nav-user { font-size: 0.85rem; color: rgba(255,255,255,0.7); }
#btn-logout {
  background: rgba(255,255,255,0.15);
  border: none; color: #fff;
  padding: 6px 14px; border-radius: var(--radius);
  cursor: pointer; font-size: 0.85rem;
}
#btn-logout:hover { background: rgba(255,255,255,0.25); }

/* PAGES */
#app { width: 100%; padding: 32px 24px; max-width: 1100px; margin: 0 auto; }
.page { display: none; }
.page.active { display: block; }
.hidden { display: none !important; }

/* AUTH */
.auth-box {
  max-width: 420px;
  margin: 80px auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px 32px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.06);
}
.auth-box h1 { font-size: 1.4rem; font-weight: 700; margin-bottom: 8px; }
.subtitle { color: var(--muted); font-size: 0.9rem; margin-bottom: 24px; }

.tabs { display: flex; border-bottom: 2px solid var(--border); margin-bottom: 24px; }
.tab { flex: 1; background: none; border: none; padding: 10px; cursor: pointer; color: var(--muted); font-size: 0.95rem; }
.tab.active { color: var(--primary); border-bottom: 2px solid var(--primary); margin-bottom: -2px; font-weight: 600; }

.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form input, .auth-form select {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  outline: none;
}
.auth-form input:focus { border-color: var(--primary); }
.auth-form button[type=submit] {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 11px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  margin-top: 4px;
}
.auth-form button[type=submit]:hover { background: var(--primary-dark); }
.sso-button {
  width: 100%;
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 700;
}
.sso-button:hover { background: var(--primary-dark); }

/* TABLES */
table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: var(--radius); overflow: hidden; margin-top: 16px; }
th { background: #f1f5f9; padding: 12px 14px; text-align: left; font-size: 0.85rem; color: var(--muted); }
td { padding: 12px 14px; border-top: 1px solid var(--border); font-size: 0.9rem; vertical-align: top; }
tr:hover td { background: #f8fafc; }
td img { width: 48px; height: 48px; object-fit: cover; border-radius: 4px; cursor: pointer; }
.file-link { color: var(--primary); font-weight: 600; text-decoration: none; }
.file-link:hover { text-decoration: underline; }
.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
}
.status-new {
  border: 1px solid #bfdbfe;
  background: #eff6ff;
  color: #1d4ed8;
}
.status-paid {
  border: 1px solid #99f6e4;
  background: #ecfdf5;
  color: #047857;
}

/* PAGE HEADER */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.page-header h2 { font-size: 1.3rem; margin-bottom: 0; }

/* BUTTONS */
button { cursor: pointer; }
#btn-add-cost {
  background: var(--primary); color: #fff;
  border: none; padding: 9px 18px;
  border-radius: var(--radius); font-size: 0.9rem; font-weight: 600;
}
#btn-add-cost:hover { background: var(--primary-dark); }

/* INLINE FORMS */
.inline-form {
  display: flex;
  gap: 10px;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.inline-form input {
  min-width: 0;
  flex: 1;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
}
.inline-form input:focus { outline: none; border-color: var(--primary); }
.inline-form button {
  border: none;
  border-radius: var(--radius);
  padding: 9px 18px;
  font-size: 0.9rem;
  font-weight: 600;
}
.inline-form button[type=submit] { background: var(--primary); color: #fff; }
.inline-form button[type=submit]:hover { background: var(--primary-dark); }
#btn-cancel-ai-tool { background: var(--border); color: var(--text); }

/* COST FORM */
#cost-form-wrapper {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; margin-bottom: 24px;
}
.form-row { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.form-row label { width: 140px; flex-shrink: 0; font-size: 0.9rem; color: var(--muted); padding-top: 9px; }
.form-row input, .form-row select, .form-row textarea {
  width: 100%;
  min-width: 0;
  flex: 1; padding: 9px 12px;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 0.9rem;
}
.form-row input:focus, .form-row select:focus, .form-row textarea:focus { outline: none; border-color: var(--primary); }
.form-actions { display: flex; gap: 10px; margin-top: 8px; padding-left: 152px; }
.form-actions button { padding: 9px 20px; border-radius: var(--radius); border: none; font-size: 0.9rem; font-weight: 600; }
.form-actions button[type=submit] { background: var(--primary); color: #fff; }
.form-actions button[type=submit]:hover { background: var(--primary-dark); }
#btn-cancel-cost { background: var(--border); color: var(--text); }
#banking-preview { display: block; max-width: min(200px, 100%); margin-top: 8px; border-radius: 4px; }

/* STATS */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-top: 16px; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px 24px; }
.stat-card .label { font-size: 0.85rem; color: var(--muted); margin-bottom: 8px; }
.stat-card .value { font-size: 1.6rem; font-weight: 700; color: var(--primary); }
.stats-section { margin-top: 32px; }
.stats-section h3 { font-size: 1rem; font-weight: 600; margin-bottom: 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.report-filters {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) auto auto;
  gap: 12px;
  align-items: end;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
#team-filter-form {
  grid-template-columns: minmax(180px, 240px) auto auto minmax(0, 1fr);
}
.report-filters label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}
.report-filters select,
.report-filters input {
  width: 100%;
  min-width: 0;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 400;
}
.report-filters select:focus,
.report-filters input:focus { outline: none; border-color: var(--primary); }
.report-filters button {
  min-height: 38px;
  border: none;
  border-radius: var(--radius);
  padding: 9px 16px;
  font-size: 0.9rem;
  font-weight: 600;
}
.report-filters button[type=submit] { background: var(--primary); color: #fff; }
#btn-reset-stats { background: var(--border); color: var(--text); }
.monthly-report-section { margin-top: 32px; }
.monthly-report-section h3 { font-size: 1rem; font-weight: 600; margin-bottom: 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.chart-section {
  margin-top: 32px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.chart-header {
  margin-bottom: 14px;
}
.chart-header h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.chart-header p {
  color: var(--muted);
  font-size: 0.88rem;
}
#stats-12-month-chart {
  display: block;
  width: 100%;
  max-width: 100%;
}

/* ACTIONS in table */
.btn-edit, .btn-edit-ai-tool { background: none; border: 1px solid var(--border); padding: 4px 10px; border-radius: 4px; font-size: 0.8rem; margin-right: 4px; }
.btn-delete, .btn-delete-ai-tool, .btn-delete-team { background: none; border: 1px solid #fca5a5; color: var(--danger); padding: 4px 10px; border-radius: 4px; font-size: 0.8rem; }
.btn-edit:hover, .btn-edit-ai-tool:hover { border-color: var(--primary); color: var(--primary); }
.btn-delete:hover, .btn-delete-ai-tool:hover, .btn-delete-team:hover { background: #fef2f2; }
.locked-note {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  margin-right: 4px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}
.payment-upload {
  display: grid;
  grid-template-columns: minmax(140px, 1fr) auto;
  gap: 8px;
  align-items: center;
  min-width: 250px;
}
.payment-upload input {
  min-width: 0;
  max-width: 100%;
  font-size: 0.8rem;
}
.payment-upload button {
  min-height: 32px;
  border: 1px solid var(--primary);
  border-radius: 4px;
  background: var(--primary);
  color: #fff;
  padding: 5px 10px;
  font-size: 0.8rem;
  font-weight: 700;
}
.payment-upload button:disabled {
  cursor: wait;
  opacity: 0.7;
}

/* ERRORS */
.form-error { color: var(--danger); font-size: 0.85rem; margin-top: 6px; min-height: 20px; }

/* UTILITY */
h2 { font-size: 1.3rem; font-weight: 700; margin-bottom: 16px; }

@media (max-width: 760px) {
  nav {
    height: auto;
    padding: 12px;
    align-items: flex-start;
    gap: 0;
    flex-direction: column;
  }

  .nav-top {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .nav-toggle {
    width: 40px;
    height: 40px;
    flex: 0 0 40px;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
    border: 0;
    border-radius: var(--radius);
    background: rgba(255,255,255,0.16);
    color: #fff;
  }

  .nav-toggle span {
    width: 18px;
    height: 2px;
    border-radius: 999px;
    background: currentColor;
    transition: transform 160ms ease, opacity 160ms ease;
  }

  nav.open .nav-toggle span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
  }

  nav.open .nav-toggle span:nth-child(2) {
    opacity: 0;
  }

  nav.open .nav-toggle span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
  }

  .nav-brand {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .nav-links {
    width: 100%;
    display: none;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 12px;
  }

  nav.open .nav-links {
    display: grid;
  }

  .nav-links a,
  #btn-logout {
    min-height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
    background: rgba(255,255,255,0.12);
    padding: 8px 10px;
    text-align: center;
  }

  .nav-links a.active {
    background: rgba(255,255,255,0.24);
  }

  #nav-user {
    grid-column: 1 / -1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: rgba(255,255,255,0.9);
  }

  #btn-logout {
    width: 100%;
  }

  #app { padding: 20px 12px 28px; }
  .auth-box { margin: 32px auto; padding: 28px 20px; }
  .page-header { align-items: stretch; flex-direction: column; gap: 10px; }
  #btn-add-cost { width: 100%; min-height: 42px; }
  .inline-form { align-items: stretch; flex-direction: column; }
  .inline-form button { min-height: 42px; }
  .report-filters {
    grid-template-columns: 1fr;
  }

  #team-filter-form {
    grid-template-columns: 1fr;
  }

  .report-filters button {
    width: 100%;
    min-height: 42px;
  }

  #cost-form-wrapper { padding: 16px; }
  .form-row { flex-direction: column; gap: 6px; }
  .form-row label { width: auto; padding-top: 0; }
  .form-actions { padding-left: 0; }
  .form-actions button { flex: 1; min-height: 42px; }

  table,
  thead,
  tbody,
  tr,
  th,
  td {
    display: block;
  }

  table {
    background: transparent;
    border-radius: 0;
    overflow: visible;
    margin-top: 12px;
  }

  thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

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

  tr {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
  }

  tr:hover td { background: transparent; }

  td {
    min-height: 42px;
    display: grid;
    grid-template-columns: minmax(96px, 36%) minmax(0, 1fr);
    gap: 12px;
    align-items: start;
    border-top: 1px solid var(--border);
    padding: 10px 12px;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  td:first-child { border-top: 0; }

  td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }

  .table-actions {
    display: flex;
    justify-content: stretch;
    gap: 8px;
  }

  .table-actions::before {
    display: none;
  }

  .btn-edit,
  .btn-edit-ai-tool,
  .btn-delete,
  .btn-delete-ai-tool,
  .btn-delete-team {
    flex: 1;
    min-height: 38px;
    margin-right: 0;
  }

  .locked-note {
    flex: 1;
    justify-content: center;
    min-height: 38px;
    margin-right: 0;
  }

  .payment-upload {
    min-width: 0;
    grid-template-columns: 1fr;
  }

  .payment-upload button {
    min-height: 38px;
  }

  .stats-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .stat-card {
    padding: 16px;
  }

  .stat-card .value {
    font-size: 1.35rem;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 420px) {
  .nav-links {
    grid-template-columns: 1fr;
  }

  td {
    grid-template-columns: 1fr;
    gap: 4px;
  }
}
