/* ===========================
   Кладбище несказанных слов
   Palette: dark brown + amber
   =========================== */

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

:root {
  --bg: #0d0906;
  --bg2: #1a0f0a;
  --bg3: #241810;
  --bg-input: #1e140e;
  --accent: #e8820c;
  --accent-light: #f4a460;
  --accent-dim: #a05808;
  --text: #e8dcc8;
  --text2: #a09080;
  --text3: #6b5b4b;
  --border: #3a2a1a;
  --danger: #c0392b;
  --success: #27ae60;
  --radius: 14px;
  --radius-sm: 8px;
  --transition: 0.25s ease;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font-body: 'PT Serif', Georgia, serif;
}

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

input, textarea, select, button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  border: none;
  outline: none;
  background: none;
}

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Views ===== */

#app {
  max-width: 600px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
}

.view {
  display: none;
  min-height: 100vh;
  animation: fadeIn 0.2s ease;
}

.view.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== LIBRARY VIEW ===== */

.lib-header {
  text-align: center;
  padding: 48px 20px 24px;
  background: linear-gradient(180deg, var(--bg2) 0%, var(--bg) 100%);
}

.lib-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 28px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text);
  letter-spacing: 0.5px;
}

.lib-author {
  margin-top: 10px;
  font-size: 14px;
  color: var(--accent);
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.lib-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px 16px;
}

.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
}

.search-box .icon-search {
  color: var(--text3);
  flex-shrink: 0;
}

.search-box input {
  flex: 1;
  background: none;
  font-size: 15px;
  color: var(--text);
  min-width: 0;
}

.search-box input::placeholder {
  color: var(--text3);
}

#filter-category {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text2);
  -webkit-appearance: none;
  appearance: none;
}

/* ===== Poems Grid ===== */

.poems-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 0 16px 100px;
}

.poem-card {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  transition: transform var(--transition), border-color var(--transition);
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 140px;
}

.poem-card:active {
  transform: scale(0.97);
}

.poem-card:hover {
  border-color: var(--accent-dim);
}

.poem-card-title {
  font-family: var(--font-body);
  font-size: 15px;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: 8px;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.poem-card-preview {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text2);
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-style: italic;
}

.poem-card-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  font-size: 11px;
  color: var(--text3);
}

.poem-card-category {
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--accent);
}

/* ===== Empty State ===== */

.empty-state {
  text-align: center;
  padding: 60px 40px;
  color: var(--text3);
}

.empty-state.hidden {
  display: none;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  color: var(--accent-dim);
  opacity: 0.6;
}

.empty-state h2 {
  font-family: var(--font-body);
  font-size: 20px;
  font-weight: 400;
  color: var(--text2);
  margin-bottom: 8px;
}

.empty-state p {
  font-size: 14px;
}

/* ===== FAB ===== */

.fab {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  right: calc(50% - 280px);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(232, 130, 12, 0.4);
  transition: transform var(--transition), box-shadow var(--transition);
  z-index: 10;
}

@media (max-width: 600px) {
  .fab {
    right: 20px;
  }
}

.fab:active {
  transform: scale(0.9);
}

.fab:hover {
  box-shadow: 0 6px 28px rgba(232, 130, 12, 0.6);
}

/* ===== EDITOR VIEW ===== */

.ed-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 12px 8px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.ed-actions {
  display: flex;
  gap: 4px;
}

.save-status {
  font-size: 12px;
  color: var(--text3);
  transition: color var(--transition);
}

.save-status.saving {
  color: var(--accent);
}

.save-status.saved {
  color: var(--success);
}

.ed-body {
  padding: 20px 16px 40px;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 56px);
}

.ed-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 24px;
  font-weight: 700;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 8px 0 12px;
  color: var(--text);
  width: 100%;
}

.ed-title::placeholder {
  color: var(--text3);
}

.ed-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.ed-meta select {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
  color: var(--text2);
  -webkit-appearance: none;
  appearance: none;
}

.word-count {
  font-size: 12px;
  color: var(--text3);
}

.ed-textarea {
  flex: 1;
  margin-top: 16px;
  font-family: var(--font-body);
  font-size: 17px;
  line-height: 1.7;
  color: var(--text);
  background: none;
  border: none;
  resize: none;
  width: 100%;
  min-height: 300px;
  padding-bottom: 100px;
}

.ed-textarea::placeholder {
  color: var(--text3);
  font-style: italic;
}

/* ===== PREVIEW VIEW ===== */

.pv-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
  background: linear-gradient(180deg, rgba(0,0,0,0.5) 0%, transparent 100%);
}

.pv-actions {
  display: flex;
  gap: 4px;
}

.pv-display {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 24px 60px;
  background-size: cover;
  background-position: center;
  transition: background var(--transition);
}

.pv-content {
  max-width: 500px;
  width: 100%;
  text-align: center;
}

.pv-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 26px;
  font-weight: 700;
  margin-bottom: 32px;
  line-height: 1.3;
}

.pv-body {
  font-family: var(--font-body);
  font-size: 18px;
  line-height: 1.8;
}

.pv-body .stanza {
  margin-bottom: 24px;
}

.pv-body .stanza:last-child {
  margin-bottom: 0;
}

