:root {
  --bg: #f2f5fb;
  --card: #ffffff;
  --text: #18243d;
  --muted: #6f7a91;
  --primary: #2d6dff;
  --danger: #d83a3a;
  --border: #dce3f3;
  --highlight: #ffe69c;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 12px 14px;
}

.brand-wrap { display: flex; align-items: center; gap: 10px; max-width: 920px; margin: 0 auto; }
.brand-wrap h1 { margin: 0; font-size: 1.12rem; }
.brand-icon { width: 25px; height: 25px; color: #2555cb; display: inline-flex; }

.app { max-width: 920px; margin: 0 auto; min-height: 100vh; padding: 12px 14px 94px; }
#init-message { margin: 0 0 12px; }
#init-message.error { color: var(--danger); }

.tab-content { display: none; }
.tab-content.active { display: block; }

.card-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 8px 16px rgba(28, 41, 70, 0.06);
}

.page-head h2 { margin: 0 0 6px; font-size: 1.35rem; }
.muted { color: var(--muted); font-size: .93rem; }

.search-wrap {
  margin-top: 12px;
  position: relative;
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 14px;
  border: 1px solid #cfdaef;
  box-shadow: 0 8px 20px rgba(37, 61, 116, 0.11);
}
.input-icon { padding-left: 12px; color: #7591ca; display: inline-flex; align-items: center; justify-content: center; }
.input-icon svg { width: 20px; height: 20px; stroke: currentColor; fill: none; }

input, textarea, select {
  width: 100%;
  border: 1px solid #c8d2ec;
  border-radius: 10px;
  padding: 10px;
  font-size: 1rem;
  background: #fff;
}
.search-wrap input { border: 0; box-shadow: none; padding: 12px 8px; }
.search-wrap input:focus { outline: none; }
.icon-btn { border: 0; background: transparent; color: #8692ab; width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center; padding: 0; cursor: pointer; }
.icon-btn svg { width: 20px; height: 20px; stroke: currentColor; fill: none; }


.select-wrap { position: relative; }
.select-wrap::after {
  content: '▾';
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #5f6f92;
  pointer-events: none;
  font-size: .9rem;
}
select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 32px;
}

label { font-size: .9rem; color: var(--muted); display: block; margin-bottom: 6px; }
textarea { min-height: 72px; resize: vertical; }

.row { display: grid; gap: 8px; }
.row.two { grid-template-columns: 1fr 1fr; }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
button {
  border: none;
  border-radius: 10px;
  padding: 9px 12px;
  cursor: pointer;
  background: #ecf1ff;
  color: #12357d;
  font-weight: 700;
}
button.primary { background: var(--primary); color: #fff; }
button.danger { background: #ffe9e9; color: var(--danger); }
button.outline { background: #fff; border: 1px solid var(--border); }

.manage-title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.manage-title-row > div h2 { margin-bottom: 4px; }
.manage-title-row > div .muted { margin: 0; }
.manage-head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.manage-actions { display: inline-flex; gap: 6px; }
.icon-action { background: rgba(255, 255, 255, 0.28); color: #fff; width: 40px; height: 40px; padding: 0; border-radius: 10px; border: 1px solid rgba(255, 255, 255, 0.35); display: inline-flex; align-items: center; justify-content: center; }
.icon-action svg { width: 20px; height: 20px; stroke: currentColor; fill: none; }
.icon-action.danger { background: rgba(126, 16, 16, 0.25); }

.credit-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  margin-bottom: 12px;
  box-shadow: 0 10px 20px rgba(28, 41, 70, 0.08);
  overflow: hidden;
}

.credit-card-header {
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.credit-card-header[data-color="藍色系"] { background: linear-gradient(135deg, #2a63dc, #3f8dff); }
.credit-card-header[data-color="綠色系"] { background: linear-gradient(135deg, #1f8d67, #30b575); }
.credit-card-header[data-color="紅色系"] { background: linear-gradient(135deg, #ba2d4f, #ea4b4b); }
.credit-card-header[data-color="紫色系"] { background: linear-gradient(135deg, #6848c9, #9f65e8); }
.credit-card-header[data-color="金黃色系"] { background: linear-gradient(135deg, #b9891f, #d7af45); }
.credit-card-header[data-color="黑色系"] { background: linear-gradient(135deg, #1f2430, #424956); }
.credit-card-header[data-color="銀灰色系"] { background: linear-gradient(135deg, #768296, #9ea9b8); }

.bank-name { font-size: .82rem; opacity: .92; }
.card-name { font-size: 1.08rem; font-weight: 800; margin-top: 2px; }

.credit-card-body { padding: 12px 14px; }
.card-section-title { font-size: .92rem; font-weight: 800; color: #495777; margin-bottom: 8px; }

.rules-list { border: 1px solid #e5eaf5; border-radius: 12px; overflow: hidden; }
.rule-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 10px; align-items: start; padding: 10px 12px; background: #fff; }
.rule-row + .rule-row { border-top: 1px solid #e8edf7; }
.rule-title { font-weight: 700; font-size: .95rem; }
.rule-main-block { min-width: 0; }
.rule-channel { margin-top: 4px; font-size: .83rem; color: #72809b; }
.rule-channel-label { margin-top: 6px; font-size: .78rem; color: #7c8aa5; text-transform: uppercase; letter-spacing: .04em; }
.rule-chip-list { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 7px; }
.chip { display: inline-flex; align-items: center; border: 1px solid #d6deef; background: #f2f6ff; color: #4d5d80; border-radius: 999px; padding: 3px 10px; font-size: .78rem; line-height: 1.4; }
.chip-control { background: #e4edff; border-color: #c4d2f3; color: #264f9e; font-weight: 700; cursor: pointer; }
.chip-control:hover { background: #d8e5ff; }
.chip-control:active { background: #ccdcff; }
.rule-metrics-badge {
  min-width: 132px;
  max-width: 148px;
  border-radius: 14px;
  border: 1px solid #e3e8f5;
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(6px);
  box-shadow: 0 4px 10px rgba(25, 46, 94, 0.08);
  padding: 8px 10px;
  text-align: right;
  justify-self: end;
}
.rate-text { font-size: 1.1rem; font-weight: 800; color: #cb2b2b; line-height: 1.2; }
.value-text { margin-top: 1px; font-size: .73rem; color: #667594; }
.weight-text { margin-top: 3px; font-size: .75rem; color: #9aa4b8; }
.formula-text { margin-top: 2px; font-size: .68rem; color: #7e8aa3; }

.rule-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
}
.info-label { font-size: .82rem; color: #75829b; }
.info-main { font-weight: 700; margin-top: 2px; }
.rule-subline { margin-top: 7px; font-size: .9rem; color: #4c5978; word-break: break-word; }
.rule-note {
  margin-top: 8px;
  display: inline-flex;
  align-items: flex-start;
  gap: 6px;
  font-size: .82rem;
  color: #5f6d8c;
}
.rule-note-icon { width: 20px; height: 20px; flex: 0 0 auto; margin-top: 1px; stroke: currentColor; fill: none; }
.rule-note a { color: var(--cathay-green, #00897b); text-decoration: underline; text-underline-offset: 2px; }
.rule-note a:hover { color: var(--cathay-blue, #1565c0); }

.rule-period { color: #8091a7; font-style: italic; }
.credit-card.expired { opacity: .55; }
.credit-card.expired .credit-card-header::after { content: '已過期'; position: absolute; right: 12px; top: 50%; transform: translateY(-50%); font-size: .75rem; color: #d83a3a; background: #fee; padding: 2px 8px; border-radius: 4px; }
.credit-card.expired .credit-card-header { position: relative; }

mark { background: var(--highlight); border-radius: 4px; padding: 0 2px; }

.bottom-tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  padding: 8px 10px;
}
.bottom-tabs button {
  width: 100%;
  min-height: 40px;
  background: #fff;
  color: #8c96aa;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}
.bottom-tabs button.active { color: var(--primary); }
.tab-icon {
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tab-icon svg { width: 24px; height: 24px; stroke: currentColor; fill: none; }

.editor {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: none;
  padding: 16px;
  overflow: auto;
}
.editor.show { display: block; }
.editor-panel {
  max-width: 900px;
  margin: 0 auto 60px;
  background: #fff;
  border-radius: 14px;
  padding: 14px;
}
.editor-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.editor-header-row h2 { margin: 0; }
.editor-close-x {
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #f7faff;
  box-shadow: 0 4px 10px rgba(25, 46, 94, 0.1);
  color: #5d6d91;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.editor-close-x svg { width: 20px; height: 20px; stroke: currentColor; fill: none; }
.reward-toggle {
  display: inline-flex;
  border: 1px solid #cfd9ef;
  border-radius: 12px;
  background: #f4f7ff;
  padding: 3px;
  gap: 4px;
}
.reward-option {
  background: transparent;
  color: #52658f;
  border-radius: 9px;
  border: 0;
  font-weight: 700;
  padding: 8px 10px;
}
.reward-option.active {
  background: #fff;
  color: #1f4eb2;
  box-shadow: 0 2px 6px rgba(25, 46, 94, 0.12);
}
.reward-field.hidden { display: none; }
.section-title { margin: 8px 0; font-size: 1rem; }

@media (max-width: 560px) {
  .row.two { grid-template-columns: 1fr; }
  .manage-title-row { align-items: flex-start; flex-direction: column; }
  .manage-head-actions { width: 100%; }
  .rule-metrics-badge { min-width: 116px; max-width: 132px; padding: 7px 8px; }
  .rate-text { font-size: 1rem; }
  .value-text,
  .weight-text { font-size: .69rem; }
  .reward-toggle { width: 100%; }
  .reward-option { flex: 1; }
}

@media (max-width: 390px) {
  .rule-layout,
  .rule-row {
    gap: 8px;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: start;
  }
  .rule-metrics-badge {
    min-width: 92px;
    max-width: 112px;
    padding: 6px 7px;
    border-radius: 12px;
    justify-self: end;
    text-align: right;
  }
  .rate-text { font-size: .94rem; }
  .rule-subline { font-size: .84rem; }
  .chip { padding: 3px 8px; font-size: .74rem; }
}

@media (min-width: 768px) {
  .bottom-tabs { left: 50%; transform: translateX(-50%); max-width: 920px; border: 1px solid var(--border); border-bottom: 0; border-radius: 12px 12px 0 0; }
}

.field-helper { margin-top: 6px; font-size: .8rem; color: #8a94aa; }

.row.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.readonly-rate {
  border: 1px dashed #cfd9ef;
  border-radius: 10px;
  background: #f8faff;
  padding: 10px 12px;
}
.readonly-rate-main {
  color: #1f4eb2;
  font-weight: 700;
  font-size: .92rem;
}
.field-helper.warn { color: #b15e00; min-height: 18px; }

/* ── Info tooltip ── */
.chip-with-info { display: inline-flex; align-items: center; gap: 0; }
.chip-info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  margin-left: 2px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: #8a9bc0;
  cursor: pointer;
  flex: 0 0 auto;
}
.chip-info-btn svg { width: 15px; height: 15px; stroke: currentColor; fill: none; }
.chip-info-btn:hover { color: var(--primary); }
.section-notice-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  padding: 0;
  border: none;
  background: transparent;
  color: #8a9bc0;
  font-size: .82rem;
  cursor: pointer;
}
.section-notice-btn svg { width: 16px; height: 16px; stroke: currentColor; fill: none; }
.section-notice-btn:hover { color: var(--primary); }

.info-tooltip-overlay {
  position: fixed;
  inset: 0;
  z-index: 900;
}
.info-tooltip {
  position: fixed;
  z-index: 910;
  max-width: min(420px, calc(100vw - 24px));
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: 0 8px 28px rgba(25, 46, 94, 0.18);
  font-size: .85rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  animation: tooltipIn .15s ease-out;
}
.info-tooltip-title {
  font-weight: 700;
  font-size: .92rem;
  margin-bottom: 6px;
  color: #1f4eb2;
}
@keyframes tooltipIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 560px) {
  .row.three { grid-template-columns: 1fr; }
}
