:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-hover: #232734;
  --primary: #4fc3f7;
  --primary-dark: #29b6f6;
  --accent: #ffb74d;
  --danger: #ef5350;
  --success: #66bb6a;
  --text: #e8eaed;
  --text-muted: #888aa0;
  --border: #2a2e3c;
  --radius: 10px;
  --shadow: 0 2px 12px rgba(0,0,0,0.3);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: calc(80px + var(--safe-bottom));
  overscroll-behavior: none;
}

.app-header {
  background: linear-gradient(135deg, #1a1d27, #232734);
  padding: 12px 12px 0;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.app-header h1 { font-size: 1.3rem; font-weight: 700; }
.app-header h1 .emoji { margin-right: 4px; }

.header-top {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  position: relative;
  min-height: 36px;
  gap: 6px;
}

.header-top .user-bar {
  margin-left: auto;
  flex-shrink: 0;
}

/* List management icon (top left) */
.btn-list-mgmt {
  background: none; border: none;
  font-size: 1.1rem; cursor: pointer;
  padding: 4px 6px; color: var(--text-muted);
  flex-shrink: 0;
}

.btn-list-mgmt:active { color: var(--text); }

/* Tab + List combined dropdown (center) */
.tab-list-wrap {
  flex: 1;
  display: flex;
  justify-content: center;
  gap: 6px;
}

.tab-list-dropdown {
  position: relative;
}

.tab-list-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-hover);
  padding: 6px 14px;
  border-radius: 20px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  max-width: 160px;
}

.tab-list-badge:active { background: var(--border); }
.tab-list-badge.active { color: var(--primary); font-weight: 700; }
.tab-list-badge span { overflow: hidden; text-overflow: ellipsis; }

.tab-list-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  min-width: 260px;
  max-width: 90vw;
  z-index: 300;
  display: none;
  overflow: hidden;
}

.tab-list-menu.visible { display: block; }

.menu-header {
  padding: 10px 14px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.menu-items { max-height: 240px; overflow-y: auto; }

.menu-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.1s;
}

.menu-list-item:active { background: var(--surface-hover); }
.menu-list-item.active { color: var(--primary); font-weight: 600; }

.menu-list-type { font-size: 0.8rem; }

.menu-action {
  display: block;
  width: 100%;
  padding: 12px 14px;
  background: none;
  border: none;
  color: var(--primary);
  font-size: 0.9rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.menu-action:active { background: var(--surface-hover); }

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px;
}

/* --- Input Bar --- */
.input-bar-wrap {
  position: sticky;
  top: 0;
  z-index: 99;
  background: var(--bg);
  padding: 8px 12px;
}

.input-bar {
  display: flex;
  gap: 8px;
}

.input-bar input[type="text"] {
  flex: 1;
  padding: 14px 16px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 16px; /* prevents iOS zoom */
  outline: none;
  transition: border-color 0.2s;
}

.input-bar input:focus { border-color: var(--primary); }

.btn-add {
  padding: 14px 18px;
  background: var(--primary);
  color: #000;
  border: none;
  border-radius: var(--radius);
  font-size: 1.2rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s;
  min-width: 50px;
}

.btn-add:active { transform: scale(0.95); background: var(--primary-dark); }

/* --- Autocomplete --- */
.autocomplete-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 58px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: var(--shadow);
  z-index: 50;
  max-height: 280px;
  overflow-y: auto;
  display: none;
}

.autocomplete-list.visible { display: block; }

.ac-item {
  padding: 14px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
}

.ac-item:last-child { border-bottom: none; }
.ac-item:active, .ac-item.highlighted { background: var(--surface-hover); }

.ac-cat-icon { font-size: 1.2rem; }
.ac-cat-badge {
  margin-left: auto;
  font-size: 0.7rem;
  color: var(--text-muted);
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 20px;
}

/* --- Category Groups --- */
.category-group {
  margin-bottom: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.group-header {
  padding: 14px 16px;
  background: var(--surface-hover);
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  user-select: none;
}

.group-header .group-count {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 400;
}

.group-body { display: block; }
.group-body.collapsed { display: none; }

/* --- List Items --- */
.list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}

.list-item:last-child { border-bottom: none; }
.list-item:active { background: var(--surface-hover); }
.list-item.checked { opacity: 0.4; }
.list-item.checked .item-name { text-decoration: line-through; }

