:root {
  color-scheme: light;
  --bg: #f3f6fb;
  --panel: #ffffff;
  --line: #d8dee9;
  --line-strong: #b8c2d2;
  --text: #1f2937;
  --muted: #667085;
  --brand: #0f766e;
  --brand-dark: #115e59;
  --accent: #2563eb;
  --sidebar: #172033;
  --sidebar-soft: #22304a;
  --danger: #b42318;
  --warn: #b54708;
  --shadow: 0 14px 34px rgba(31, 41, 55, 0.10);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Microsoft JhengHei", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

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

button {
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
  min-height: 38px;
}

button:hover {
  border-color: var(--brand);
}

button.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

button.primary:hover {
  background: var(--brand-dark);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button.danger {
  border-color: #f1b8b2;
  color: var(--danger);
}

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

textarea {
  min-height: 76px;
  resize: vertical;
}

.boot {
  padding: 32px;
  color: var(--muted);
}

.login-shell {
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.login-panel h1,
.page-title h1 {
  margin: 0;
  font-size: 24px;
}

.login-panel p,
.page-title p {
  margin: 6px 0 0;
  color: var(--muted);
}

.form-grid {
  display: grid;
  gap: 14px;
  margin-top: 22px;
}

.form-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
}

.error {
  color: var(--danger);
  background: #fff4f2;
  border: 1px solid #fecdca;
  border-radius: 6px;
  padding: 10px 12px;
}

.warn {
  color: var(--warn);
  background: #fffaeb;
  border: 1px solid #fedf89;
  border-radius: 6px;
  padding: 10px 12px;
}

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

.sidebar {
  background: var(--sidebar);
  color: #f8fafc;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.brand {
  display: grid;
  gap: 4px;
}

.brand strong {
  font-size: 18px;
}

.brand span,
.user-line {
  color: #cbd5e1;
  font-size: 13px;
}

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

.nav button {
  text-align: left;
  border-color: transparent;
  background: transparent;
  color: #e5e7eb;
}

.nav button.active,
.nav button:hover {
  background: var(--sidebar-soft);
  border-color: #344054;
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 10px;
}

.sidebar-footer button {
  border-color: #344054;
  background: #223044;
  color: #fff;
}

.main {
  min-width: 0;
  padding: 24px;
}

.page-title {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.toolbar input {
  min-width: 260px;
}

.page-actions {
  margin-bottom: 0;
}

.muted {
  color: var(--muted);
}

.detail-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-top: 14px;
  padding: 14px;
}

.inline-form {
  display: grid;
  grid-template-columns: 150px 120px minmax(180px, 1fr) minmax(180px, 1fr) auto;
  gap: 10px;
  margin-top: 12px;
}

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

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

.permission-grid {
  align-items: center;
  display: grid;
  gap: 8px 12px;
  grid-template-columns: minmax(120px, 1fr) repeat(4, 48px);
  margin-top: 10px;
  max-width: 520px;
}

.permission-check {
  display: flex;
  justify-content: center;
}

.schedule-grid {
  display: grid;
  grid-template-columns: minmax(320px, 0.85fr) minmax(520px, 1.4fr);
  gap: 14px;
}

.bank-transaction-grid {
  display: grid;
  grid-template-columns: minmax(520px, 1fr) minmax(520px, 1fr);
  gap: 14px;
}

.full-span {
  grid-column: 1 / -1;
}

.summary-line {
  color: var(--muted);
  margin-top: 10px;
}

.report-summary {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 12px;
  padding: 12px 14px;
}

.settings-section {
  margin-bottom: 18px;
}

.settings-add-row {
  grid-template-columns: minmax(220px, 1fr) auto;
  margin: 10px 0;
  max-width: 520px;
}

.tax-add-row {
  grid-template-columns: minmax(180px, 1fr) 120px auto;
}

.settings-table input,
.settings-table select {
  min-width: 90px;
  width: 100%;
}

.selectable-row {
  cursor: pointer;
}

.selected-row {
  background: #eef4ff;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  box-shadow: 0 8px 24px rgba(31, 41, 55, 0.04);
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.stat strong {
  display: block;
  margin-top: 6px;
  font-size: 24px;
}

.compact-stats {
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  margin-top: 14px;
}

.summary-section {
  margin-top: 16px;
}

.summary-section h3 {
  font-size: 16px;
  margin: 0 0 8px;
}

.table-wrap {
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(31, 41, 55, 0.04);
  -webkit-overflow-scrolling: touch;
}

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

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

th {
  background: #f8fafc;
  color: #475467;
  font-weight: 600;
}

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

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

.drawer {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.38);
  display: grid;
  place-items: center;
  padding: 24px;
}

.dialog {
  width: min(560px, 100%);
  background: #fff;
  border-radius: 8px;
  padding: 20px;
}

.wide-dialog {
  width: min(860px, 100%);
}

.project-summary-dialog {
  max-height: calc(100vh - 48px);
  overflow: auto;
  width: min(1180px, 100%);
}

.dialog h2 {
  margin: 0 0 14px;
  font-size: 20px;
}

.line-items-title h2 {
  margin: 0;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}

.two-col {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.inline-check {
  display: flex !important;
  align-items: center;
  gap: 8px;
}

.inline-check input {
  width: auto;
}

.full-label {
  display: grid;
  gap: 6px;
  margin-top: 14px;
  color: var(--muted);
}

.two-col .full-label {
  grid-column: 1 / -1;
}

.line-items {
  margin-top: 18px;
  display: grid;
  gap: 10px;
}

.line-items-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.invoice-item-row {
  display: grid;
  grid-template-columns: 1fr 110px 140px auto;
  gap: 8px;
}

.invoice-total {
  text-align: right;
  color: var(--muted);
}

.attachment-panel {
  margin-top: 18px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.attachment-upload {
  display: flex;
  align-items: center;
  gap: 8px;
}

.attachment-upload input {
  max-width: 260px;
}

.attachment-list {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.attachment-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
}

.attachment-row a {
  color: var(--brand);
  text-decoration: none;
  overflow-wrap: anywhere;
}

.small-empty {
  padding: 12px;
  text-align: left;
}

.scan-paths {
  color: var(--muted);
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

@media (max-width: 860px) {
  .shell {
    display: block;
  }

  .sidebar {
    border-bottom: 1px solid rgba(255, 255, 255, 0.10);
    box-shadow: 0 10px 26px rgba(15, 23, 42, 0.18);
    display: grid;
    gap: 10px;
    grid-template-areas:
      "brand footer"
      "user user"
      "nav nav";
    grid-template-columns: minmax(0, 1fr) auto;
    padding: 12px 14px 10px;
    position: sticky;
    top: 0;
    z-index: 10;
  }

  .brand {
    gap: 0;
    grid-area: brand;
  }

  .brand strong {
    font-size: 17px;
    line-height: 1.2;
  }

  .brand span {
    font-size: 12px;
  }

  .user-line {
    display: none;
  }

  .nav {
    display: flex;
    gap: 8px;
    grid-area: nav;
    margin: 0 -14px;
    overflow-x: auto;
    padding: 0 14px 2px;
    scrollbar-width: none;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
  }

  .nav::-webkit-scrollbar {
    display: none;
  }

  .nav button {
    background: rgba(255, 255, 255, 0.07);
    border-color: rgba(255, 255, 255, 0.10);
    border-radius: 999px;
    flex: 0 0 auto;
    font-size: 14px;
    min-height: 36px;
    padding: 7px 12px;
    scroll-snap-align: center;
    white-space: nowrap;
  }

  .nav button.active,
  .nav button:hover {
    background: var(--brand);
    border-color: #2dd4bf;
    color: #fff;
  }

  .sidebar-footer {
    align-self: start;
    display: flex;
    gap: 8px;
    grid-area: footer;
    margin: 0;
  }

  .sidebar-footer button {
    border-color: rgba(255, 255, 255, 0.12);
    font-size: 13px;
    min-height: 34px;
    padding: 6px 10px;
  }

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

  .two-col,
  .invoice-item-row,
  .attachment-row,
  .inline-form,
  .schedule-grid,
  .bank-transaction-grid {
    grid-template-columns: 1fr;
  }

  .full-span {
    grid-column: auto;
  }

  .attachment-upload {
    align-items: stretch;
    flex-direction: column;
  }

  .main {
    padding: 16px 12px 30px;
  }

  .page-title {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 14px;
  }

  .page-title h1 {
    font-size: 22px;
  }

  .page-title p {
    font-size: 13px;
  }

  .page-title button {
    width: 100%;
  }

  .toolbar {
    gap: 8px;
  }

  .toolbar input,
  .toolbar select {
    flex: 1 1 180px;
    min-width: 0;
  }

  .stat {
    padding: 12px;
  }

  .stat strong {
    font-size: 20px;
    overflow-wrap: anywhere;
  }

  table {
    min-width: 680px;
  }

  th,
  td {
    padding: 9px 10px;
  }

  .drawer {
    align-items: end;
    padding: 10px;
    place-items: end center;
  }

  .dialog,
  .wide-dialog,
  .project-summary-dialog {
    border-radius: 10px;
    max-height: calc(100svh - 20px);
    overflow: auto;
    padding: 16px;
    width: 100%;
  }

  .dialog-actions {
    flex-wrap: wrap;
  }

  .dialog-actions button {
    flex: 1 1 120px;
  }

  .line-items-title {
    align-items: flex-start;
    flex-direction: column;
  }

  .invoice-total {
    text-align: left;
  }
}

@media (max-width: 520px) {
  body {
    font-size: 14px;
  }

  button,
  input,
  select,
  textarea {
    min-height: 42px;
  }

  .login-shell {
    align-items: center;
    padding: 16px 12px;
  }

  .login-panel {
    border-radius: 10px;
    padding: 22px 18px;
  }

  .login-panel h1 {
    font-size: 26px;
  }

  .form-grid {
    gap: 13px;
    margin-top: 20px;
  }

  .sidebar {
    padding: 10px 12px 9px;
  }

  .nav {
    margin: 0 -12px;
    padding-inline: 12px;
  }

  .sidebar-footer button {
    min-height: 32px;
    padding: 5px 9px;
  }

  .main {
    padding: 14px 10px 28px;
  }

  .stats,
  .compact-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stat span {
    font-size: 12px;
  }

  .stat strong {
    font-size: 18px;
  }

  .table-wrap {
    border-radius: 7px;
  }

  .table-wrap table {
    min-width: 0;
  }

  .table-wrap thead {
    display: none;
  }

  .table-wrap tbody,
  .table-wrap tr,
  .table-wrap td {
    display: block;
    width: 100%;
  }

  .table-wrap tr {
    border-bottom: 1px solid var(--line);
    padding: 10px 12px;
  }

  .table-wrap tr:last-child {
    border-bottom: 0;
  }

  .table-wrap td {
    border-bottom: 0;
    padding: 5px 0;
  }

  .table-wrap td[data-label] {
    display: grid;
    gap: 8px;
    grid-template-columns: minmax(88px, 36%) minmax(0, 1fr);
  }

  .table-wrap td[data-label]::before {
    color: var(--muted);
    content: attr(data-label);
    font-size: 12px;
    font-weight: 600;
  }

  .table-wrap td:not([data-label]) {
    display: flex;
    justify-content: flex-end;
    padding-top: 8px;
  }

  .table-wrap td:not([data-label]) button {
    width: 100%;
  }

  .attachment-upload input {
    max-width: none;
  }
}
