:root {
  --bg: #eef2f5;
  --surface: #ffffff;
  --surface-soft: #f7f9fb;
  --ink: #17212b;
  --muted: #5f6f80;
  --line: #d7e0e8;
  --accent: #126c66;
  --accent-dark: #0d514d;
  --warn: #9a4b00;
  --danger: #b3261e;
  --radius: 8px;
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

.login-body {
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(420px, 100%);
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 18px 50px rgba(16, 24, 40, 0.12);
}

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

.login-brand {
  color: var(--ink);
  margin-bottom: 20px;
}

.login-message {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 36px;
  border: 0;
  border-radius: 6px;
  padding: 0 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  background: var(--accent-dark);
}

button.secondary {
  background: #e8eef2;
  color: var(--ink);
}

button.secondary:hover {
  background: #dce6ed;
}

button.danger {
  background: #fee8e6;
  color: var(--danger);
}

button.danger:hover {
  background: #ffd8d4;
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.68;
}

.app-shell {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: #16222d;
  color: #fff;
  padding: 22px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 28px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: #e7f3ef;
  color: var(--accent-dark);
  font-weight: 800;
}

.brand small {
  display: block;
  color: #aebbc6;
  margin-top: 2px;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav button {
  justify-content: flex-start;
  width: 100%;
  background: transparent;
  color: #dce6ed;
  text-align: left;
}

.nav button.active,
.nav button:hover {
  background: #223240;
  color: #fff;
}

.main {
  padding: 24px;
  overflow-x: hidden;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

h1,
h2 {
  margin: 0;
  line-height: 1.2;
  letter-spacing: 0;
}

h1 {
  font-size: 24px;
}

h2 {
  font-size: 17px;
}

p {
  margin: 6px 0 0;
  color: var(--muted);
}

.topbar-actions,
.inline-actions,
.form-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.user-badge {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 10px;
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.work-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 10px 28px rgba(16, 24, 40, 0.06);
}

.lock-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
  padding: 14px;
  border: 1px solid #e6c35c;
  border-radius: var(--radius);
  background: #fff7df;
}

.lock-banner strong,
.lock-banner span {
  display: block;
}

.lock-banner span {
  margin-top: 4px;
  color: #745900;
  font-size: 13px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 14px;
}

.payment-grid {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
}

.wide-field {
  grid-column: span 2;
}

label {
  display: grid;
  gap: 6px;
  min-width: 0;
}

label span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  color: var(--ink);
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid rgba(18, 108, 102, 0.18);
  border-color: var(--accent);
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin: 20px 0 10px;
}

.edit-state {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.first-title {
  margin-top: 0;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
  background: #fff;
}

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

th {
  background: var(--surface-soft);
  color: #415263;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0;
}

tr:last-child td {
  border-bottom: 0;
}

.items-table input,
.items-table select,
.items-table textarea {
  min-height: 34px;
}

.date-col {
  width: 136px;
}

.address-col {
  width: 36%;
}

.rate-col {
  width: 190px;
}

.hours-col {
  width: 120px;
}

.money-col {
  width: 120px;
  text-align: right;
}

.action-col {
  width: 56px;
}

.wide-actions {
  width: 260px;
}

.line-total {
  display: block;
  min-width: 84px;
  padding-top: 8px;
  text-align: right;
  font-weight: 700;
}

.summary-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 16px;
  margin-top: 16px;
  align-items: start;
}

.work-summary-grid {
  grid-template-columns: repeat(3, minmax(160px, 1fr));
}

.work-records-table {
  min-width: 920px;
}

.compact-actions {
  margin-top: 12px;
}

.filter-checkbox {
  align-self: end;
}

.section-title.compact {
  margin-top: 0;
}

.compact-table table,
.summary-table {
  min-width: 420px;
}

.notes-field {
  display: grid;
  margin-top: 16px;
}

.analytics-filters {
  display: grid;
  grid-template-columns: repeat(7, minmax(128px, 1fr));
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
  gap: 12px;
  margin: 16px 0;
}

.metric-card {
  min-height: 82px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.metric-card span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.metric-card strong {
  display: block;
  margin-top: 8px;
  font-size: 23px;
}

.metric-card.total {
  background: #17212b;
  border-color: #17212b;
  color: #fff;
}

.metric-card.total span {
  color: #cbd5df;
}

.split-tables {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.split-tables table {
  min-width: 840px;
}

.summary-panel {
  min-width: 0;
}

.admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.admin-panel {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
}

.wide-admin-panel {
  grid-column: 1 / -1;
}

.generated-password {
  min-height: 54px;
  display: grid;
  place-items: center;
  border: 1px dashed #9eb0bf;
  border-radius: var(--radius);
  background: #fff;
  font-size: 24px;
  font-weight: 800;
  letter-spacing: 0;
}

.access-list {
  display: grid;
  gap: 8px;
}

.access-list div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.access-list span,
.hint {
  color: var(--muted);
  font-size: 12px;
}

.mobile-app-panel {
  grid-template-columns: minmax(0, 1fr);
}

.mobile-app-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
}

.mobile-app-status {
  min-height: 42px;
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--muted);
  overflow-wrap: anywhere;
  font-size: 13px;
}

.invoice-footer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 16px;
  margin-top: 16px;
  align-items: start;
}

