:root {
  color-scheme: light;
  --ink: #17202a;
  --muted: #5f6f78;
  --brand: #1f6f78;
  --brand-strong: #15545c;
  --accent: #2f80ed;
  --brand-dark: #26343d;
  --paper: #ffffff;
  --surface: #f8fbfc;
  --soft: #f3f6f8;
  --line: #d9e4e8;
  --warn: #fff4d8;
  --danger: #d64545;
  --success: #d7f0e6;
  --shadow-sm: 0 0.25rem 0.7rem rgba(17, 35, 45, 0.08);
  --shadow-md: 0 0.75rem 1.8rem rgba(17, 35, 45, 0.1);
  font-family: Arial, "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
}

* { box-sizing: border-box; min-width: 0; }
[hidden] { display: none !important; }
[inert] { pointer-events: none; user-select: none; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--soft);
  font-size: 0.95rem;
  line-height: 1.55;
  overflow-x: hidden;
  text-rendering: optimizeLegibility;
}

body.license-pending .app-shell,
body.license-pending main {
  display: none;
}

.license-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(circle at top left, rgba(47, 128, 237, 0.16), transparent 32rem),
    linear-gradient(135deg, #eef7f8 0%, #f8fbfc 50%, #eef2f5 100%);
}

.license-card {
  width: min(100%, 42rem);
  margin: 0;
  padding: 1.5rem;
  overflow: visible;
}

.license-card h1 {
  margin: 0 0 0.75rem;
  font-size: clamp(1.8rem, 5vw, 2.6rem);
}

.eyebrow {
  margin: 0 0 0.35rem;
  color: var(--brand);
  font-size: 0.82rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.license-form {
  margin-top: 1rem;
}

.license-device-list {
  display: grid;
  gap: 0.75rem;
}

.license-device-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.85rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--surface);
}

.license-device-card p {
  margin: 0.2rem 0 0;
}

.license-footnote {
  display: grid;
  gap: 0.65rem;
  margin-top: 1rem;
}

.secondary-button {
  color: var(--brand-dark);
  background: #fff;
  border-color: var(--line);
}
.secondary-button:hover { background: var(--soft); }

