:root {
  --bg: #fafaf9;
  --surface: #ffffff;
  --border: #ececec;
  --border-strong: #d4d4d4;
  --text: #18181b;
  --text-muted: #71717a;
  --text-subtle: #a1a1aa;
  --chip-bg: #f4f4f5;
  --chip-bg-hover: #e9e9eb;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  overscroll-behavior: none;
}

@media (max-width: 768px) {
  input, textarea, select { font-size: 16px !important; }
}

.topbar {
  padding: max(1.5rem, env(safe-area-inset-top)) 1.5rem 0.75rem;
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
}

.topbar .logo {
  height: 64px;
  width: auto;
  display: block;
  max-width: 70vw;
  object-fit: contain;
}

main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: min-content;
  align-items: start;
  gap: 1.25rem;
  padding: 0.5rem max(1.5rem, env(safe-area-inset-right)) 1.5rem max(1.5rem, env(safe-area-inset-left));
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
}

@media (max-width: 768px) {
  html, body { font-size: 15px; }
  .topbar { padding: max(1rem, env(safe-area-inset-top)) 1rem 0.5rem; }
  .topbar .logo { height: 48px; }
  main { grid-template-columns: 1fr; padding: 0.5rem 1rem 1rem; gap: 1rem; }
  .panel { padding: 1rem; }
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.panel-head {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.875rem;
}

.panel-head input,
.panel-head h2 {
  flex: 1;
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
  border: none;
  background: transparent;
  padding: 0;
  letter-spacing: -0.01em;
  color: var(--text);
}

.panel-head input::placeholder { color: var(--text-subtle); font-weight: 600; }
.panel-head input:focus { outline: none; }

.count {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  background: var(--chip-bg);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  min-width: 1.5rem;
  text-align: center;
}

.add-row {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}

.add-row input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
  min-height: 44px;
}

.add-row input::placeholder { color: var(--text-subtle); }

.add-row input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn-icon {
  min-width: 44px;
  min-height: 44px;
  width: 2.75rem;
  height: 2.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  font-weight: 400;
  background: var(--text);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  padding: 0;
  line-height: 1;
  flex-shrink: 0;
}

.btn-icon:hover { background: #3f3f46; }

.btn-secondary {
  padding: 0.5rem 0.875rem;
  font-size: 0.85rem;
  font-family: inherit;
  font-weight: 500;
  background: var(--text);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
}

.btn-secondary:hover { background: #3f3f46; }

.btn-ghost {
  padding: 0.5rem 0.875rem;
  font-size: 0.85rem;
  font-family: inherit;
  font-weight: 500;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--border-strong);
  background: var(--chip-bg);
}

.items {
  list-style: none;
  padding: 0;
  margin: 0.5rem 0 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.65rem 0.5rem 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.15s;
}

.item:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-sm);
}

.item-label {
  flex: 1;
  font-size: 0.95rem;
  word-break: break-word;
}

.item-chevron {
  display: none;
  color: var(--text-subtle);
  font-size: 1.1rem;
}