.totals-box {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
  overflow: hidden;
}

.totals-box div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
}

.totals-box div:last-child {
  border-bottom: 0;
}

.totals-box .grand {
  background: #17212b;
  color: #fff;
}

.form-actions {
  justify-content: flex-end;
  margin-top: 18px;
}

.alert {
  margin-bottom: 14px;
  padding: 10px 12px;
  border: 1px solid #badbcc;
  border-radius: var(--radius);
  background: #effaf3;
  color: #164b2e;
}

.alert.error {
  border-color: #f3b8b2;
  background: #fff0ee;
  color: var(--danger);
}

.message-dialog {
  width: min(420px, calc(100vw - 32px));
}

.message-dialog .dialog-message {
  margin: 0;
  color: #2d3743;
  line-height: 1.7;
  white-space: pre-line;
}

.message-dialog menu {
  margin-top: 18px;
}

.hidden {
  display: none;
}

.search {
  max-width: 320px;
}

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

.company-group-row td {
  background: #edf5f4;
  color: var(--accent-dark);
  font-weight: 800;
  letter-spacing: 0;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 0 9px;
  background: #e8eef2;
  color: #38495a;
  font-size: 12px;
  font-weight: 700;
}

.pill.paid {
  background: #e0f4e8;
  color: #17633b;
}

.pill.sent {
  background: #e6f0ff;
  color: #1e4f9a;
}

.pill.cancelled {
  background: #f6e6e5;
  color: #8f1e16;
}

.pill.locked,
.pill.error {
  background: #f6e6e5;
  color: #8f1e16;
}

.pill.info {
  background: #e6f0ff;
  color: #1e4f9a;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 38px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.checkbox-field input {
  width: 18px;
  min-height: 18px;
}

.checkbox-field span {
  color: var(--ink);
}

.audit-table {
  min-width: 980px;
}

.audit-table pre {
  max-width: 420px;
  max-height: 220px;
  overflow: auto;
  margin: 8px 0 0;
  padding: 10px;
  border-radius: 6px;
  background: #f6f8fa;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 12px;
}

.audit-table summary {
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 700;
}

dialog {
  width: min(720px, calc(100vw - 28px));
  max-height: min(90vh, 760px);
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  box-shadow: 0 20px 70px rgba(15, 23, 42, 0.25);
  overflow: auto;
}

dialog::backdrop {
  background: rgba(22, 34, 45, 0.42);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.dialog-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  padding: 18px;
}

.dialog-fields label.wide {
  grid-column: 1 / -1;
}

.icon-button {
  width: 36px;
  padding: 0;
  background: #e8eef2;
  color: var(--ink);
  font-size: 22px;
  line-height: 1;
}

