:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1a2332;
  --muted: #5b6b7c;
  --border: #d8e0e8;
  --brand: #0f6b4c;
  --brand-hover: #0c563d;
  --shadow: 0 8px 24px rgba(16, 36, 54, 0.08);
  --radius: 14px;
  --font: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: linear-gradient(180deg, #eef5f1 0%, var(--bg) 220px);
  min-height: 100vh;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

.wrap {
  width: min(960px, calc(100% - 32px));
  margin: 0 auto;
}
/* Fee calculator needs a wider canvas for side-by-side layout */
body:has(.fee-page) .wrap {
  width: min(1180px, calc(100% - 32px));
}

.site-header {
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,0.85);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.header-inner {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 16px 0;
}
.brand {
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--text);
  text-decoration: none;
}
.brand-sub { color: var(--muted); font-size: 0.9rem; }

.main { padding: 28px 0 64px; }

.site-footer {
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
  padding: 20px 0 32px;
}

.hero h1 { margin: 0 0 8px; font-size: 1.8rem; }
.hero p { margin: 0; color: var(--muted); line-height: 1.5; }
.hero-actions { margin-top: 16px; }

.tool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
  margin-top: 28px;
}

.tool-card {
  position: relative;
  display: block;
  overflow: hidden;
  isolation: isolate;
  min-height: 148px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 18px 16px;
  color: inherit;
  text-decoration: none;
  cursor: grab;
  user-select: none;
  transition: transform .12s ease, box-shadow .12s ease, border-color .12s ease;
}
.tool-card:hover {
  border-color: #b7d0c4;
  box-shadow: 0 10px 28px rgba(16, 36, 54, 0.12);
  text-decoration: none;
}
.tool-card.dragging {
  opacity: 0.55;
  cursor: grabbing;
}
.tool-card.drag-over {
  outline: 2px dashed var(--brand);
  outline-offset: 2px;
}
.tool-card-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-repeat: no-repeat;
  background-position: right -8px bottom -12px;
  background-size: 58% auto;
  opacity: 0.16;
}
/* Fallback: works even if tools.php bg field was not uploaded */
.tool-card[data-id="excel-split"] .tool-card-bg {
  background-image: url("/assets/img/excel-split-bg.png");
}
.tool-card[data-id="case-convert"] .tool-card-bg {
  background-image: url("/assets/img/case-convert-bg.png");
}
.tool-card[data-id="listing-prompt"] .tool-card-bg {
  background-image: url("/assets/img/listing-prompt-bg.png");
}
.tool-card[data-id="takealot-fee"] .tool-card-bg {
  background-image: url("/assets/img/takealot-fee-bg.png");
}
.tool-card > :not(.tool-card-bg) {
  position: relative;
  z-index: 1;
}
.tool-card h2 {
  margin: 0 0 8px;
  font-size: 1.1rem;
  max-width: 70%;
}
.tool-card p {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
  max-width: 72%;
}
.tool-card .hint {
  margin-top: 12px;
  font-size: 0.75rem;
  color: #8a97a5;
}

.crumb { margin: 0 0 16px; }
.tool-page h1 { margin: 0 0 8px; }
.lead { color: var(--muted); margin: 0 0 16px; }
.rules {
  margin: 0 0 20px;
  padding-left: 1.2rem;
  color: var(--muted);
  line-height: 1.55;
}
.rules code {
  background: #e8eef3;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.9em;
}

.panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  display: grid;
  gap: 16px;
}
.field {
  display: grid;
  gap: 8px;
}
.field span { font-weight: 600; font-size: 0.92rem; }
.field input[type="file"],
.field input[type="number"],
.field input[type="text"],
.field input[type="password"],
.field textarea,
.field select {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  width: 100%;
}
.field textarea {
  resize: vertical;
  min-height: 120px;
  line-height: 1.5;
}
.actions { display: flex; gap: 10px; flex-wrap: wrap; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  border: 1px solid transparent;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn-sm {
  padding: 6px 12px;
  font-size: 0.85rem;
}
.btn-primary {
  background: var(--brand);
  color: #fff;
}
.btn-primary:hover { background: var(--brand-hover); text-decoration: none; color: #fff; }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-ghost {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}
.btn-ghost:hover { border-color: #b0bec8; }

.result-list {
  display: grid;
  gap: 12px;
}
.result-row {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  background: #fbfcfd;
}
.result-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.result-head strong { font-size: 0.95rem; }
.result-note {
  color: var(--muted);
  font-size: 0.8rem;
}
.result-head .copy-btn { margin-left: auto; }
.result-out {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font: inherit;
  line-height: 1.5;
  color: var(--text);
  min-height: 1.5em;
}

.status { min-height: 1.4em; margin: 0; color: var(--muted); }
.status.error { color: #b42318; }
.status.ok { color: var(--brand); }
.hidden { display: none !important; }
.download-wrap { margin: 0; }

.manual-fallback {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 10px 12px;
}
.manual-fallback summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 8px;
}
.manual-fallback .field { margin-top: 10px; }
.scrape-box {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  background: #fbfcfd;
}
.scrape-box strong { display: block; margin-bottom: 8px; }
.tool-panel-top {
  justify-content: flex-end;
  margin-bottom: 4px;
}
.gen-note {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.45;
}
.listing-page h1 { margin-bottom: 16px; }
.listing-results,
.listing-prompt-box {
  margin-top: 16px;
  display: grid;
  gap: 14px;
}
.listing-part {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  background: #fbfcfd;
}
.listing-part-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.listing-part-head > span:first-child {
  font-weight: 600;
  font-size: 0.92rem;
}
.listing-count {
  color: var(--muted);
  font-size: 0.8rem;
  margin-right: auto;
}
.listing-count.over { color: #b42318; font-weight: 600; }
.listing-part textarea,
.listing-prompt-box textarea {
  width: 100%;
  resize: vertical;
  font: inherit;
  line-height: 1.45;
}

.fee-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 14px;
}
.fee-page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}
.fee-page-head h1 { margin: 0 0 4px; font-size: 1.55rem; }
.fee-note {
  margin: 0;
  max-width: 42rem;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.4;
  font-weight: 400;
}
.fx-field {
  min-width: 148px;
  margin: 0;
  padding: 4px 8px 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  box-shadow: none;
}
.fx-field > span {
  font-size: 0.72rem;
  margin-bottom: 0;
  line-height: 1.2;
}
.fx-field input {
  padding: 4px 8px;
  min-height: 0;
  height: 30px;
  font-size: 0.92rem;
  margin-top: 2px;
}
.fx-field .fx-hint {
  display: block;
  margin-top: 1px;
  color: var(--muted);
  font-size: 0.65rem;
  line-height: 1.25;
  max-width: 160px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.fee-split {
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(0, 0.95fr);
  gap: 16px;
  align-items: start;
}
.fee-form,
.fee-result {
  margin: 0;
}
.fee-form .fee-row {
  display: grid;
  gap: 12px;
  margin-bottom: 12px;
}
.fee-row-1 { grid-template-columns: 1fr; }
.fee-row-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.fee-row-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.fee-row-cat {
  grid-template-columns: minmax(0, 1fr) minmax(120px, 160px);
  align-items: end;
}
.fee-retail-field { min-width: 0; }
.fee-result {
  position: sticky;
  top: 72px;
  background:
    linear-gradient(165deg, rgba(15, 107, 76, 0.06), transparent 42%),
    var(--card);
}
.fee-result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}
.fee-result-title { margin: 0; font-size: 1.1rem; }
.fee-result-empty {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  padding: 28px 8px;
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: 12px;
  background: rgba(255,255,255,0.55);
}
.fee-table-wrap {
  overflow: auto;
  max-height: min(58vh, 560px);
}
.kpi-profit {
  background: linear-gradient(180deg, #eef8f2, #f7fbf9);
  border-color: #cfe3d8;
}
.kpi-profit .kpi-pair {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  align-items: baseline;
}
.kpi-profit .kpi-pair b { font-size: 1.2rem; }
.kpi-profit .kpi-pair span { color: var(--muted); font-size: 0.85rem; font-weight: 500; }
@media (max-width: 980px) {
  .fee-split { grid-template-columns: 1fr; }
  .fee-result { position: static; }
}
@media (max-width: 720px) {
  .fee-page-head {
    flex-direction: column;
    align-items: stretch;
  }
  .fx-field {
    width: 100%;
    max-width: 220px;
    align-self: flex-start;
  }
  .fx-field .fx-hint { max-width: none; }
  .fee-row-2 { grid-template-columns: 1fr 1fr; }
  .fee-row-4 { grid-template-columns: 1fr 1fr; }
  .fee-row-cat {
    grid-template-columns: 1fr;
    align-items: stretch;
  }
}
@media (max-width: 480px) {
  .fee-page-head h1 { font-size: 1.3rem; }
  .fee-row-2,
  .fee-row-4 { grid-template-columns: 1fr; }
  .fx-field { max-width: none; }
  .fee-kpis { grid-template-columns: 1fr; }
  .fee-table { font-size: 0.85rem; }
  .fee-table th,
  .fee-table td { padding: 8px 6px; }
}
.cat-combo { position: relative; }
.cat-dropdown {
  position: absolute;
  z-index: 20;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  max-height: 280px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.cat-option {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 10px 12px;
  font: inherit;
  cursor: pointer;
}
.cat-option:hover { background: #f3f7f5; }
.cat-option em {
  font-style: normal;
  color: var(--muted);
  white-space: nowrap;
}
.cat-empty {
  padding: 12px;
  color: var(--muted);
}
.readonly-box {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  background: #f7f9fb;
  color: var(--muted);
  min-height: 46px;
  display: grid;
  gap: 4px;
}
.readonly-box.ready { color: var(--text); }
.readonly-box strong { font-size: 0.98rem; }
.readonly-box .muted { color: var(--muted); font-size: 0.85rem; }
.fee-meta {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.fee-kpis {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
@media (max-width: 560px) {
  .fee-kpis { grid-template-columns: 1fr; }
}
.kpi {
  background: #f6faf8;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
}
.kpi-label {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 6px;
}
.kpi-value {
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.3;
}
.kpi-sub {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--muted);
}
.kpi-value.pos { color: var(--brand); }
.kpi-value.neg { color: #b42318; }
.fee-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
.fee-table th,
.fee-table td {
  border-bottom: 1px solid var(--border);
  padding: 10px 8px;
  text-align: left;
  vertical-align: top;
}
.fee-table th:nth-child(2),
.fee-table th:nth-child(3),
.fee-table td:nth-child(2),
.fee-table td:nth-child(3) {
  text-align: right;
  white-space: nowrap;
}
.fee-table .total-row td {
  font-weight: 700;
  border-bottom: none;
  padding-top: 14px;
}
