:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #dde1e7;
  --text: #1c222b;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-contrast: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 8px 24px rgba(16, 24, 40, 0.16);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1d2127;
    --surface-2: #262b33;
    --border: #353b45;
    --text: #e8eaed;
    --text-muted: #9aa2af;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 3px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 60px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.title-input {
  font-size: 2rem;
  font-weight: 700;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 4px 2px;
  width: 100%;
  border-bottom: 2px solid transparent;
  transition: border-color 0.15s ease;
}
.title-input:focus {
  outline: none;
  border-bottom-color: var(--accent);
}
.title-input::placeholder { color: var(--text-muted); font-weight: 600; }

.description-input {
  font-size: 0.95rem;
  color: var(--text-muted);
  border: none;
  background: transparent;
  resize: vertical;
  min-height: 2.4em;
  padding: 4px 2px;
  font-family: inherit;
  border-bottom: 2px solid transparent;
  transition: border-color 0.15s ease;
}
.description-input:focus {
  outline: none;
  border-bottom-color: var(--border);
  color: var(--text);
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow);
  position: sticky;
  top: 10px;
  z-index: 20;
}

.toolbar-spacer { flex: 1 1 auto; }

.add-item-form {
  display: flex;
  gap: 6px;
  flex: 1 1 260px;
  min-width: 220px;
}
.add-item-form input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.9rem;
}
.add-item-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.05s ease, background 0.15s ease, box-shadow 0.15s ease;
}
.btn:hover { background: var(--border); }
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { background: #1d4ed8; }
.btn-accent { background: #16a34a; border-color: #16a34a; color: #fff; margin-left: auto; }
.btn-accent:hover { background: #15803d; }
.btn-ghost { background: transparent; }

/* ---------- Board ---------- */
.board {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.pool-section {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.pool-section h2 {
  margin: 0 0 2px;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 6px;
}
.hint {
  margin: 0 0 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.count {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.chip-list {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 8px;
  min-height: 46px;
  border-radius: 8px;
  transition: background 0.12s ease;
  padding: 4px;
}
.chip-list.drag-over {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  outline: 2px dashed var(--accent);
  outline-offset: 2px;
}
.chip-list.empty::before {
  content: attr(data-empty-text);
  color: var(--text-muted);
  font-size: 0.82rem;
  font-style: italic;
  padding: 10px 4px;
}

/* ---------- Chips ---------- */
.chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  cursor: grab;
  user-select: none;
  box-shadow: var(--shadow);
  transition: box-shadow 0.15s ease, transform 0.1s ease, opacity 0.1s ease;
}
.chip:active { cursor: grabbing; }
.chip.dragging { opacity: 0.4; }
.chip .chip-remove {
  border: none;
  background: transparent;
  color: inherit;
  opacity: 0.6;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  padding: 0;
}
.chip .chip-remove:hover { opacity: 1; }

/* ---------- Categories ---------- */
.categories-section > h2 {
  margin: 0 0 10px;
  font-size: 1rem;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
  align-items: start;
  border-radius: var(--radius);
  transition: background 0.12s ease;
}
.categories-grid.category-drag-active {
  background: color-mix(in srgb, var(--accent) 5%, transparent);
}

.category-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 5px solid var(--cat-color, var(--accent));
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.category-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.drag-handle {
  border: none;
  background: transparent;
  cursor: grab;
  font-size: 1rem;
  line-height: 1;
  opacity: 0.45;
  padding: 2px 2px;
  flex: 0 0 auto;
}
.drag-handle:hover { opacity: 0.85; }
.drag-handle:active { cursor: grabbing; }

.color-swatch-wrap {
  position: relative;
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}
.color-swatch {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--cat-color, var(--accent));
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--border);
  pointer-events: none;
}
.color-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  border: none;
  padding: 0;
  background: none;
}

.category-name-input {
  flex: 1 1 100px;
  min-width: 70px;
  border: none;
  background: transparent;
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
  padding: 2px 4px;
  border-bottom: 2px solid transparent;
}
.category-name-input:focus { outline: none; border-bottom-color: var(--cat-color, var(--accent)); }

.type-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  flex: 0 0 auto;
}
.type-btn {
  border: none;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 5px 9px;
  cursor: pointer;
}
.type-btn.active {
  background: var(--cat-color, var(--accent));
  color: #fff;
}

.icon-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 0.95rem;
  opacity: 0.55;
  padding: 2px 4px;
  flex: 0 0 auto;
}
.icon-btn:hover { opacity: 1; }

.delete-category-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  color: #dc2626;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1;
  opacity: 0.75;
  padding: 2px 4px;
  flex: 0 0 auto;
}
.delete-category-btn:hover { opacity: 1; }

.category-card.dragging-category {
  opacity: 0.55;
  border: 2px dashed var(--accent);
  box-shadow: var(--shadow-lg);
}

.category-chip-list {
  min-height: 60px;
  flex: 1 1 auto;
}
.category-chip-list .chip {
  background: color-mix(in srgb, var(--cat-color, var(--accent)) 22%, var(--surface));
  border-color: color-mix(in srgb, var(--cat-color, var(--accent)) 55%, var(--border));
  color: var(--chip-text, var(--text));
}

.add-category-tile {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 110px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.add-category-tile:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 6%, transparent);
}

.add-category-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--surface);
}
.add-category-form input {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.9rem;
}
.add-category-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.add-category-form-actions { display: flex; gap: 6px; }
.add-category-form-actions .btn { flex: 1 1 auto; }

/* ---------- PDF preview modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}
.modal-overlay[hidden] { display: none; }

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.pdf-modal {
  width: min(900px, 100%);
  height: min(90vh, 1100px);
}

.open-modal {
  width: min(480px, 100%);
  max-height: min(80vh, 640px);
}

.open-modal-body {
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.open-modal-empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.88rem;
}

.save-entry {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}
.save-entry:hover {
  background: var(--surface-2);
  border-color: var(--border);
}
.save-entry-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.save-entry-title {
  font-weight: 600;
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.save-entry-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
}
.save-entry-delete {
  border: none;
  background: transparent;
  color: #dc2626;
  opacity: 0.6;
  cursor: pointer;
  font-size: 0.9rem;
  padding: 4px;
  flex: 0 0 auto;
}
.save-entry-delete:hover { opacity: 1; }

.open-modal-footer { justify-content: center; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 1rem; }

.pdf-modal-frame {
  flex: 1 1 auto;
  width: 100%;
  border: none;
  background: #525659;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}

/* ---------- Footer ---------- */
.app-footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.78rem;
  padding-top: 10px;
}

@media (max-width: 640px) {
  .title-input { font-size: 1.5rem; }
  .toolbar { position: static; }
  .btn-accent { margin-left: 0; }
}