button, input, select, textarea { font: inherit; }
button, input, select, textarea, p, h1, h2, h3, td, th, span, strong, li { overflow-wrap: anywhere; }
button, .file-button {
  border: 1px solid transparent;
  border-radius: 0.38rem;
  padding: 0.48rem 0.85rem;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
  font-weight: 600;
  line-height: 1.35;
  transition: background 140ms ease, border-color 140ms ease, transform 140ms ease, box-shadow 140ms ease;
}
button:hover, .file-button:hover { background: #246fcf; }
button:active, .file-button:active { transform: translateY(1px); }
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.file-button:focus-visible {
  outline: 3px solid rgba(47, 128, 237, 0.25);
  outline-offset: 2px;
}
button.danger { background: var(--danger); }
button.danger:hover { background: #b93131; }

.app-navbar {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 1rem;
  min-height: 56px;
  padding: 0.45rem 0.9rem;
  background: var(--brand-dark);
  box-shadow: 0 0.4rem 1.2rem rgba(13, 23, 31, 0.16);
}

.navbar-brand {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.25rem 0;
  color: #fff;
  background: transparent;
  border-radius: 0;
  white-space: nowrap;
  font-size: 1rem;
  font-weight: 700;
}
.navbar-brand:hover { background: transparent; color: #fff; text-decoration: underline; }

.mobile-nav-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.45);
  white-space: nowrap;
}
.mobile-nav-toggle:hover { background: rgba(255, 255, 255, 0.18); }

.app-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.25rem;
}
.nav-item { position: relative; }
.nav-link {
  padding: 0.45rem 0.7rem;
  color: #fff;
  background: transparent;
  border-radius: 0.35rem;
  font-size: 0.92rem;
  white-space: nowrap;
}
.nav-link[data-menu-toggle]::after { content: "▾"; margin-left: 0.35rem; font-size: 0.75em; }
.nav-item.active > .nav-link,
.nav-item.open > .nav-link,
.nav-link:hover { background: rgba(255, 255, 255, 0.14); }
.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 60;
  display: none;
  min-width: 12rem;
  padding: 0.35rem 0;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 0.25rem;
  background: #fff;
  box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.12);
}
.dropdown-menu-end { left: auto; right: 0; }
.nav-item.open .dropdown-menu { display: grid; }
.dropdown-menu button {
  width: 100%;
  padding: 0.42rem 0.85rem;
  color: #212529;
  background: #fff;
  border-radius: 0;
  text-align: left;
  font-size: 0.92rem;
  font-weight: 500;
}
.dropdown-menu button:hover,
.dropdown-menu button.active { color: #16181b; background: #e9ecef; }
.install-button {
  color: #f8f9fa;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.65);
  white-space: nowrap;
  font-size: 0.85rem;
}
.install-button:hover { color: #212529; background: #f8f9fa; }
.install-button[disabled] {
  opacity: 0.72;
  cursor: default;
  transform: none;
}

.pwa-install-help {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(13, 23, 31, 0.52);
}
.pwa-install-card {
  position: relative;
  width: min(100%, 34rem);
  max-height: min(88vh, 44rem);
  margin: 0;
  overflow: auto;
}
.pwa-install-card h2 {
  margin: 0 2rem 0.65rem 0;
  font-size: clamp(1.45rem, 5vw, 2rem);
}
.pwa-install-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  border-radius: 999px;
  background: #eef2f5;
  color: var(--ink);
  font-size: 1.35rem;
  line-height: 1;
}
.pwa-install-close:hover { background: #dde7ee; }
.pwa-install-steps {
  display: grid;
  gap: 0.55rem;
  margin: 1rem 0;
  padding-left: 1.35rem;
  line-height: 1.65;
}
.pwa-install-note {
  margin-bottom: 0;
  padding: 0.75rem;
  border-radius: 0.45rem;
  background: #f1f8ff;
}

main {
  width: min(1280px, calc(100% - 2.5rem));
  margin: 1.35rem auto 3rem;
}

.status {
  margin: 0 0 1rem;
  padding: 0.75rem 1rem;
  border-left: 4px solid #2b9b75;
  border-radius: 0.45rem;
  background: #e9f7ef;
  color: #0f5132;
  overflow-wrap: anywhere;
}
.status[data-type="success"] { background: var(--success); color: #0f5132; }
.status[data-type="error"] { background: #f8d7da; color: #842029; }
.status[data-type="info"] { background: #e7f1ff; color: #084298; }

.panel {
  margin-top: 1rem;
  padding: 1rem;
  border: 1px solid rgba(217, 228, 232, 0.85);
  border-radius: 0.5rem;
  background: var(--paper);
  box-shadow: var(--shadow-sm);
  max-width: 100%;
  overflow-x: clip;
}
.home-hero {
  padding: 2.15rem;
  background: var(--surface);
  box-shadow: var(--shadow-md);
}
.home-hero h1, .report-panel h1 {
  margin: 0 0 1rem;
  font-size: 2.3rem;
  font-weight: 700;
}
.home-hero p { max-width: 48rem; margin: 0 0 1rem; font-size: 1.05rem; line-height: 1.7; }
.home-actions, .backup-actions, .report-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.home-actions button:nth-child(2), .backup-actions .file-button { background: #6c757d; }
.home-actions button:nth-child(n+3) { background: #0d6efd; }
.flow-help {
  margin: 0;
  padding: 0.75rem 0.9rem 0.75rem 1.35rem;
  border-left: 4px solid var(--brand);
  border-radius: 0.25rem;
  background: #eef6ff;
  color: #243b53;
  line-height: 1.65;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  gap: 0.9rem;
}
.metrics article {
  display: flex;
  min-height: 7rem;
  flex-direction: column;
  justify-content: space-between;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: #fff;
  box-shadow: var(--shadow-sm);
}
.metrics span { display: block; color: var(--muted); }
.metrics strong {
  display: block;
  max-width: 100%;
  margin-top: 0.55rem;
  color: #0b2b40;
  font-size: 1.58rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.12;
  letter-spacing: 0;
  overflow-wrap: normal;
  word-break: normal;
  white-space: nowrap;
}

.dashboard-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(240px, 1fr); gap: 1.25rem; align-items: start; }
.split { display: grid; grid-template-columns: minmax(280px, 360px) minmax(0, 1fr); gap: 1.25rem; align-items: start; }
.three-column { grid-template-columns: repeat(3, minmax(220px, 1fr)); }
#page-items .three-column {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1rem;
}
#page-items .form-card {
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--surface);
}
#page-items .compact {
  max-height: 64vh;
  overflow: auto;
  padding-right: 0.25rem;
}
#page-items .record-card.small {
  grid-template-columns: 1fr;
  overflow: visible;
  align-items: start;
}
#page-items .record-card.small .actions { justify-content: flex-start; }
#page-items .record-card.small h3 {
  color: #111;
  line-height: 1.45;
  word-break: break-word;
}
#page-installments .split {
  grid-template-columns: minmax(280px, 380px) minmax(0, 1fr);
}

