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

:root {
  --primary: #e74c3c;
  --primary-dark: #c0392b;
  --bg: #f5f5f5;
  --card-shadow: 0 2px 8px rgba(0,0,0,0.1);
  --header-bg: #e74c3c;
  --text: #333;
  --text-light: #888;
  --radius: 12px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

#app { height: 100%; position: relative; }
.view { display: none; height: 100%; flex-direction: column; }
.view.active { display: flex; }

.app-header {
  background: var(--header-bg); color: white; padding: 16px 20px;
  display: flex; align-items: center; justify-content: space-between;
  min-height: 60px; box-shadow: 0 2px 8px rgba(0,0,0,0.15); z-index: 10;
}
.app-header h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.3px; }
.header-actions { display: flex; gap: 8px; }

.icon-btn {
  background: none; border: none; color: white; cursor: pointer;
  padding: 8px; border-radius: 50%; display: flex; align-items: center;
  justify-content: center; transition: background 0.2s;
}
.icon-btn:hover { background: rgba(255,255,255,0.2); }
.icon-btn:active { background: rgba(255,255,255,0.3); }

.tag-filter-bar {
  display: flex; align-items: center; padding: 8px 12px;
  background: white; border-bottom: 1px solid #eee; gap: 8px;
}
.tag-filter-bar.hidden { display: none; }
.tag-filter-scroll {
  flex: 1; display: flex; gap: 8px; overflow-x: auto;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.tag-filter-scroll::-webkit-scrollbar { display: none; }
.tag-chip {
  padding: 6px 14px; border-radius: 20px; border: 1.5px solid #ddd;
  background: white; font-size: 13px; font-weight: 500; color: var(--text);
  cursor: pointer; white-space: nowrap; transition: all 0.2s;
}
.tag-chip:hover { border-color: var(--primary); color: var(--primary); }
.tag-chip.active { background: var(--primary); color: white; border-color: var(--primary); }
.tag-manage-btn { color: #888; flex-shrink: 0; }

.search-bar {
  display: flex; align-items: center; padding: 8px 16px;
  background: white; border-bottom: 1px solid #eee;
}
.search-bar.hidden { display: none; }
.search-bar input { flex: 1; border: none; outline: none; font-size: 16px; padding: 8px; background: transparent; }
.search-bar .icon-btn { color: #888; }

.notes-grid {
  flex: 1; overflow-y: auto; padding: 16px; display: grid;
  grid-template-columns: repeat(2, 1fr); gap: 12px; align-content: start;
  -webkit-overflow-scrolling: touch;
}
.notes-grid.hidden { display: none; }
@media (min-width: 600px) { .notes-grid { grid-template-columns: repeat(3, 1fr); padding: 20px; gap: 16px; } }
@media (min-width: 900px) { .notes-grid { grid-template-columns: repeat(4, 1fr); } }

.note-card {
  background: white; border-radius: var(--radius); padding: 16px;
  box-shadow: var(--card-shadow); cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s; min-height: 120px;
  display: flex; flex-direction: column; overflow: hidden; border: 2px solid transparent;
}
.note-card:hover { transform: translateY(-2px); box-shadow: 0 4px 16px rgba(0,0,0,0.15); }
.note-card:active { transform: scale(0.97); }
.note-card .card-title {
  font-size: 15px; font-weight: 600; margin-bottom: 8px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.note-card .card-preview {
  font-size: 13px; color: var(--text-light); line-height: 1.4; flex: 1;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.note-card .card-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.note-card .card-tag {
  font-size: 10px; padding: 2px 8px; border-radius: 10px;
  background: rgba(231,76,60,0.1); color: var(--primary); font-weight: 500;
}
.note-card .card-date {
  font-size: 11px; color: #bbb; margin-top: 10px; padding-top: 8px; border-top: 1px solid #f0f0f0;
}

.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; color: #ccc; padding: 40px;
}
.empty-state.hidden { display: none; }
.empty-state p { margin-top: 12px; font-size: 18px; font-weight: 500; }
.empty-state .sub { font-size: 14px; margin-top: 4px; color: #ddd; }

.fab {
  position: fixed; bottom: 24px; right: 24px; width: 60px; height: 60px;
  border-radius: 50%; background: var(--primary); color: white; font-size: 32px;
  border: none; cursor: pointer; box-shadow: 0 4px 16px rgba(231,76,60,0.4);
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.2s, background 0.2s; z-index: 100;
}
.fab:hover { background: var(--primary-dark); transform: scale(1.1); }
.fab:active { transform: scale(0.95); }

.editor-header { justify-content: space-between; }
.editor-actions { display: flex; gap: 4px; }
.editor-body { flex: 1; display: flex; flex-direction: column; padding: 0; overflow: hidden; }

.note-title-input {
  border: none; outline: none; font-size: 22px; font-weight: 700;
  padding: 20px 20px 8px; background: transparent; color: var(--text);
}
.note-title-input::placeholder { color: #ccc; }
.note-content-input {
  flex: 1; border: none; outline: none; font-size: 16px; line-height: 1.6;
  padding: 8px 20px 20px; background: transparent; color: var(--text);
  resize: none; -webkit-overflow-scrolling: touch;
}
.note-content-input::placeholder { color: #ccc; }

.color-picker {
  display: flex; gap: 10px; padding: 12px 20px; background: white;
  border-bottom: 1px solid #eee; justify-content: center; flex-wrap: wrap;
}
.color-picker.hidden { display: none; }
.color-dot {
  width: 32px; height: 32px; border-radius: 50%; border: 2px solid #ddd;
  cursor: pointer; transition: transform 0.15s, border-color 0.15s;
}
.color-dot:hover { transform: scale(1.15); }
.color-dot.selected { border-color: var(--primary); border-width: 3px; }

.tag-picker { padding: 12px 20px; background: white; border-bottom: 1px solid #eee; }
.tag-picker.hidden { display: none; }
.tag-picker-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 10px; font-weight: 600; font-size: 14px; color: var(--text);
}
.tag-picker-close-btn { color: #888 !important; }
.tag-picker-list { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.tag-picker-item {
  padding: 6px 14px; border-radius: 20px; border: 1.5px solid #ddd;
  background: white; font-size: 13px; cursor: pointer; transition: all 0.2s;
}
.tag-picker-item:hover { border-color: var(--primary); }
.tag-picker-item.selected { background: var(--primary); color: white; border-color: var(--primary); }
.tag-picker-add { display: flex; gap: 8px; align-items: center; }
.tag-picker-add input {
  flex: 1; border: 1.5px solid #ddd; border-radius: 8px;
  padding: 8px 12px; font-size: 13px; outline: none;
}
.tag-picker-add input:focus { border-color: var(--primary); }
.tag-add-btn {
  padding: 8px 16px; border: none; border-radius: 8px; background: var(--primary);
  color: white; font-size: 13px; font-weight: 600; cursor: pointer; transition: background 0.2s;
}
.tag-add-btn:hover { background: var(--primary-dark); }

.editor-tags { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 20px; background: rgba(255,255,255,0.5); }
.editor-tags.hidden { display: none; }
.editor-tag {
  font-size: 12px; padding: 3px 10px; border-radius: 12px;
  background: rgba(231,76,60,0.1); color: var(--primary); font-weight: 500;
}

.modal {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
}
.modal.hidden { display: none; }
.modal-backdrop { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); }
.modal-content {
  position: relative; background: white; border-radius: 16px; padding: 24px;
  width: 85%; max-width: 340px; text-align: center; box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}
.modal-content h3 { font-size: 18px; margin-bottom: 8px; }
.modal-content p { font-size: 14px; color: var(--text-light); margin-bottom: 20px; }
.modal-actions { display: flex; gap: 12px; justify-content: center; }
.modal-btn {
  padding: 10px 24px; border: none; border-radius: 8px; font-size: 15px;
  font-weight: 600; cursor: pointer; transition: background 0.2s;
}
.modal-btn.cancel { background: #f0f0f0; color: var(--text); }
.modal-btn.cancel:hover { background: #e0e0e0; }
.modal-btn.danger { background: var(--primary); color: white; }
.modal-btn.danger:hover { background: var(--primary-dark); }

.share-modal-content { text-align: left; }
.share-modal-content h3 { text-align: center; margin-bottom: 16px; }
.share-options { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.share-option-btn {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px;
  border: 1.5px solid #eee; border-radius: 10px; background: white;
  font-size: 15px; cursor: pointer; transition: all 0.2s; color: var(--text);
}
.share-option-btn:hover { border-color: var(--primary); background: #fff5f5; }
.share-cancel-btn { width: 100%; text-align: center; }

.manage-tags-content { text-align: left; max-height: 80vh; overflow-y: auto; }
.manage-tags-content h3 { text-align: center; margin-bottom: 16px; }
.manage-tags-list {
  display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px;
  max-height: 200px; overflow-y: auto;
}
.manage-tag-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; border: 1px solid #eee; border-radius: 8px; font-size: 14px;
}
.manage-tag-item .tag-name { font-weight: 500; }
.manage-tag-item .tag-count { font-size: 12px; color: var(--text-light); margin-left: 8px; }
.manage-tag-delete {
  background: none; border: none; color: #ccc; cursor: pointer;
  padding: 4px; border-radius: 50%; display: flex; transition: color 0.2s;
}
.manage-tag-delete:hover { color: var(--primary); }

.toast {
  position: fixed; bottom: 100px; left: 50%; transform: translateX(-50%);
  background: #333; color: white; padding: 12px 24px; border-radius: 24px;
  font-size: 14px; font-weight: 500; z-index: 2000;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2); transition: opacity 0.3s, transform 0.3s;
  white-space: nowrap;
}
.toast.hidden { opacity: 0; transform: translateX(-50%) translateY(20px); pointer-events: none; }
.toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }

@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.note-card { animation: fadeIn 0.3s ease; }

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ddd; border-radius: 4px; }

@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .fab { bottom: calc(24px + env(safe-area-inset-bottom)); }
}
