:root {
  --color-primary: #4f6bff;
  --color-primary-dark: #3450e0;
  --color-bg: #f5f6fa;
  --color-card: #ffffff;
  --color-border: #e5e7ef;
  --color-text: #1f2333;
  --color-text-muted: #767b91;
  --color-success: #1fa870;
  --color-danger: #e0503c;
  --color-warning: #d99a1f;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-card: 0 1px 3px rgba(31, 35, 51, 0.08);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
}

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

a {
  color: var(--color-primary);
}

ul, ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

h1, h2, h3, h4 {
  margin: 0;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
}

/* ---------- 頂部欄 ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 20px 14px;
  background: var(--color-card);
  border-bottom: 1px solid var(--color-border);
}

.topbar h1 {
  font-size: 19px;
  font-weight: 700;
}

.topbar-user {
  font-size: 13px;
  color: var(--color-text-muted);
  background: var(--color-bg);
  padding: 4px 10px;
  border-radius: 999px;
}

/* ---------- 主內容區 ---------- */

.page-content {
  flex: 1;
  padding: 16px 16px calc(96px + var(--safe-bottom));
}

.page {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* .page的display:flex跟瀏覽器內建[hidden]{display:none}屬於同層級的author規則，
   靠class選擇器會贏過屬性選擇器的預設優先權，所以要額外補這條規則，
   否則切換分頁時舊分頁不會真的消失，只是疊在新分頁後面。 */
.page[hidden] {
  display: none;
}

.loading, .empty-state, .error-state {
  text-align: center;
  color: var(--color-text-muted);
  padding: 32px 12px;
  font-size: 14px;
}

.error-state {
  color: var(--color-danger);
}

.empty-state-block {
  background: var(--color-card);
  border-radius: var(--radius);
  padding: 28px 20px;
  text-align: center;
  box-shadow: var(--shadow-card);
}

.empty-state-block p {
  margin: 0 0 12px;
}

/* ---------- 底部導覽列 ---------- */

.bottom-nav {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 560px;
  display: flex;
  background: var(--color-card);
  border-top: 1px solid var(--color-border);
  padding-bottom: var(--safe-bottom);
  z-index: 20;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px 10px;
  background: none;
  border: none;
  color: var(--color-text-muted);
}

.nav-item.active {
  color: var(--color-primary);
}

.nav-icon {
  font-size: 20px;
  line-height: 1;
}

.nav-label {
  font-size: 11px;
}

/* ---------- 右下角新增按鈕 ---------- */

.fab {
  position: fixed;
  right: max(20px, calc(50% - 280px + 20px));
  bottom: calc(74px + var(--safe-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--color-primary);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  box-shadow: 0 4px 12px rgba(79, 107, 255, 0.4);
  z-index: 15;
}

.fab:active {
  background: var(--color-primary-dark);
}

/* ---------- 卡片 / 清單通用 ---------- */

.item-list, .furniture-list, .simple-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item-card {
  background: var(--color-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 12px 14px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.item-card.item-done {
  opacity: 0.6;
}

.item-card.item-done .item-title {
  text-decoration: line-through;
}

.item-checkbox {
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid var(--color-border);
  background: #fff;
  color: var(--color-success);
  font-weight: 700;
  font-size: 14px;
  margin-top: 2px;
}

.item-card.item-done .item-checkbox {
  background: var(--color-success);
  border-color: var(--color-success);
  color: #fff;
}

.item-main {
  flex: 1;
  min-width: 0;
}

.item-title {
  font-weight: 600;
  font-size: 15px;
  word-break: break-word;
}

.item-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
  font-size: 13px;
  color: var(--color-text-muted);
  align-items: center;
}

.item-note, .item-note-inline {
  font-size: 13px;
  color: var(--color-text-muted);
  margin-top: 4px;
  word-break: break-word;
}

.item-footer {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 6px;
}

.tag {
  display: inline-block;
  background: var(--color-bg);
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12px;
  color: var(--color-text-muted);
}

.tag-selected {
  background: rgba(31, 168, 112, 0.12);
  color: var(--color-success);
}

/* ---------- 常買清單 chips ---------- */

.template-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  border: 1px solid var(--color-border);
  background: var(--color-card);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13px;
  color: var(--color-text);
  white-space: nowrap;
}

.chip-outline {
  color: var(--color-primary);
  border-color: var(--color-primary);
}

.list-section-title {
  font-size: 14px;
  color: var(--color-text-muted);
  margin-bottom: 8px;
  font-weight: 600;
}

/* ---------- 家具家電 ---------- */

.furniture-card {
  background: var(--color-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

.furniture-card summary {
  list-style: none;
  padding: 14px;
  cursor: pointer;
}

.furniture-card summary::-webkit-details-marker {
  display: none;
}

.furniture-summary-main {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.furniture-summary-meta {
  margin-top: 6px;
  font-size: 13px;
  color: var(--color-text-muted);
}

.status-badge {
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 999px;
  font-weight: 600;
}

.status-neutral {
  background: var(--color-bg);
  color: var(--color-text-muted);
}

.status-progress {
  background: rgba(217, 154, 31, 0.14);
  color: var(--color-warning);
}

.status-done {
  background: rgba(31, 168, 112, 0.14);
  color: var(--color-success);
}

.furniture-detail {
  padding: 0 14px 14px;
  border-top: 1px solid var(--color-border);
}

.furniture-detail h4 {
  font-size: 13px;
  color: var(--color-text-muted);
  margin: 14px 0 8px;
}

.candidate-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.candidate-card {
  background: var(--color-bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.candidate-card.candidate-selected {
  outline: 2px solid var(--color-success);
}

.candidate-main {
  display: flex;
  align-items: center;
  gap: 8px;
}

.candidate-actions {
  display: flex;
  gap: 14px;
  margin-top: 6px;
}

.furniture-detail-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.summary-bar {
  display: flex;
  justify-content: space-between;
  background: var(--color-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 14px;
  gap: 10px;
}

.summary-label {
  display: block;
  font-size: 12px;
  color: var(--color-text-muted);
}

.summary-value {
  display: block;
  font-size: 16px;
  font-weight: 700;
  margin-top: 2px;
}

/* ---------- 理財 ---------- */

.finance-summary-card {
  background: var(--color-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.finance-summary-row {
  display: flex;
  gap: 12px;
}

.finance-summary-row > div {
  flex: 1;
}

.finance-summary-row.main .summary-value {
  font-size: 24px;
}

.finance-summary-row.owner-breakdown {
  border-top: 1px dashed var(--color-border);
  padding-top: 10px;
}

.summary-value.positive {
  color: var(--color-success);
}

.summary-value.negative {
  color: var(--color-danger);
}

.finance-section {
  background: var(--color-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

.finance-section > summary {
  list-style: none;
  padding: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.finance-section > summary::-webkit-details-marker {
  display: none;
}

.btn-add-inline {
  border: none;
  background: none;
  color: var(--color-primary);
  font-size: 13px;
  font-weight: 600;
  padding: 4px 8px;
}

.simple-list {
  padding: 0 14px 14px;
  border-top: 1px solid var(--color-border);
}

.simple-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--color-border);
}

.simple-list li:last-child {
  border-bottom: none;
}

.simple-list .row-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.row-value {
  font-weight: 600;
  white-space: nowrap;
}

/* ---------- 房貸試算 ---------- */

#mortgage-calculator {
  padding: 0 14px 14px;
  border-top: 1px solid var(--color-border);
}

.mortgage-segment {
  background: var(--color-bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-top: 12px;
}

.mortgage-segment-header {
  display: flex;
  gap: 8px;
  align-items: center;
}

.mortgage-segment-header input {
  flex: 1;
}

.mortgage-segment-fields {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 8px;
}

.mortgage-segment-fields label {
  font-size: 12px;
  color: var(--color-text-muted);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#mortgage-add-segment {
  margin-top: 12px;
}

.mortgage-results {
  margin-top: 16px;
}

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

.mortgage-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  min-width: 480px;
}

.mortgage-table th, .mortgage-table td {
  padding: 8px 6px;
  text-align: right;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

.mortgage-table th:first-child, .mortgage-table td:first-child {
  text-align: left;
}

.mortgage-table tfoot td {
  font-weight: 700;
  border-bottom: none;
  border-top: 2px solid var(--color-border);
}

.hint-text {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 10px;
  line-height: 1.6;
}

/* ---------- 設定頁 ---------- */

.settings-card {
  background: var(--color-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.settings-card h3 {
  font-size: 14px;
  color: var(--color-text-muted);
}

/* ---------- 按鈕 / 表單 ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  border: none;
  text-decoration: none;
  cursor: pointer;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}

.btn-danger {
  background: rgba(224, 80, 60, 0.12);
  color: var(--color-danger);
}

.btn-outline {
  background: #fff;
  border: 1px solid var(--color-border);
  color: var(--color-text);
}

.btn-outline:disabled {
  color: var(--color-text-muted);
  cursor: not-allowed;
}

.btn-link {
  background: none;
  border: none;
  color: var(--color-primary);
  font-size: 13px;
  padding: 0;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-size: 15px;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--color-text-muted);
}

.form input, .form select, .form textarea {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 15px;
  color: var(--color-text);
  background: #fff;
  width: 100%;
}

.form textarea {
  min-height: 70px;
  resize: vertical;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
}

.checkbox-label {
  flex-direction: row !important;
  align-items: center;
  gap: 8px !important;
}

.checkbox-label input {
  width: auto;
}

/* ---------- Modal ---------- */

.modal-root {
  position: fixed;
  inset: 0;
  z-index: 100;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 22, 34, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal-card {
  background: #fff;
  width: 100%;
  max-width: 560px;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding-bottom: calc(20px + var(--safe-bottom));
  animation: modal-slide-up 0.18s ease-out;
}

@keyframes modal-slide-up {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 6px;
  position: sticky;
  top: 0;
  background: #fff;
}

.modal-header h2 {
  font-size: 17px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 18px;
  color: var(--color-text-muted);
  width: 32px;
  height: 32px;
}

.modal-body {
  padding: 10px 20px 4px;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + var(--safe-bottom));
  transform: translateX(-50%);
  background: rgba(31, 35, 51, 0.92);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 200;
  max-width: 90%;
  text-align: center;
}

.toast-success {
  background: rgba(31, 168, 112, 0.95);
}

@media (min-width: 600px) {
  .fab {
    right: calc(50% - 280px + 20px);
  }
}