h1, h2, h3 { line-height: 1.25; }
h2 { margin: 0.5rem 0 0.75rem; font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
.form-card, .record-list { display: grid; gap: 0.85rem; }
.compact { margin-top: 0.75rem; max-height: 52vh; overflow: auto; padding-right: 0.25rem; }
.inline-check { display: flex; align-items: center; gap: 0.55rem; }
.inline-check input { width: auto; }
.muted-card { opacity: 0.66; }
.nested { padding: 0.9rem; border-radius: 0.4rem; background: #f7fbff; border: 1px dashed #b6cad7; }
label { display: grid; gap: 0.35rem; font-weight: 600; }
input, select, textarea {
  width: 100%;
  max-width: 100%;
  border: 1px solid #ced4da;
  border-radius: 0.38rem;
  padding: 0.48rem 0.6rem;
  background: #fff;
  color: var(--ink);
}
input:focus,
select:focus,
textarea:focus {
  border-color: rgba(47, 128, 237, 0.72);
  box-shadow: 0 0 0 3px rgba(47, 128, 237, 0.12);
}
textarea { resize: vertical; }

.record-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 1rem;
  padding: 0.85rem;
  border: 1px solid #dbe7ea;
  border-radius: 0.5rem;
  background: #fff;
  max-width: 100%;
  overflow: hidden;
}
.record-card.warning { background: var(--warn); }
.record-card h3, .record-card p { margin: 0 0 0.35rem; overflow-wrap: anywhere; }
.record-card p, .muted, .empty { color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: 0.45rem; align-content: start; justify-content: flex-end; }
.actions button { padding: 0.3rem 0.6rem; font-size: 0.84rem; }
.form-card .muted { margin-top: -0.25rem; }

.recent-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 0.7rem; }
.recent-list li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 1rem; padding: 0.75rem; border-radius: 0.35rem; background: #eef8f7; }
.recent-list strong { overflow-wrap: anywhere; word-break: break-word; }

