:root {
  --app-height: 100svh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);

  --bg: #0f131a;
  --panel: #171d28;
  --line: #2b3445;
  --text: #e8edf8;
  --muted: #a5afc1;
  --brand: #6a7fb8;
  --accent: #4f84eb;
  --danger: #ef4444;
  --success: #22c55e;
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; background: var(--bg); }
body {
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
}

.app {
  width: 100%;
  height: var(--app-height);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--bg);
  overflow: hidden;
}

.header {
  padding-top: calc(var(--safe-top) + 10px);
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.header-title { font-size: 17px; font-weight: 700; }
.header-actions { display: flex; gap: 8px; align-items: center; }

.btn-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 0;
  background: var(--line);
  color: var(--text);
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-text {
  padding: 8px 12px;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn-text:active { opacity: 0.6; }

.content {
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 12px max(12px, var(--safe-right)) 8px max(12px, var(--safe-left));
}

.note-list {
  max-width: 700px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.note-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  cursor: pointer;
  transition: transform .05s;
}
.note-card:active { transform: scale(0.99); }
.note-name { font-size: 15px; font-weight: 600; margin-bottom: 4px; }
.note-preview {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.35;
  max-height: 2.7em;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.note-meta {
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
}

.empty {
  text-align: center;
  color: var(--muted);
  margin-top: 40px;
  font-size: 14px;
}

.composer {
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  padding-top: 8px;
  padding-left: max(10px, var(--safe-left));
  padding-right: max(10px, var(--safe-right));
  padding-bottom: calc(8px + var(--safe-bottom));
}
.composer-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 700px;
  margin: 0 auto;
}
.input {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  resize: none;
  border: 1px solid #344057;
  border-radius: 20px;
  padding: 9px 12px;
  font-size: 16px;
  line-height: 1.3;
  background: #1d2533;
  color: var(--text);
  outline: none;
}
.send {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 999px;
  font-size: 18px;
  background: var(--accent);
  color: #fff;
}

/* Login overlay */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 100;
}
.login-box {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-title {
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  margin-bottom: 4px;
}
.login-sub {
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  line-height: 1.4;
}
.code-input {
  width: 100%;
  font-size: 28px;
  letter-spacing: 8px;
  text-align: center;
  padding: 12px 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  outline: none;
}
.btn-primary {
  width: 100%;
  padding: 12px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.btn-secondary {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}

/* Editor */
.editor-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: none;
  flex-direction: column;
  z-index: 90;
}
.editor-overlay.open { display: flex; }
.editor-header {
  padding-top: calc(var(--safe-top) + 10px);
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.editor-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px max(12px, var(--safe-right)) 12px max(12px, var(--safe-left));
}
.editor-form {
  max-width: 700px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.editor-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.editor-input {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.editor-textarea {
  width: 100%;
  flex: 1;
  min-height: 200px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  outline: none;
  resize: none;
  font-family: inherit;
}

.toast {
  position: fixed;
  top: calc(var(--safe-top) + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(-120%);
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 200;
  transition: transform .25s ease;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* Breadcrumbs */
.breadcrumb-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  flex: 1;
  min-width: 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.breadcrumb-bar::-webkit-scrollbar { display: none; }
.breadcrumb-item {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  color: var(--text);
  opacity: 0.8;
}
.breadcrumb-item:hover { opacity: 1; }
.breadcrumb-item:last-of-type { opacity: 1; }
.breadcrumb-sep {
  font-size: 13px;
  color: var(--muted);
  opacity: 0.6;
  white-space: nowrap;
}

/* Folder cards */
.folder-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  cursor: pointer;
  transition: transform .05s;
  display: flex;
  align-items: center;
  gap: 12px;
}
.folder-card:active { transform: scale(0.99); }
.folder-icon { font-size: 22px; line-height: 1; }
.folder-info { flex: 1; min-width: 0; }
.folder-name { font-size: 15px; font-weight: 600; margin-bottom: 2px; }
.folder-meta { font-size: 12px; color: var(--muted); }

/* Search overlay */
.search-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: none;
  flex-direction: column;
  z-index: 95;
}
.search-overlay.open { display: flex; }
.search-header {
  padding-top: calc(var(--safe-top) + 10px);
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  display: flex;
  align-items: center;
  gap: 10px;
}
.search-input {
  flex: 1;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.search-results {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px max(12px, var(--safe-right)) 12px max(12px, var(--safe-left));
}

/* Folder select in editor */
.folder-select {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.folder-select::after {
  content: '\25bc';
  font-size: 10px;
  color: var(--muted);
  margin-left: 8px;
}
.folder-picker {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  margin-top: 4px;
}
.folder-picker-item {
  padding: 10px 12px;
  font-size: 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
  color: var(--text);
}
.folder-picker-item:last-child { border-bottom: 0; }
.folder-picker-item.selected { background: var(--accent); color: #fff; }
.folder-picker-item:hover:not(.selected) { background: var(--line); }

/* Context menu */
.context-menu {
  position: fixed;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px 0;
  min-width: 130px;
  z-index: 150;
  display: none;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.context-menu.open { display: block; }
.context-item {
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text);
}
.context-item:hover { background: var(--line); }
.context-item.context-danger { color: var(--danger); }
.context-item.context-danger:hover { background: rgba(239,68,68,0.15); }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 160;
}
.modal-overlay.open { display: flex; }
.modal-box {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px;
  max-width: 340px;
  width: 100%;
}
.modal-title {
  font-size: 17px;
  font-weight: 700;
  margin-bottom: 10px;
  text-align: center;
}
.modal-body {
  font-size: 14px;
  color: var(--muted);
  line-height: 1.5;
  text-align: center;
  margin-bottom: 16px;
}
.modal-actions {
  display: flex;
  gap: 10px;
}
.modal-actions button { flex: 1; }
.btn-danger {
  padding: 12px;
  border: 0;
  border-radius: 12px;
  background: var(--danger);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

/* Bottom Action Sheet */
.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 140;
  display: none;
  flex-direction: column;
  justify-content: flex-end;
}
.sheet-overlay.open { display: flex; }

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
  opacity: 0;
  transition: opacity 0.25s ease;
}
.sheet-overlay.open .sheet-backdrop { opacity: 1; }

.sheet-container {
  position: relative;
  background: var(--panel);
  border-top: 1px solid var(--line);
  border-radius: 20px 20px 0 0;
  padding: 8px 0 calc(12px + var(--safe-bottom));
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}
.sheet-overlay.open .sheet-container { transform: translateY(0); }

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  margin: 6px auto 10px;
}

.sheet-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 20px;
  font-size: 16px;
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sheet-item:active { background: var(--line); }

.sheet-icon {
  width: 28px;
  text-align: center;
  font-size: 18px;
}

.sheet-divider {
  height: 1px;
  background: var(--line);
  margin: 6px 16px;
}

.sheet-destructive { color: var(--danger); }

@media (orientation: landscape) {
  .header { padding-top: calc(var(--safe-top) + 6px); padding-bottom: 8px; }
  .header-title { font-size: 15px; }
  .editor-header { padding-top: calc(var(--safe-top) + 6px); padding-bottom: 8px; }
  .composer { padding-top: 6px; padding-bottom: calc(6px + var(--safe-bottom)); }
  .input { min-height: 36px; }
  .send { width: 40px; height: 40px; }
}

@media (max-width: 400px) {
  .header-actions { gap: 4px; }
  .btn-icon { width: 32px; height: 32px; font-size: 15px; }
}
