:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242736;
  --border: #2e3248;
  --text: #e2e8f0;
  --text-muted: #8892a4;
  --accent: #6c8ef5;
  --accent-hover: #8aa4ff;
  --danger: #e05c5c;
  --danger-hover: #f07070;
  --success: #4caf82;
  --queue-color: #f5a623;
  --bag-color: #7ec8e3;
  --task-color: #a8d8a8;
  --done-color: #556655;
  --radius: 6px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

header h1 { font-size: 18px; font-weight: 600; }

.header-actions { display: flex; gap: 8px; }

.available-banner {
  background: #1e2f1e;
  border-bottom: 1px solid #2e5c2e;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  flex-shrink: 0;
}

.available-banner strong { color: var(--success); }
#available-list { color: var(--text-muted); flex: 1; }
.btn-close { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 18px; line-height: 1; }

main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Tree Panel ─────────────────────────────────────────────────────── */
#tree-panel {
  width: 320px;
  min-width: 200px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
}

#tree-container {
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

/* Tree nodes */
.tree-node { user-select: none; }

/* ── Drag and drop ───────────────────────────────────────────────── */
.drag-handle {
  opacity: 0;
  color: var(--text-muted);
  cursor: grab;
  font-size: 13px;
  padding-right: 2px;
  flex-shrink: 0;
  user-select: none;
}
.tree-node-row:hover .drag-handle { opacity: 1; }
.tree-node-row.dragging { opacity: 0.35; }
.tree-node-row.drop-above { box-shadow: 0 -2px 0 0 var(--accent); }
.tree-node-row.drop-below { box-shadow: 0 2px 0 0 var(--accent); }
.tree-node-row.drop-into  { box-shadow: inset 0 0 0 2px var(--accent); }

.tree-node-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  cursor: pointer;
  border-radius: var(--radius);
  margin: 1px 4px;
  font-size: 14px;
  transition: background .12s;
}

.tree-node-row:hover { background: var(--surface2); }
.tree-node-row.selected { background: #2a3060; }
.tree-node-row.done { opacity: .45; text-decoration: line-through; }

.tree-toggle {
  width: 14px;
  font-size: 10px;
  color: var(--text-muted);
  flex-shrink: 0;
  text-align: center;
}

.type-icon { font-size: 13px; flex-shrink: 0; }
.node-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.available-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--success);
  flex-shrink: 0;
}

.tree-children {
  padding-left: 18px;
}

/* ── Detail Panel ───────────────────────────────────────────────────── */
#detail-panel {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  background: var(--bg);
}

.detail-placeholder {
  color: var(--text-muted);
  font-size: 15px;
  margin-top: 40px;
  text-align: center;
}

.detail-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.detail-header h2 { font-size: 20px; font-weight: 600; }

.type-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 20px;
  text-transform: uppercase;
  letter-spacing: .06em;
}

.badge-task  { background: #1a2e1a; color: var(--task-color); }
.badge-queue { background: #2e2010; color: var(--queue-color); }
.badge-bag   { background: #10202e; color: var(--bag-color); }

form { display: flex; flex-direction: column; gap: 14px; max-width: 480px; }

label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
}

input[type="text"], select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  padding: 8px 10px;
  outline: none;
  transition: border-color .15s;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
}

textarea { resize: vertical; }

select option { background: var(--surface); }

.form-actions { display: flex; gap: 10px; margin-top: 4px; }

#children-section {
  margin-top: 28px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

#children-section h3 {
  font-size: 13px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 10px;
}

.add-child-actions { display: flex; gap: 8px; }

#complete-section {
  margin-top: 20px;
  display: flex;
  gap: 10px;
}

/* ── Buttons ────────────────────────────────────────────────────────── */
button { cursor: pointer; font-family: var(--font); font-size: 13px; border: none; border-radius: var(--radius); padding: 7px 14px; transition: background .12s; }

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

.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }

.btn-danger    { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-danger:hover { background: #3a1a1a; }

.btn-icon { background: none; border: none; color: var(--text-muted); font-size: 18px; padding: 2px 6px; border-radius: var(--radius); }
.btn-icon:hover { background: var(--surface2); color: var(--text); }

.btn-add-child { font-size: 12px; padding: 5px 10px; }

/* ── Token auth prompt ──────────────────────────────────────────────── */
#token-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

#token-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#token-modal h3 { font-size: 16px; }
#token-modal p  { font-size: 13px; color: var(--text-muted); }

#token-form { display: flex; flex-direction: row; gap: 8px; margin: 0; max-width: none; }
.token-input { flex: 1; }

/* ── Modal ──────────────────────────────────────────────────────────── */
#modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 24px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

#modal h3 { font-size: 16px; }

/* ── Criteria builder ───────────────────────────────────────────────── */
.criteria-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 8px;
}

.criteria-add-btns { display: flex; gap: 6px; }

.criterion-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  margin-bottom: 6px;
}

.criterion-type-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 20px;
  text-transform: uppercase;
  letter-spacing: .06em;
  flex-shrink: 0;
}

.badge-time       { background: #2a1a35; color: #c8a0e8; }
.badge-location   { background: #102020; color: #7ec8c8; }
.badge-recurrence { background: #1a2a1a; color: #7ec87e; }

.criterion-field {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.criterion-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  padding: 4px 7px;
  outline: none;
}

.criterion-input:focus { border-color: var(--accent); }

input[type="time"].criterion-input   { width: 108px; }
input[type="number"].criterion-input { width: 80px; }

.criteria-hint {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  margin-top: 4px;
}

.btn-small { font-size: 11px !important; padding: 4px 9px !important; }

.btn-icon-sm {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 6px;
  border-radius: var(--radius);
  margin-left: auto;
  flex-shrink: 0;
}

.btn-icon-sm:hover { color: var(--danger); background: #3a1a1a; }

/* ── Move picker modal ──────────────────────────────────────────────── */
#move-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#move-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 24px;
  width: 360px;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#move-modal h3 { font-size: 16px; }

.move-picker-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.move-picker-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  transition: background .12s;
}

.move-picker-item:hover { background: var(--surface2); }
.move-picker-empty { font-size: 13px; color: var(--text-muted); font-style: italic; }

/* ── Location status pill ───────────────────────────────────────────── */
.loc-pill {
  font-size: 12px;
  padding: 5px 11px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: transparent;
  cursor: pointer;
  font-family: var(--font);
  transition: opacity .15s, border-color .15s, color .15s;
  white-space: nowrap;
}
.loc-unknown    { color: var(--text-muted); }
.loc-unknown:hover { color: var(--text); border-color: var(--text-muted); }
.loc-requesting { color: var(--accent); border-color: var(--accent); opacity: .7; cursor: default; }
.loc-active     { color: var(--success); border-color: var(--success); background: #0d1f12; }
.loc-active:hover { background: #102516; }
.loc-denied     { color: var(--danger); border-color: var(--danger); background: #1f0d0d; }
.loc-denied:hover { background: #2a1010; }
.loc-unavailable { color: var(--text-muted); opacity: .5; cursor: default; }

/* ── Map picker modal ───────────────────────────────────────────────── */
#map-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 150;
}

#map-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  width: min(640px, calc(100vw - 32px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#map-modal h3 { font-size: 16px; }

#map-container {
  width: 100%;
  height: 350px;
  border-radius: var(--radius);
  overflow: hidden;
}

.map-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.map-coords {
  flex: 1;
  font-family: monospace;
  font-size: 12px;
  color: var(--text-muted);
}

.map-radius-label {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.map-radius-label input { width: 80px; }

/* ── Utilities ──────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Back button (mobile only) ──────────────────────────────────────── */
#btn-back { display: none; }

/* ── Mobile portrait layout ─────────────────────────────────────────── */
@media (max-width: 600px) {
  main {
    position: relative;
    overflow: hidden;
  }

  #tree-panel {
    width: 100%;
    height: 100%;
    border-right: none;
  }

  #detail-panel {
    width: 100%;
    height: 100%;
    padding: 16px;
    overflow-y: auto;
  }

  /* Single-pane navigation via class on <main> */
  main.mobile-view-detail #tree-panel  { display: none; }
  main.mobile-view-tree   #detail-panel { display: none; }

  /* Back button visible on mobile */
  #btn-back {
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }

  /* Larger touch targets */
  .tree-node-row { padding: 10px 14px; font-size: 15px; }
  button { padding: 10px 16px; font-size: 14px; }
  .btn-add-child { font-size: 13px; padding: 8px 12px; }

  form { max-width: 100%; }

  #modal { width: calc(100vw - 32px); }
}