.picker { position: relative; display: grid; gap: 0.35rem; }
.autocomplete-menu {
  position: absolute;
  z-index: 40;
  top: calc(100% - 0.05rem);
  left: 0;
  right: 0;
  max-height: 18rem;
  overflow: auto;
  padding: 0.35rem;
  border: 1px solid #ced4da;
  border-radius: 0.35rem;
  background: #fff;
  box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.12);
}
.autocomplete-option {
  width: 100%;
  margin: 0;
  border-radius: 0.25rem;
  padding: 0.45rem 0.6rem;
  color: var(--ink);
  background: transparent;
  text-align: left;
  font-weight: 600;
}
.autocomplete-option:hover, .autocomplete-option:focus { background: #e9f2ff; color: #0d6efd; }
.autocomplete-empty { margin: 0; padding: 0.45rem 0.6rem; color: var(--muted); }

.bar-chart, .source-chart, .chart-fallback { display: grid; gap: 0.7rem; }
.bar-row, .source-row { display: grid; grid-template-columns: 5.7rem minmax(90px, 1fr) minmax(4rem, auto); gap: 0.75rem; align-items: center; }
.bar-label { color: var(--muted); font-weight: 700; overflow-wrap: anywhere; }
.bar-stack, .source-meter { display: flex; height: 1.05rem; overflow: hidden; border-radius: 999px; background: #e7eef2; }
.source-meter { height: 0.75rem; }
.bar-segment { display: block; min-width: 0; }
.bar-segment.sales, .legend-dot.sales { background: #22577a; }
.bar-segment.purchases, .legend-dot.purchases { background: #38a3a5; }
.bar-segment.consumptions, .legend-dot.consumptions { background: #57cc99; }
.legend-dot { display: inline-block; width: 0.75rem; height: 0.75rem; margin-right: 0.4rem; border-radius: 999px; vertical-align: middle; }

.report-panel > section { margin-top: 1rem; }
.report-tabs { margin: 1rem 0; }
.report-tabs button { background: #6c757d; }
.report-tabs button.active { background: var(--brand); }
.inline-report-form {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.inline-report-form label { min-width: min(18rem, 100%); }
.report-summary { display: grid; gap: 0.75rem; margin-bottom: 1rem; }
.report-table {
  width: 100%;
  max-width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  background: #fff;
  box-shadow: 0 0.25rem 0.75rem rgba(0, 0, 0, 0.05);
}
.report-table th {
  background: var(--brand-dark);
  color: #fff;
  text-align: left;
}
.report-table th,
.report-table td {
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--line);
  vertical-align: middle;
}
.report-table td:last-child,
.money {
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.25;
}
.report-table td:last-child { text-align: right; }
.report-totals {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
}
.report-totals strong {
  display: block;
  padding: 0.65rem 0.8rem;
  border-left: 4px solid var(--brand);
  border-radius: 0.25rem;
  background: #f1f8ff;
  overflow-wrap: anywhere;
}
.chart-card {
  height: 320px;
  margin: 0.75rem 0 1.25rem;
  padding: 0.75rem;
  border: 1px solid var(--line);
  border-radius: 0.35rem;
  background: #fff;
}
.chart-card canvas { width: 100% !important; height: 100% !important; }
.report-card { background: #fbfdff; }

.file-button input { display: none; }

@media (max-width: 900px) {
  .app-navbar {
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: center;
    gap: 0.55rem 0.75rem;
    padding: 0.55rem 0.75rem;
  }
  .navbar-brand { white-space: normal; }
  .mobile-nav-toggle {
    display: inline-flex;
    grid-column: 2;
    grid-row: 1;
    padding-inline: 0.7rem;
  }
  .app-nav {
    display: none;
    grid-column: 1 / -1;
    grid-row: 2;
    grid-template-columns: 1fr;
    justify-content: stretch;
    gap: 0.45rem;
    width: 100%;
    overflow-x: visible;
    overflow-y: visible;
    padding: 0.45rem 0 0.15rem;
  }
  .app-navbar.nav-open .app-nav { display: grid; }
  .install-button {
    grid-column: 3;
    grid-row: 1;
    align-self: center;
  }
  .nav-item {
    width: 100%;
    min-width: 0;
  }
  .nav-link {
    width: 100%;
    justify-content: space-between;
    background: rgba(255, 255, 255, 0.08);
    font-size: 0.9rem;
    text-align: left;
  }
  .dropdown-menu {
    position: static;
    width: 100%;
    min-width: 0;
    margin: 0.25rem 0 0.15rem;
    padding: 0.25rem;
    border-color: rgba(255, 255, 255, 0.18);
    border-radius: 0.4rem;
    background: rgba(255, 255, 255, 0.08);
    box-shadow: none;
  }
  .nav-item.open .dropdown-menu { display: grid; }
  .dropdown-menu button {
    color: #fff;
    background: transparent;
    border-radius: 0.3rem;
    padding: 0.58rem 0.9rem 0.58rem 1.1rem;
  }
  .dropdown-menu button:hover,
  .dropdown-menu button.active {
    color: #fff;
    background: rgba(255, 255, 255, 0.18);
  }
  #page-items .three-column, #page-installments .split { grid-template-columns: 1fr; }
  .split, .dashboard-grid, .three-column { grid-template-columns: 1fr; }
  .record-card { grid-template-columns: 1fr; }
  .actions { justify-content: flex-start; }
  .recent-list li { grid-template-columns: 1fr; }
  .bar-row, .source-row { grid-template-columns: 4.8rem minmax(60px, 1fr); }
  .bar-row strong, .source-row strong { grid-column: 2; }
}

@media (max-width: 540px) {
  main { width: min(100% - 1rem, 1120px); }
  .app-navbar { position: relative; }
  .mobile-nav-toggle,
  .install-button {
    padding-inline: 0.55rem;
    font-size: 0.84rem;
  }
  .home-hero { padding: 1.2rem; }
  .home-hero h1, .report-panel h1 { font-size: 1.9rem; }
  .home-actions button,
  .backup-actions button,
  .backup-actions .file-button { flex: 1 1 9rem; }
  .metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
  }
  .metrics article {
    min-height: 6.2rem;
    padding: 0.85rem;
  }
  .metrics strong { font-size: 1.32rem; }
}

@media (max-width: 360px) {
  .metrics { grid-template-columns: 1fr; }
}