menu {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin: 0;
  padding: 14px 18px 18px;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 3;
    padding: 12px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.14);
  }

  .brand {
    margin-bottom: 12px;
  }

  .nav {
    display: flex;
    overflow-x: auto;
    gap: 8px;
    padding-bottom: 2px;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
  }

  .nav button {
    flex: 0 0 auto;
    width: auto;
    min-width: 94px;
    text-align: center;
    justify-content: center;
    scroll-snap-align: start;
  }

  .form-grid,
  .invoice-footer,
  .summary-grid,
  .analytics-filters,
  .metric-grid,
  .admin-grid {
    grid-template-columns: 1fr 1fr;
  }

  .wide-field {
    grid-column: 1 / -1;
  }
}

@media (max-width: 720px) {
  html {
    min-height: 100%;
    background: #f6f8fa;
  }

  body {
    background: #f6f8fa;
    padding-bottom: calc(78px + env(safe-area-inset-bottom));
    overscroll-behavior-y: contain;
  }

  .login-body {
    padding-bottom: 24px;
  }

  button {
    min-height: 40px;
  }

  .brand {
    gap: 10px;
    margin-bottom: 10px;
  }

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

  .brand small {
    display: none;
  }

  .sidebar {
    top: 0;
    padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  }

  .sidebar .brand {
    margin-bottom: 0;
  }

  .nav {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 20;
    display: flex;
    gap: 8px;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 -12px 32px rgba(16, 24, 40, 0.12);
    overflow-x: auto;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
  }

  .nav button {
    min-width: 74px;
    height: 48px;
    min-height: 48px;
    padding: 0 10px;
    border: 1px solid transparent;
    border-radius: 10px;
    background: transparent;
    color: #415263;
    font-size: 12px;
    font-weight: 800;
    text-align: center;
    justify-content: center;
  }

  .nav button.active {
    border-color: #b7d8d4;
    background: #e7f3ef;
    color: var(--accent-dark);
  }

  .nav button:hover {
    background: #eef5f4;
    color: var(--accent-dark);
  }

  .main {
    padding: 12px 12px 0;
  }

  .topbar,
  .section-title,
  .lock-banner {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar {
    margin-bottom: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
  }

  h1 {
    font-size: 22px;
  }

  #pageSubtitle {
    font-size: 13px;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .topbar-actions .user-badge {
    grid-column: 1 / -1;
    justify-content: center;
  }

  .topbar-actions button,
  .inline-actions button,
  .form-actions button,
  .section-title > button {
    width: 100%;
  }

  .work-panel {
    padding: 14px;
    box-shadow: 0 6px 18px rgba(16, 24, 40, 0.06);
  }

  .payment-grid,
  .analytics-filters,
  .admin-panel {
    padding: 12px;
  }

  .form-grid,
  .invoice-footer,
  .summary-grid,
  .analytics-filters,
  .metric-grid,
  .admin-grid,
  .dialog-fields {
    grid-template-columns: 1fr;
  }

  .mobile-app-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .totals-box {
    width: 100%;
  }

  table {
    min-width: 680px;
  }

  th,
  td {
    padding: 8px;
  }

  .items-table {
    min-width: 680px;
  }

  .date-col {
    width: 130px;
  }

  .rate-col {
    width: 170px;
  }

  .hours-col,
  .money-col {
    width: 104px;
  }

  .row-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(76px, 1fr));
    width: 100%;
  }

  .row-actions button {
    width: 100%;
  }

  dialog {
    width: calc(100vw - 18px);
    max-height: calc(100vh - 18px);
  }

  .dialog-head {
    padding: 14px;
  }

  .dialog-fields {
    padding: 14px;
    gap: 12px;
  }

  menu {
    display: grid;
    grid-template-columns: 1fr;
    padding: 12px 14px 14px;
  }
}

@media (max-width: 520px) {
  .topbar-actions,
  .form-actions {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  .sidebar {
    padding: 10px;
  }

  .nav button {
    min-width: 72px;
    padding: 0 10px;
    font-size: 12px;
  }

  .search {
    max-width: none;
  }

  .access-list div {
    display: grid;
  }

  .access-list strong {
    overflow-wrap: anywhere;
  }

  .mobile-app-actions {
    grid-template-columns: 1fr;
  }
}
