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

:root {
  --bg: #0f0f10;
  --surface: #1a1a1d;
  --surface-2: #25252a;
  --text: #eaeaea;
  --muted: #8a8a94;
  --accent: #6aa8ff;
  --danger: #d9534f;
  --ok: #4caf50;
  --border: #2d2d33;
  --radius: 10px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

button, input {
  font: inherit;
  color: inherit;
}

button {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 1rem;
  cursor: pointer;
}
button:hover { background: #32323a; }
button.danger { background: var(--danger); border-color: var(--danger); }
button.danger:hover { background: #c0403c; }

input[type="text"], input[type="password"] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.8rem;
  width: 100%;
  outline: none;
  font-family: inherit;
  font-size: 1rem;
}
input:focus { border-color: var(--accent); }

.screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
}

/* Login */
#login-screen { background: var(--bg); }
.card {
  width: min(360px, 90vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex; flex-direction: column; gap: 0.75rem;
}
.card h1 { font-size: 1.5rem; margin-bottom: 0.5rem; text-align: center; }
.error { color: var(--danger); min-height: 1.2em; font-size: 0.9rem; }

/* Main view */
#main-screen {
  flex-direction: column;
  padding: 2rem 1.5rem 5rem;
}
#head-display {
  flex: 1;
  width: 100%;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 1rem;
  text-align: center;
  position: relative;
}
#head-stack-label {
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  cursor: pointer;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
  user-select: none;
  display: inline-flex;
  align-items: baseline;
  gap: 0.4rem;
  white-space: nowrap;
}
#head-stack-label .sep { opacity: 0.7; }
#head-stack-label .count { font-variant-numeric: tabular-nums; }
#head-stack-label .delete {
  color: var(--danger);
  opacity: 0.75;
  cursor: pointer;
  padding: 0 0.2rem;
  border-radius: 3px;
}
#head-stack-label .delete:hover { opacity: 1; background: rgba(217,83,79,0.12); }

#head-stack-picker .picker-row .delete {
  color: var(--danger);
  opacity: 0.75;
  cursor: pointer;
  padding: 0 0.2rem;
  border-radius: 3px;
}
#head-stack-picker .picker-row .delete:hover { opacity: 1; background: rgba(217,83,79,0.12); }
#head-stack-label:hover { color: var(--text); background: var(--surface); }

#head-stack-label-wrap {
  position: relative;
  display: flex;
  justify-content: center;
}

/* Stack picker — plain-text list that opens upward from above the label.
   The active label stays exactly where it is; other stacks appear above it. */
#head-stack-picker {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translate(-50%, -0.35rem);
  width: min(320px, 80vw);
  max-height: 40vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0;
  z-index: 4;
  scrollbar-width: thin;
}
#head-stack-picker .picker-row {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.3rem 0.6rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  opacity: 0.6;
  cursor: pointer;
  white-space: nowrap;
}
#head-stack-picker .picker-row:hover { opacity: 1; color: var(--text); }
#head-stack-picker .picker-row .name {
  overflow: hidden;
  text-overflow: ellipsis;
}
#head-stack-picker .picker-row .count {
  font-variant-numeric: tabular-nums;
}
#head-stack-picker .picker-row .sep {
  opacity: 0.7;
}

/* Stacked-cards visualization under the head. Absolutely positioned so it
   never pushes the head text upwards — the head display stays centered. */
#head-stack-viz {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: calc(50% + 3.6rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 220px;
  cursor: pointer;
  padding: 6px 4px;
}
#head-stack-viz .bar {
  height: 5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 3px;
  width: 45%;
  transition: opacity 0.2s;
}

/* Expanded replacement for the bars — plain-text list of items beneath the
   top. Shares the bars' anchor point so the head above does not shift. */
#head-stack-list {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: calc(50% + 3.2rem);
  width: min(560px, 90vw);
  max-height: 40vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0;
  cursor: pointer;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
#head-stack-list .sub-item {
  background: transparent;
  border: none;
  padding: 0.25rem 0;
  font-size: 1.1rem;
  color: var(--text);
  opacity: 0.55;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.35;
  text-align: center;
}
#head-stack-list .sub-item:hover { opacity: 0.85; }