.item-checkbox {
  width: 28px;
  height: 28px;
  min-width: 28px;
  border: 2px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

.item-checkbox.checked {
  background: var(--success);
  border-color: var(--success);
}

.item-checkbox.checked::after {
  content: '✓';
  color: #fff;
  font-size: 16px;
  font-weight: bold;
}

.item-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.item-name {
  font-size: 1rem;
  font-weight: 500;
  word-break: break-word;
}

.item-name-edit {
  font-size: 1rem;
  font-weight: 500;
  background: var(--bg);
  border: 1px solid var(--primary);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 8px;
  outline: none;
  width: 100%;
}

.item-quantity {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 600;
}

.item-quantity-edit {
  font-size: 0.8rem;
  background: var(--bg);
  border: 1px solid var(--primary);
  border-radius: 6px;
  color: var(--accent);
  padding: 4px 6px;
  outline: none;
  width: 80px;
  margin-top: 2px;
}

.item-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.btn-icon {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  transition: all 0.15s;
}

.btn-icon:active { background: var(--bg); color: var(--text); }
.btn-delete:active { color: var(--danger); }
.btn-star { font-size: 1.2rem; }
.btn-star.starred { color: var(--accent); }
.btn-star:not(.starred):active { color: var(--accent); }

/* Classification spinner */
.spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    margin-left: 8px;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* --- Empty State --- */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}

.empty-state .big-emoji { font-size: 2.5rem; margin-bottom: 12px; }

/* --- Bottom Toolbar --- */
.bottom-toolbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  display: flex;
  gap: 8px;
  justify-content: center;
  z-index: 100;
}

.toolbar-btn {
  padding: 12px 20px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
  flex: 1;
  max-width: 200px;
}

.toolbar-btn:active { background: var(--surface-hover); }
.toolbar-btn.danger:active { border-color: var(--danger); color: var(--danger); }

@media (max-width: 520px) {
  .container { padding: 10px; }
  .app-header h1 { font-size: 1.1rem; }
  .btn-add { padding: 14px 14px; min-width: 44px; }
  .bottom-toolbar { padding: 10px 12px; padding-bottom: calc(10px + var(--safe-bottom)); }
  .toolbar-btn { padding: 10px 14px; font-size: 0.8rem; }
  .list-dropdown { max-width: 200px; font-size: 0.8rem; }
  .app-tabs { max-width: 260px; }
  .tab-btn { padding: 8px; font-size: 0.8rem; }
  .login-card { padding: 28px 20px; }
  .modal-card { padding: 24px 20px; }
  .create-user-form { flex-direction: column; }
  .create-user-form input, .create-user-form select, .create-user-form .btn-login { width: 100%; }
  .user-row { flex-wrap: wrap; gap: 8px; }
  .user-info { flex: 1; min-width: 0; }
  .user-actions { flex-shrink: 0; }
  .priority-select { width: 32px; height: 26px; font-size: 0.75rem; }
}

/* ============================================================
   LOADING SPLASH
   ============================================================ */

.loading-spinner-wrap { text-align: center; }
.loading-spinner-wrap .big-emoji { font-size: 2.5rem; margin-bottom: 16px; }

.spinner-lg {
  width: 32px; height: 32px; margin: 0 auto;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ============================================================
   LOGIN SCREEN
   ============================================================ */

.login-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px; background: var(--bg);
}

.login-card {
  background: var(--surface); border-radius: var(--radius);
  padding: 40px 32px; width: 100%; max-width: 380px;
  box-shadow: var(--shadow); text-align: center;
}

.login-card h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 8px; }
.login-subtitle { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 24px; }

.login-form { display: flex; flex-direction: column; gap: 12px; }

.login-form input {
  padding: 14px 16px; background: var(--bg);
  border: 2px solid var(--border); border-radius: var(--radius);
  color: var(--text); font-size: 16px; outline: none;
}

.login-form input:focus { border-color: var(--primary); }

.btn-login {
  padding: 14px; background: var(--primary); color: #000;
  border: none; border-radius: var(--radius);
  font-size: 1rem; font-weight: 600; cursor: pointer;
}

.btn-login:active { transform: scale(0.97); }
.btn-login:disabled { opacity: 0.5; cursor: default; }

.login-error { color: var(--danger); font-size: 0.85rem; margin-top: 12px; min-height: 1.2em; }

/* ============================================================
   USER BAR
   ============================================================ */

.user-bar {
  display: flex; align-items: center; justify-content: flex-end;
  gap: 8px; position: relative;
}

