*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #f8fafc;
  --surface:      #ffffff;
  --border:       #e2e8f0;
  --muted:        #94a3b8;
  --text:         #1e293b;
  --text-sub:     #64748b;
  --primary:      #3b82f6;
  --accent:       #10b981;
  --danger:       #ef4444;
  --highlight:    #fef08a;   /* exact match — yellow */
  --highlight-fz: #fed7aa;   /* fuzzy match — orange */
  --radius:       6px;
  --shadow:       0 1px 4px rgba(0,0,0,.08);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh; /* dynamic viewport — fixes iOS Safari address bar */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ================================================================
   Header
   ================================================================ */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  flex-shrink: 0;
  z-index: 10;
}
header h1 { font-size: 16px; font-weight: 700; letter-spacing: -.3px; }
#status-bar { display: flex; align-items: center; gap: 8px; color: var(--text-sub); font-size: 12px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.dot-gray  { background: var(--muted); }
.dot-green { background: var(--accent); }
.dot-red   { background: var(--danger); }

/* ================================================================
   Layout — sidebar + data table
   ================================================================ */
.layout {
  display: grid;
  grid-template-columns: 210px 1fr;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

/* ================================================================
   Panels
   ================================================================ */
.panel {
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.panel-data { border-right: none; overflow: hidden; }

/* ================================================================
   Controls Panel
   ================================================================ */
.panel-controls { padding: 0; }
.section { padding: 14px 14px 10px; border-bottom: 1px solid var(--border); }
.section h2 {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .5px; color: var(--text-sub); margin-bottom: 8px;
}
.hint { font-size: 11px; color: var(--muted); margin-bottom: 8px; line-height: 1.6; }

/* ---- Dataset selector ---- */
.dataset-select {
  display: block;
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  margin-bottom: 8px;
  cursor: pointer;
  appearance: auto;
  min-height: 44px; /* touch target */
}
.dataset-select:focus { outline: none; border-color: var(--primary); }

/* ================================================================
   Buttons
   ================================================================ */
.btn {
  display: block;
  width: 100%;
  padding: 9px 12px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 6px;
  min-height: 44px; /* touch target */
  transition: opacity .15s, background .15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:last-child { margin-bottom: 0; }
.btn:disabled   { opacity: .45; cursor: not-allowed; }
.btn-primary    { background: var(--primary); color: #fff; }
.btn-danger     { background: var(--danger);  color: #fff; }
.btn:not(:disabled):hover  { opacity: .85; }
.btn:not(:disabled):active { opacity: .7; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; margin-bottom: 0; }

/* ================================================================
   Center Panel — Toolbar + Data Table
   ================================================================ */

/* ---- Toolbar ---- */
.data-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.toolbar-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text-sub);
  white-space: nowrap;
}

/* ---- Fuzzy toggle button ---- */
.fuzzy-btn {
  padding: 5px 10px;
  font-size: 11px;
  font-weight: 600;
  border: 1.5px solid #f97316;
  border-radius: var(--radius);
  background: transparent;
  color: #f97316;
  cursor: pointer;
  transition: background .15s, color .15s;
  white-space: nowrap;
  flex-shrink: 0;
  min-height: 32px;
  -webkit-tap-highlight-color: transparent;
}
.fuzzy-btn:hover  { background: #fff7ed; }
.fuzzy-btn.active { background: #f97316; color: #fff; }

mark.fuzzy { background: var(--highlight-fz); border-radius: 2px; }
.mode-or mark { background: #d1fae5; }
.mode-fuzzy mark.fuzzy { background: var(--highlight-fz); }

/* ---- Format badge ---- */
.format-badge {
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .5px;
  text-transform: uppercase;
  color: #fff;
  flex-shrink: 0;
}

/* ---- AND / OR segmented toggle ---- */
.mode-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  flex-shrink: 0;
}
.mode-btn {
  padding: 5px 11px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .5px;
  border: none;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  min-height: 32px;
  transition: background .15s, color .15s;
  -webkit-tap-highlight-color: transparent;
}
.mode-btn:first-child { border-right: 1px solid var(--border); }
.mode-btn.active { background: var(--primary); color: #fff; }
.mode-btn:not(.active):hover { background: var(--border); color: var(--text); }

/* ---- Search input ---- */
.data-search-wrap {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  min-width: 0;
}
#data-search {
  width: 100%;
  padding: 8px 32px 8px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 13px;
  background: var(--bg);
  outline: none;
  min-height: 36px;
  transition: border-color .2s, box-shadow .2s;
}
#data-search:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.12);
  background: var(--surface);
}
#data-search:disabled { opacity: .5; cursor: not-allowed; }

.search-clear {
  position: absolute;
  right: 10px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 50%;
  display: none;
  min-height: 28px;
  min-width: 28px;
}
.search-clear:hover { color: var(--text); background: var(--bg); }

.row-count {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  min-width: 90px;
  text-align: right;
}

/* ---- Data Table ---- */
#data-table-wrap {
  flex: 1;
  overflow: auto; /* both axes — horizontal scroll on mobile */
  position: relative;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
#data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
#data-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--bg);
  text-align: left;
  padding: 6px 14px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .4px;
  color: var(--text-sub);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
#data-table td {
  padding: 6px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 260px;
}
#data-table tr:hover td { background: var(--bg); cursor: default; }

/* ---- Search highlight ---- */
mark {
  background: var(--highlight);
  color: var(--text);
  border-radius: 2px;
  padding: 0 1px;
  font-style: normal;
}
.mode-or mark { background: #d1fae5; }

/* ---- Empty / no-match states ---- */
.data-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
  text-align: center;
  padding: 20px;
}

/* ================================================================
   Toast
   ================================================================ */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 9px 18px;
  border-radius: 20px;
  font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,.2);
  z-index: 1000;
  max-width: calc(100vw - 32px);
  text-align: center;
  transition: opacity .3s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toast.hidden { opacity: 0; pointer-events: none; }
.toast.error  { background: var(--danger); }
.toast.warn   { background: #f97316; }

/* ================================================================
   Idle warning bar
   ================================================================ */
#idle-bar {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 6px 14px;
  background: #fff7ed;
  border-bottom: 1px solid #fed7aa;
  font-size: 12px;
  color: #92400e;
  flex-shrink: 0;
}
#idle-bar.visible { display: flex; }
#idle-bar strong  { font-weight: 700; }
#btn-idle-cancel  {
  padding: 3px 10px;
  border: 1px solid #f97316;
  border-radius: var(--radius);
  background: transparent;
  color: #f97316;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}
#btn-idle-cancel:hover { background: #f97316; color: #fff; }

/* ================================================================
   Mobile — max-width 640px
   ================================================================ */
@media (max-width: 640px) {

  body { font-size: 13px; }

  header {
    padding: 8px 12px;
    gap: 8px;
  }
  header h1 { font-size: 13px; }
  #status-text { display: none; } /* save space — dot alone conveys state */

  /* Stack sidebar on top of data panel */
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  /* Sidebar becomes a compact horizontal bar */
  .panel-controls {
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-y: visible;
  }

  .section {
    padding: 8px 10px;
    display: flex;
    align-items: center;
    gap: 8px;
    border-bottom: none;
  }

  .section h2,
  .hint { display: none; }

  .dataset-select {
    flex: 1;
    min-width: 0;
    margin-bottom: 0;
    font-size: 13px;
    padding: 8px 8px;
    min-height: 40px;
  }

  .btn-row { flex-shrink: 0; gap: 6px; }

  .btn {
    padding: 8px 12px;
    font-size: 12px;
    min-height: 40px;
    white-space: nowrap;
  }

  /* Toolbar — allow wrapping */
  .data-toolbar {
    flex-wrap: wrap;
    gap: 6px;
    padding: 7px 10px;
  }

  .toolbar-label { display: none; }
  .format-badge  { order: -1; }

  .row-count {
    min-width: unset;
    flex-basis: 100%;
    text-align: left;
    padding-left: 2px;
    order: 10;
  }

  /* Table: smaller cells, horizontal scroll already enabled */
  #data-table { font-size: 12px; }
  #data-table th { padding: 5px 10px; font-size: 10px; }
  #data-table td { padding: 5px 10px; max-width: 180px; }
}
