/* ============================================================
   CRMLS Reports — mobile-first premium UI
   ============================================================ */
:root {
  --ink: #0f1b2d;
  --ink-2: #1c2c44;
  --paper: #f7f5f0;
  --card: #ffffff;
  --line: #e6e1d6;
  --muted: #6b7688;
  --text: #172033;
  --gold: #c9a24b;
  --gold-deep: #a9822f;
  --accent: #2f6f6b;
  --danger: #c0432f;
  --ok: #2f8f5b;
  --radius: 16px;
  --shadow: 0 10px 30px rgba(15, 27, 45, 0.10);
  --shadow-sm: 0 2px 10px rgba(15, 27, 45, 0.06);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  color: var(--text);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
}
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 0.9rem; margin: 0.5rem 0 0; }
[hidden] { display: none !important; }
em { font-style: italic; color: var(--text); }

/* ---- Buttons ---- */
button { font-family: inherit; cursor: pointer; }
.btn-primary {
  background: var(--ink);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 0.85rem 1.4rem;
  font-size: 1rem;
  font-weight: 600;
  transition: transform 0.08s ease, background 0.2s ease;
}
.btn-primary:hover { background: var(--ink-2); }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.6; cursor: default; }
.btn-ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 0.5rem 0.9rem;
  font-size: 0.9rem;
  font-weight: 500;
}
.btn-ghost:hover { border-color: var(--muted); }

/* ---- Top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--ink);
  color: #fff;
  padding: env(safe-area-inset-top) 0 0;
}
.topbar-inner {
  max-width: 1120px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem;
}
.brand-row { display: flex; align-items: center; gap: 0.6rem; }
.topbar-title { font-weight: 600; letter-spacing: 0.01em; }
.topbar-actions { display: flex; gap: 0.5rem; }
.topbar .btn-ghost { color: #fff; border-color: rgba(255,255,255,0.25); }
.topbar .btn-ghost:hover { border-color: rgba(255,255,255,0.6); }

.brand-mark {
  width: 56px; height: 56px; border-radius: 14px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--gold), var(--gold-deep));
  color: var(--ink); font-weight: 800; font-size: 1.3rem; letter-spacing: 0.02em;
  box-shadow: var(--shadow-sm);
}
.brand-mark.small { width: 34px; height: 34px; border-radius: 9px; font-size: 0.85rem; }

/* ---- Login ---- */
.login-body {
  min-height: 100vh; display: grid; place-items: center;
  background: radial-gradient(120% 120% at 50% 0%, #1c2c44 0%, var(--ink) 60%);
  padding: 1.5rem;
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--card); border-radius: 22px; padding: 2rem 1.6rem;
  box-shadow: var(--shadow);
}
.login-card .brand { text-align: center; margin-bottom: 1.5rem; }
.login-card .brand-mark { margin: 0 auto 0.9rem; }
.login-card h1 { margin: 0; font-size: 1.4rem; }
.login-card form { display: flex; flex-direction: column; gap: 0.5rem; }
.login-card label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.login-card input {
  padding: 0.85rem 1rem; font-size: 1rem;
  border: 1px solid var(--line); border-radius: 12px; background: #fbfaf7;
}
.login-card input:focus { outline: 2px solid var(--gold); border-color: var(--gold); }
.login-card .btn-primary { margin-top: 0.6rem; }

/* ---- Ask view ---- */
.ask-view { min-height: calc(100vh - 60px); display: grid; place-items: center; padding: 1.5rem 1rem 3rem; }
.ask-hero { text-align: center; max-width: 560px; width: 100%; }
.ask-title { font-family: var(--serif); font-size: clamp(1.8rem, 6vw, 2.6rem); margin: 0.2rem 0 0.6rem; line-height: 1.1; }
.ask-sub { font-size: 1rem; margin: 0 auto 2rem; max-width: 460px; }