.item-sends {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.send-btn {
  padding: 0.25rem 0.6rem;
  font-size: 0.75rem;
  font-family: inherit;
  font-weight: 500;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
  white-space: nowrap;
  max-width: 12rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.send-btn:hover {
  background: var(--accent);
  color: white;
}

.icon-btn {
  width: 1.75rem;
  height: 1.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: 0.8rem;
  color: var(--text-subtle);
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}

.icon-btn:hover { color: var(--text); background: var(--chip-bg); }
.icon-btn.danger:hover { color: var(--danger); background: #fee2e2; }

.icon-btn svg { width: 16px; height: 16px; display: block; }
.dest-items .icon-btn svg { width: 18px; height: 18px; }

.empty {
  text-align: center;
  color: var(--text-subtle);
  padding: 1.75rem 1rem;
  font-size: 0.85rem;
}

.dest-list {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 0.875rem;
  margin-bottom: 0.5rem;
  background: var(--surface);
  transition: border-color 0.15s;
}

.dest-list:hover { border-color: var(--border-strong); }
.dest-list:last-child { margin-bottom: 0; }

.dest-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.dest-head input {
  flex: 1;
  font-weight: 600;
  border: none;
  background: transparent;
  padding: 0.15rem 0;
  font-size: 0.92rem;
  color: var(--text);
  font-family: inherit;
}

.dest-head input:focus { outline: none; }

.dest-items {
  list-style: none;
  padding: 0;
  margin: 0;
}

.dest-items li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.4rem 0.25rem 0.4rem 0.5rem;
  font-size: 0.875rem;
  border-bottom: 1px solid var(--border);
}

.dest-items li:last-child { border-bottom: none; }

.dest-items li > span {
  flex: 1;
  word-break: break-word;
}

.dest-items .icon-btn {
  width: 1.5rem;
  height: 1.5rem;
  font-size: 0.75rem;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
}

.dest-items li:hover .icon-btn { opacity: 1; }

.dest-items .empty {
  padding: 0.5rem 0;
  font-size: 0.8rem;
  background: none;
  border: none;
}

.bottombar {
  padding: 0.5rem 1.5rem max(1.5rem, env(safe-area-inset-bottom));
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  text-align: center;
}

/* ─── Mobile (≤768px) ───────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .item {
    padding: 0.85rem;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }
  .item:active {
    background: var(--chip-bg);
    transform: scale(0.99);
  }
  .item-sends { display: none; }
  .item > .icon-btn { display: none; }
  .item-chevron { display: block; }

  .send-btn { padding: 0.5rem 0.875rem; font-size: 0.875rem; min-height: 36px; }

  .dest-items li { padding: 0.65rem 0.4rem 0.65rem 0.6rem; font-size: 0.95rem; }
  .dest-items .icon-btn { opacity: 1; width: 2.25rem; height: 2.25rem; font-size: 1rem; }

  .panel-head input, .panel-head h2 { font-size: 1.05rem; }
  .dest-head input { font-size: 1rem; }

  .btn-ghost { width: 100%; padding: 0.75rem; }
}

/* ─── Touch devices (any size) — bigger tap targets ─────────────────────── */
@media (hover: none) and (pointer: coarse) {
  .icon-btn { min-width: 36px; min-height: 36px; }
  .dest-items .icon-btn { opacity: 1; }
  .send-btn { min-height: 36px; padding: 0.5rem 0.8rem; font-size: 0.85rem; }
}

/* ─── Bottom Sheet (mobile actions) ─────────────────────────────────────── */
.sheet[hidden] { display: none !important; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  animation: sheet-fade-in 0.15s ease-out;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.sheet-panel {
  position: relative;
  background: var(--surface);
  border-radius: 1.25rem 1.25rem 0 0;
  padding: 0.5rem 1.25rem max(1.25rem, env(safe-area-inset-bottom));
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.15);
  animation: sheet-slide-up 0.2s ease-out;
}

.sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--border-strong);
  border-radius: 999px;
  margin: 0.5rem auto 1rem;
}

.sheet-label {
  font-size: 1.2rem;
  font-weight: 600;
  text-align: center;
  padding: 0 0 1rem;
  word-break: break-word;
  letter-spacing: -0.01em;
}

.sheet-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.sheet-btn {
  width: 100%;
  min-height: 52px;
  padding: 0.875rem 1rem;
  font-size: 1rem;
  font-family: inherit;
  font-weight: 500;
  background: var(--accent-soft);
  color: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sheet-btn:active { background: var(--accent); color: white; }

.sheet-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 1rem 0;
  font-size: 0.9rem;
}

.sheet-secondary {
  display: flex;
  gap: 0.5rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
  margin-top: 0.25rem;
}

.sheet-secondary-btn {
  flex: 1;
  min-height: 48px;
  padding: 0.75rem;
  font-size: 0.95rem;
  font-family: inherit;
  font-weight: 500;
  background: var(--chip-bg);
  color: var(--text);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.sheet-secondary-btn.danger { color: var(--danger); }
.sheet-secondary-btn:active { background: var(--chip-bg-hover); }

.sheet-close {
  width: 100%;
  margin-top: 0.5rem;
  min-height: 44px;
  padding: 0.75rem;
  font-size: 0.95rem;
  font-family: inherit;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

@keyframes sheet-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes sheet-slide-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