.user-badge {
  font-size: 1.1rem;
  color: var(--text);
  background: var(--surface-hover);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-badge:active { background: var(--border); }

.user-menu-wrap { position: relative; }

.user-dropdown {
  position: absolute; top: calc(100% + 6px); right: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  min-width: 200px; z-index: 150; display: none; overflow: hidden;
}

.user-dropdown.visible { display: block; }

.dropdown-item {
  display: block; width: 100%; padding: 14px 16px;
  background: none; border: none; color: var(--text);
  font-size: 0.9rem; text-align: left; cursor: pointer;
}

.dropdown-item:active { background: var(--surface-hover); }
.dropdown-user-name {
  padding: 12px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  text-align: center;
}
.dropdown-logout { color: var(--danger); }
.dropdown-logout:active { background: rgba(239, 83, 80, 0.1); }
.dropdown-divider { border: none; border-top: 1px solid var(--border); margin: 4px 0; }

/* ============================================================
   APP TABS
   ============================================================ */

.app-tabs {
  display: flex; gap: 0; margin-top: 12px;
  border-bottom: 1px solid var(--border);
  max-width: 320px; margin-left: auto; margin-right: auto;
}

.tab-btn {
  flex: 1; padding: 10px; background: none; border: none;
  border-bottom: 2px solid transparent; color: var(--text-muted);
  font-size: 0.9rem; font-weight: 600; cursor: pointer;
}

.tab-btn:active { color: var(--text); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ============================================================
   TODO ITEMS
   ============================================================ */

.todo-list { display: flex; flex-direction: column; gap: 4px; }

.todo-item {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px; background: var(--surface);
  border-radius: var(--radius); border: 1px solid var(--border);
  min-height: 52px;
}

.todo-item.checked { opacity: 0.4; }
.todo-item.checked .todo-text { text-decoration: line-through; }

.todo-text { font-size: 1rem; font-weight: 500; word-break: break-word; }

.priority-select {
  padding: 2px 4px; border: none; border-radius: 6px;
  font-size: 0.8rem; font-weight: 700; cursor: pointer;
  outline: none; width: 36px; height: 28px;
  text-align: center; text-align-last: center;
  appearance: none; -webkit-appearance: none; -moz-appearance: none;
}

/* ============================================================
   MODALS
   ============================================================ */

.modal-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.6); display: flex;
  align-items: center; justify-content: center;
  z-index: 200; padding: 20px;
}

.modal-card {
  background: var(--surface); border-radius: var(--radius);
  padding: 28px; width: 100%; max-width: 380px;
  box-shadow: var(--shadow); max-height: 90vh; overflow-y: auto;
}

.modal-card.modal-wide { max-width: 560px; }
.modal-card h2 { font-size: 1.1rem; margin-bottom: 16px; }
.modal-hint { color: var(--text-muted); font-size: 0.85rem; margin-bottom: 20px; }

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}

.modal-header h2 { margin: 0; }

.modal-close {
  background: none; border: none; color: var(--text-muted);
  font-size: 1.5rem; cursor: pointer; padding: 0 8px;
}

.modal-close:active { color: var(--text); }

/* ============================================================
   ADMIN USER MANAGEMENT
   ============================================================ */

.admin-content { display: flex; flex-direction: column; gap: 20px; }

.admin-section h3 {
  font-size: 0.9rem; font-weight: 600; margin-bottom: 10px; color: var(--primary);
}

.create-user-form { display: flex; flex-wrap: wrap; gap: 8px; }

.create-user-form input, .create-user-form select {
  flex: 1; min-width: 120px; padding: 10px 14px;
  background: var(--bg); border: 2px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 0.9rem; outline: none;
}

.create-user-form input:focus, .create-user-form select:focus { border-color: var(--primary); }
.create-user-form .btn-login { padding: 10px 20px; }

.user-list { display: flex; flex-direction: column; gap: 8px; }

.user-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--bg);
  border-radius: 8px; border: 1px solid var(--border);
}

.user-row.locked { border-color: var(--danger); }
.user-row.inactive { opacity: 0.55; }

.user-info { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user-name { font-weight: 600; font-size: 0.9rem; }

.user-role-badge {
  font-size: 0.7rem; padding: 2px 8px; border-radius: 20px; font-weight: 600;
}

.user-role-badge.admin { background: var(--primary); color: #000; }
.user-role-badge.user { background: var(--surface-hover); color: var(--text-muted); }

.user-flag {
  font-size: 0.7rem; color: var(--accent);
  background: rgba(255, 183, 77, 0.15); padding: 2px 8px; border-radius: 20px;
}

.user-flag.locked-flag { color: var(--danger); background: rgba(239, 83, 80, 0.15); }
.user-actions { display: flex; gap: 4px; }
.btn-unlock:active { color: var(--success); }

/* ============================================================
   CONFIRM MODAL
   ============================================================ */

.confirm-card { max-width: 400px; text-align: center; }
#confirmModal.modal-overlay { z-index: 300; }
#promptModal.modal-overlay { z-index: 300; }

.confirm-message {
  font-size: 0.95rem; color: var(--text-muted);
  margin: 16px 0 24px; line-height: 1.5;
}

.confirm-actions { display: flex; gap: 12px; justify-content: center; }
.confirm-actions .btn-login { flex: 1; padding: 12px; font-size: 0.9rem; }

.btn-cancel { background: var(--surface-hover) !important; color: var(--text) !important; }
.btn-cancel:active { background: var(--border) !important; }
.btn-danger { background: var(--danger) !important; color: #fff !important; }
.btn-danger:active { opacity: 0.85; }

.prompt-input {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  outline: none;
  margin-top: 8px;
}

.prompt-input:focus { border-color: var(--primary); }