.mic {
  position: relative;
  width: 108px; height: 108px; border-radius: 50%;
  border: none; margin: 0.5rem auto 0.8rem;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--ink), var(--ink-2));
  color: #fff; box-shadow: var(--shadow);
  transition: transform 0.1s ease;
}
.mic:active { transform: scale(0.96); }
.mic-ring {
  position: absolute; inset: -6px; border-radius: 50%;
  border: 3px solid var(--gold); opacity: 0;
}
.mic.listening { background: linear-gradient(135deg, var(--accent), #245c58); }
.mic.listening .mic-ring { animation: pulse 1.4s ease-out infinite; opacity: 1; }
@keyframes pulse {
  0% { transform: scale(1); opacity: 0.7; }
  100% { transform: scale(1.35); opacity: 0; }
}
.mic-status { font-size: 0.95rem; min-height: 1.4em; margin: 0 0 1.6rem; }

.text-fallback { display: flex; gap: 0.5rem; max-width: 460px; margin: 0 auto 1.6rem; }
.text-fallback input {
  flex: 1; padding: 0.8rem 1rem; font-size: 1rem;
  border: 1px solid var(--line); border-radius: 12px; background: var(--card);
}
.text-fallback input:focus { outline: 2px solid var(--gold); border-color: var(--gold); }
.text-fallback .btn-primary { padding: 0.8rem 1.1rem; }

.examples { display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: center; }
.chip {
  background: var(--card); border: 1px solid var(--line); color: var(--text);
  border-radius: 999px; padding: 0.5rem 0.9rem; font-size: 0.85rem;
  transition: border-color 0.2s, background 0.2s;
}
.chip:hover { border-color: var(--gold); background: #fffdf7; }

/* ---- Clarify view ---- */
.clarify-view, .report-view, .loading-view { max-width: 1120px; margin: 0 auto; padding: 1.25rem 1rem 4rem; }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.5rem; box-shadow: var(--shadow-sm); max-width: 620px; margin: 1rem auto; }
.eyebrow { text-transform: uppercase; letter-spacing: 0.12em; font-size: 0.72rem; font-weight: 700; color: var(--gold-deep); margin: 0 0 0.4rem; }
.panel-title { font-family: var(--serif); font-size: 1.5rem; margin: 0 0 0.4rem; }
.questions { display: flex; flex-direction: column; gap: 1.25rem; margin: 1.4rem 0; }
.q-block label.q-text { display: block; font-weight: 600; margin-bottom: 0.55rem; }
.q-block .q-why { font-size: 0.82rem; color: var(--muted); margin: -0.35rem 0 0.55rem; }
.q-options { display: flex; flex-wrap: wrap; gap: 0.45rem; margin-bottom: 0.5rem; }
.opt {
  border: 1px solid var(--line); background: #fbfaf7; border-radius: 999px;
  padding: 0.45rem 0.85rem; font-size: 0.88rem; color: var(--text);
}
.opt.selected { background: var(--ink); color: #fff; border-color: var(--ink); }
.q-block input.q-free {
  width: 100%; padding: 0.7rem 0.9rem; border: 1px solid var(--line);
  border-radius: 10px; font-size: 0.95rem; background: #fbfaf7;
}
.q-block input.q-free:focus { outline: 2px solid var(--gold); border-color: var(--gold); }
.clarify-actions { display: flex; justify-content: space-between; gap: 0.75rem; margin-top: 0.5rem; }

/* ---- Loading ---- */
.loading-view { display: grid; place-items: center; min-height: 50vh; gap: 1rem; }
.spinner {
  width: 46px; height: 46px; border-radius: 50%;
  border: 4px solid var(--line); border-top-color: var(--gold);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Report header ---- */
.back-btn { margin-bottom: 0.9rem; font-weight: 600; }
.report-header { margin-bottom: 1rem; }
.report-header h1 { font-family: var(--serif); font-size: clamp(1.5rem, 5vw, 2.1rem); margin: 0 0 0.25rem; }
.report-header .report-meta { color: var(--muted); font-size: 0.9rem; }

/* ---- Stat bar ---- */
.stat-bar {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem;
  margin-bottom: 1.25rem;
}
.stat {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 0.9rem 1rem; box-shadow: var(--shadow-sm);
}
.stat .stat-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; }
.stat .stat-value { font-family: var(--serif); font-size: 1.35rem; font-weight: 600; margin-top: 0.2rem; }
.stat.hero { grid-column: span 2; background: linear-gradient(135deg, var(--ink), var(--ink-2)); color: #fff; border: none; }
.stat.hero .stat-label { color: rgba(255,255,255,0.7); }
.stat.hero .stat-value { color: var(--gold); font-size: 1.7rem; }

/* ---- Report toolbar ---- */
.report-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.75rem; margin-bottom: 1rem; flex-wrap: wrap;
}
.view-toggle { display: inline-flex; background: var(--card); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg { border: none; background: transparent; padding: 0.5rem 0.95rem; font-size: 0.9rem; font-weight: 500; color: var(--muted); }
.seg.active { background: var(--ink); color: #fff; }
.toolbar-right { display: flex; align-items: center; gap: 0.6rem; }
.sort-label { font-size: 0.82rem; color: var(--muted); display: flex; align-items: center; gap: 0.35rem; }
.sort-label select { padding: 0.45rem 0.5rem; border: 1px solid var(--line); border-radius: 9px; background: var(--card); font-size: 0.85rem; }

/* ---- Column/title builder overlay ---- */
.layout-overlay {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(15, 27, 45, 0.55);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 1.5rem 1rem; overflow-y: auto;
}
.layout-modal {
  background: var(--paper); border-radius: 20px; box-shadow: var(--shadow);
  width: 100%; max-width: 720px; padding: 1.25rem 1.25rem 1.5rem; margin: auto 0;
}
.lb-modal-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.lb-modal-head h2 { font-family: var(--serif); font-size: 1.5rem; margin: 0; }
.lb-intro { font-size: 0.9rem; margin: 0.35rem 0 1rem; }
.ask-columns-btn { margin-top: 1.6rem; }
.lb-add-row { margin-bottom: 0.6rem; }
.lb-dropdown { width: 100%; padding: 0.7rem 0.8rem; font-size: 0.95rem; }

/* ---- Layout builder ---- */
.layout-builder {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-sm); padding: 1rem 1.1rem 1.2rem; margin-bottom: 0;
}
.lb-row { margin-bottom: 0.9rem; }
.lb-row:last-child { margin-bottom: 0; }
.lb-head { display: flex; align-items: baseline; gap: 0.6rem; flex-wrap: wrap; margin-bottom: 0.5rem; }
.lb-title { font-weight: 700; font-size: 0.9rem; }
.lb-hint { font-size: 0.75rem; color: var(--muted); }
.chip-track {
  display: flex; flex-wrap: wrap; gap: 0.45rem; min-height: 44px;
  padding: 0.5rem; border-radius: 12px; background: #fbfaf7; border: 1px dashed var(--line);
}
.active-track { border-color: var(--gold); background: #fffdf5; }
.col-chip {
  display: inline-flex; align-items: center; gap: 0.4rem;
  background: var(--ink); color: #fff; border-radius: 999px;
  padding: 0.4rem 0.5rem 0.4rem 0.7rem; font-size: 0.85rem; font-weight: 600;
  touch-action: none; user-select: none; cursor: grab;
}
.col-chip .grip { opacity: 0.6; font-size: 0.8rem; letter-spacing: -2px; }
.col-chip .rm {
  border: none; background: rgba(255,255,255,0.18); color: #fff; cursor: pointer;
  width: 18px; height: 18px; border-radius: 50%; display: grid; place-items: center;
  font-size: 0.7rem; line-height: 1;
}
.col-chip .rm:hover { background: var(--danger); }
.col-chip.dragging { opacity: 0.45; cursor: grabbing; }
.col-chip.drop-target { outline: 2px dashed var(--gold); outline-offset: 2px; }
.palette-chip {
  background: var(--card); border: 1px solid var(--line); color: var(--text);
  border-radius: 999px; padding: 0.4rem 0.8rem; font-size: 0.85rem; cursor: pointer;
}
.palette-chip:hover { border-color: var(--gold); background: #fffdf7; }
.palette-chip::before { content: "＋ "; color: var(--gold-deep); font-weight: 700; }
.lb-saved-controls { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.4rem; }
.lb-select, .lb-name {
  padding: 0.5rem 0.6rem; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); font-size: 0.88rem; font-family: inherit;
}
.lb-name { flex: 1; min-width: 140px; }
.lb-select { min-width: 160px; }
.lb-spacer { flex: 1 1 12px; }
.lb-save { padding: 0.5rem 1rem; }

/* ---- Cards ---- */
.cards-grid { display: grid; grid-template-columns: 1fr; gap: 1rem; }
.listing {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow-sm); display: flex; flex-direction: column;
  transition: transform 0.1s ease, box-shadow 0.2s ease;
}
.listing:hover { box-shadow: var(--shadow); }
.listing-photo { position: relative; aspect-ratio: 16 / 10; background: #e9e5db; overflow: hidden; }
.listing-photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.listing-photo .no-photo { width: 100%; height: 100%; display: grid; place-items: center; color: var(--muted); font-size: 0.85rem; }
.badge {
  position: absolute; top: 0.6rem; left: 0.6rem;
  background: rgba(15,27,45,0.88); color: #fff; font-size: 0.72rem; font-weight: 600;
  padding: 0.28rem 0.6rem; border-radius: 999px; letter-spacing: 0.02em;
}
.badge.dom { left: auto; right: 0.6rem; background: rgba(201,162,75,0.95); color: var(--ink); }
.listing-body { padding: 0.9rem 1rem 1.1rem; }
.listing-price { font-family: var(--serif); font-size: 1.4rem; font-weight: 600; }
.listing-addr { font-weight: 600; margin-top: 0.15rem; }
.listing-sub { color: var(--muted); font-size: 0.88rem; }
.listing-facts { display: flex; flex-wrap: wrap; gap: 0.4rem 0.9rem; margin-top: 0.7rem; font-size: 0.88rem; }
.listing-facts .fact b { font-weight: 700; }
.listing-foot { margin-top: 0.7rem; padding-top: 0.6rem; border-top: 1px solid var(--line); font-size: 0.78rem; color: var(--muted); display: flex; justify-content: space-between; gap: 0.5rem; }

/* ---- Table ---- */
.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-sm); }
table { border-collapse: collapse; width: 100%; font-size: 0.88rem; min-width: 720px; }
thead th {
  text-align: left; padding: 0.7rem 0.85rem; background: #fbfaf7;
  border-bottom: 2px solid var(--line); font-weight: 700; white-space: nowrap;
  position: sticky; top: 0; cursor: pointer; user-select: none;
}
thead th .arrow { color: var(--gold-deep); font-size: 0.7rem; }
tbody td { padding: 0.65rem 0.85rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
tbody tr:hover { background: #fdfcf9; }
td.num, th.num { text-align: right; }

/* ---- Empty / toast ---- */
.empty { text-align: center; color: var(--muted); padding: 3rem 1rem; }
.toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%);
  background: var(--danger); color: #fff; padding: 0.8rem 1.2rem; border-radius: 12px;
  box-shadow: var(--shadow); z-index: 50; max-width: 90vw; font-size: 0.9rem;
}

/* ---- Tablet / desktop ---- */
@media (min-width: 640px) {
  .stat-bar { grid-template-columns: repeat(4, 1fr); }
  .stat.hero { grid-column: span 2; }
  .cards-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 960px) {
  .cards-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ---- Print / PDF ---- */
@media print {
  .topbar, .report-toolbar, #errorToast, .view-toggle, .toolbar-right, .back-btn { display: none !important; }
  body { background: #fff; }
  .report-view { padding: 0; max-width: none; }
  #tableWrap { display: block !important; box-shadow: none; }
  #cardsWrap { display: grid !important; grid-template-columns: repeat(2, 1fr) !important; gap: 0.5rem; }
  .listing { break-inside: avoid; box-shadow: none; border: 1px solid #ccc; }
  .stat.hero { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .stat-bar { grid-template-columns: repeat(4, 1fr); }
}