.pv-author {
  margin-top: 40px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 2px;
  opacity: 0.5;
}

/* ===== Buttons ===== */

.btn-icon {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--text2);
  transition: background var(--transition), color var(--transition);
}

.btn-icon:active {
  background: rgba(255,255,255,0.08);
}

.btn-danger-icon {
  color: var(--text3);
}

.btn-danger-icon:active {
  color: var(--danger);
}

.btn-primary {
  width: 100%;
  padding: 14px 20px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  transition: background var(--transition);
}

.btn-primary:active {
  background: var(--accent-dim);
}

.btn-secondary {
  padding: 12px 24px;
  background: var(--bg3);
  color: var(--text2);
  border-radius: var(--radius-sm);
  font-size: 15px;
  border: 1px solid var(--border);
}

.btn-danger {
  padding: 12px 24px;
  background: var(--danger);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
}

/* ===== Modals ===== */

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal.hidden {
  display: none;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-sheet {
  position: relative;
  width: 100%;
  max-width: 600px;
  max-height: 85vh;
  background: var(--bg2);
  border-radius: 20px 20px 0 0;
  padding: 12px 20px calc(20px + var(--safe-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.modal-handle {
  width: 36px;
  height: 4px;
  background: var(--text3);
  border-radius: 2px;
  margin: 0 auto 16px;
}

.modal-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 20px;
  text-align: center;
  margin-bottom: 20px;
}

.modal-section {
  margin-bottom: 20px;
}

.modal-section h4 {
  font-size: 13px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 12px;
}

/* ===== Backdrops Grid ===== */

.backdrops-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.backdrop-item {
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color var(--transition), transform var(--transition);
  position: relative;
  overflow: hidden;
}

.backdrop-item.active {
  border-color: var(--accent);
}

.backdrop-item:active {
  transform: scale(0.92);
}

.backdrop-item-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 4px;
  font-size: 9px;
  text-align: center;
  background: rgba(0,0,0,0.6);
  color: #fff;
}

/* ===== Fonts List ===== */

.fonts-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.font-item {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid var(--border);
  transition: border-color var(--transition), background var(--transition);
  font-size: 16px;
}

.font-item.active {
  border-color: var(--accent);
  background: rgba(232, 130, 12, 0.08);
}

.font-item:active {
  background: rgba(255,255,255,0.04);
}

/* ===== Colors Grid ===== */

.colors-grid {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.color-item {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color var(--transition), transform var(--transition);
  position: relative;
}

.color-item.active {
  border-color: var(--accent);
}

.color-item.active::after {
  content: '';
  position: absolute;
  inset: 4px;
  border-radius: 50%;
  border: 2px solid var(--bg);
}

.color-item:active {
  transform: scale(0.88);
}

/* ===== Align Options ===== */

.align-options {
  display: flex;
  gap: 8px;
}

.align-item {
  flex: 1;
  padding: 10px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  color: var(--text2);
  transition: border-color var(--transition), background var(--transition);
}

.align-item.active {
  border-color: var(--accent);
  background: rgba(232, 130, 12, 0.08);
  color: var(--accent);
}

/* ===== Range Slider ===== */

.range-slider {
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}

.range-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg2);
}

/* ===== Confirm Dialog ===== */

.confirm-box {
  position: relative;
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 28px 24px 20px;
  margin: auto;
  max-width: 320px;
  text-align: center;
  animation: scaleIn 0.2s ease;
}

.confirm-box p {
  font-size: 16px;
  margin-bottom: 20px;
  color: var(--text);
}

.confirm-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.export-box {
  max-width: 360px;
}

.export-box h3 {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 18px;
  margin-bottom: 16px;
}

@keyframes scaleIn {
  from { transform: scale(0.9); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

/* ===== Upload Button ===== */

.upload-btn {
  display: block;
  text-align: center;
  padding: 12px 16px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  color: var(--text2);
  font-size: 14px;
  cursor: pointer;
  transition: border-color var(--transition);
}

.upload-btn:active {
  border-color: var(--accent);
}

/* ===== Toast ===== */

.toast {
  position: fixed;
  bottom: calc(32px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg3);
  color: var(--text);
  padding: 12px 24px;
  border-radius: 30px;
  font-size: 14px;
  z-index: 200;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
  border: 1px solid var(--border);
  animation: toastIn 0.3s ease;
  white-space: nowrap;
}

.toast.hidden {
  display: none;
}

@keyframes toastIn {
  from { transform: translateX(-50%) translateY(20px); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ===== Responsive ===== */

@media (max-width: 400px) {
  .lib-title {
    font-size: 24px;
  }

  .poems-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .poem-card {
    min-height: auto;
    flex-direction: row;
    align-items: flex-start;
    gap: 12px;
  }

  .poem-card-preview {
    -webkit-line-clamp: 2;
  }

  .backdrops-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (min-width: 601px) {
  .poems-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* ===== iOS-specific ===== */

@supports (-webkit-touch-callout: none) {
  .ed-textarea {
    padding-bottom: 260px;
  }

  body {
    -webkit-user-select: auto;
  }
}

/* ===== Selection ===== */

::selection {
  background: var(--accent-dim);
  color: #fff;
}

/* ===== Scrollbar ===== */

::-webkit-scrollbar {
  width: 4px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}