/* Page-number-style count in the bottom-right corner */
#head-count {
  position: fixed;
  right: calc(1rem + env(safe-area-inset-right));
  bottom: calc(1rem + env(safe-area-inset-bottom));
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  opacity: 0.5;
  letter-spacing: 0.05em;
  pointer-events: none;
  z-index: 3;
}
#head-content {
  font-size: clamp(1.6rem, 5vw, 2.6rem);
  line-height: 1.3;
  max-width: 900px;
  word-wrap: break-word;
  white-space: pre-wrap;
}
#head-content.empty {
  color: var(--muted);
  font-style: italic;
}

#bottom-bar {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  gap: 0.6rem;
  align-items: center;
  z-index: 5;
}

#menu-btn {
  position: fixed;
  top: calc(0.75rem + env(safe-area-inset-top));
  left: calc(0.75rem + env(safe-area-inset-left));
  width: 2.2rem;
  height: 2.2rem;
  padding: 0;
  border-radius: 999px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  opacity: 0.55;
  z-index: 5;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
}
#menu-btn:hover {
  opacity: 1;
  color: var(--text);
  background: var(--surface-2);
}

.quick-btn {
  border-radius: 999px;
  width: 2.2rem;
  height: 2.2rem;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1;
  background: transparent;
  border: none;
  color: var(--muted);
  opacity: 0.5;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
}
.quick-btn:hover {
  opacity: 1;
  background: var(--surface-2);
}
#quick-push-btn:hover { color: var(--ok); }
#quick-pop-btn:hover  { color: var(--danger); }

/* Modals */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.75);
  backdrop-filter: blur(4px);
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  animation: fade 0.15s ease;
}
.modal-card {
  width: min(440px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.modal-card h2 {
  font-size: 1rem;
  color: var(--muted);
  font-weight: 600;
}
.modal-card h2 span { color: var(--text); }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}
.modal button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #0b1220;
}
.modal button.primary:hover { background: #549af0; }
.modal button.primary.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.modal button.primary.danger:hover { background: #c0403c; }
.pop-preview {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 0.9rem;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.95rem;
  max-height: 12rem;
  overflow-y: auto;
}
.pop-preview.empty { color: var(--muted); font-style: italic; }

/* Overlay */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.75);
  backdrop-filter: blur(4px);
  z-index: 10;
  display: flex; justify-content: center;
  overflow-y: auto;
  animation: fade 0.15s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.overlay-inner {
  width: min(560px, 100%);
  padding: 2.5rem 1.25rem calc(2.5rem + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 1.5rem;
}
#overlay-close {
  position: absolute;
  top: 0.75rem; right: 0.75rem;
  padding: 0.35rem 0.6rem;
  background: transparent;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
}

.section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex; flex-direction: column; gap: 0.75rem;
}
.section h2 {
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 600;
}
.section h3 { font-size: 0.95rem; color: var(--muted); margin-top: 0.5rem; }

.row { display: flex; gap: 0.5rem; align-items: center; }
.row input { flex: 1; }

/* Items list */
.items-list { list-style: none; display: flex; flex-direction: column; gap: 0.4rem; }
.item-row {
  display: flex; align-items: center; gap: 0.4rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.6rem;
}
.item-row.dragging { opacity: 0.5; }
.item-row.drag-over { border-color: var(--accent); }
.item-handle {
  cursor: grab;
  color: var(--muted);
  padding: 0 0.4rem;
  user-select: none;
  touch-action: none;
}
.item-text {
  flex: 1;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  resize: none;
  min-height: 1.6em;
  font-family: inherit;
  font-size: 1rem;
  white-space: pre-wrap;
  word-break: break-word;
}
.item-text:focus { border-color: var(--accent); background: var(--surface); }
.item-del {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0.2rem 0.5rem;
  font-size: 0.95rem;
}
.item-del:hover { color: var(--danger); }

/* Heads */
.heads-list { list-style: none; display: flex; flex-direction: column; gap: 0.4rem; }
.heads-list li {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.7rem;
}
.heads-list .name { font-size: 0.85rem; color: var(--muted); margin-bottom: 0.2rem; }
.heads-list .item { white-space: pre-wrap; word-break: break-word; }
.heads-list .empty { color: var(--muted); font-style: italic; }

.panel { display: flex; flex-direction: column; gap: 0.5rem; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;   bottom: calc(5rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 1rem;
  z-index: 100;
  max-width: 90vw;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
  animation: toast-in 0.2s ease;
}
@keyframes toast-in {
  from { transform: translate(-50%, 10px); opacity: 0; }
  to   { transform: translate(-50%, 0); opacity: 1; }
}
