* { box-sizing: border-box; }
:root {
  --bg: #0f0f14;
  --panel: #1b1b25;
  --panel-2: #222231;
  --text: #e8e8ef;
  --muted: #a9a9b8;
  --accent: #7c8cff;
  --accent-2: #6bd0ff;
  --border: #2a2a3a;
  --success: #3ddc97;
  --danger: #ff6b6b;
  --shadow: 0 6px 24px rgba(0,0,0,.25);
}

/* Light theme */
[data-theme="light"] {
  --bg: #f8f9fa;
  --panel: #ffffff;
  --panel-2: #f1f3f4;
  --text: #202124;
  --muted: #5f6368;
  --accent: #4285f4;
  --accent-2: #1a73e8;
  --border: #dadce0;
  --success: #137333;
  --danger: #d93025;
  --shadow: 0 6px 24px rgba(32,33,36,.15);
  
  /* Light theme toned colors */
  --tone-blue-bg: #e8f0fe;
  --tone-blue-bd: #4285f4;
  --tone-blue-tx: #1a73e8;
  --tone-green-bg: #e6f4ea;
  --tone-green-bd: #34a853;
  --tone-green-tx: #137333;
  --tone-gray-bg: #f1f3f4;
  --tone-gray-bd: #5f6368;
  --tone-gray-tx: #3c4043;
  --tone-purple-bg: #f3e8ff;
  --tone-purple-bd: #9c27b0;
  --tone-purple-tx: #6a1b9a;
  --tone-danger-bg: #ffffff;
  --tone-danger-bd: #d93025;
  --tone-danger-tx: #d93025;

  /* toned colors */
  --tone-blue-bg: #1a2342;
  --tone-blue-bd: #2a3a7a;
  --tone-blue-tx: #cfe1ff;
  --tone-green-bg: #132a26;
  --tone-green-bd: #1f4a41;
  --tone-green-tx: #cbf5e3;
  --tone-gray-bg: #1e1e28;
  --tone-gray-bd: #2c2c3a;
  --tone-gray-tx: #ddddec;
  --tone-purple-bg: #2a1a3a;
  --tone-purple-bd: #4a2a7a;
  --tone-purple-tx: #e6cfff;
  --tone-danger-bg: #3a1a1a;
  --tone-danger-bd: #7a2a2a;
  --tone-danger-tx: #ffcfcf;
}
html, body { height: 100%; }
body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background .3s ease, color .3s ease;
}

.app { max-width: 900px; margin: 20px auto 48px; padding: 0 16px; }

.app__header { margin-bottom: 12px; }
.header-content { display: flex; justify-content: space-between; align-items: flex-start; }
.title { font-weight: 600; font-size: 20px; }
.subtitle { color: var(--muted); font-size: 13px; margin-top: 6px; }
.subtitle a { 
  color: var(--accent); 
  text-decoration: none; 
  transition: color 0.2s ease;
}
.subtitle a:hover { 
  color: var(--accent-2); 
  text-decoration: underline;
}

.theme-toggle {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 16px;
  transition: all .15s ease;
  flex-shrink: 0;
}
.theme-toggle:hover {
  border-color: var(--accent);
  transform: scale(1.05);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.toolbar button,
.export__buttons button {
  -webkit-appearance: none;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 14px;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  min-height: 44px;
  transition: border-color .15s ease, transform .05s ease, background .15s ease, box-shadow .15s ease, filter .2s ease;
  white-space: nowrap;
  box-shadow: none;
  position: relative;
}
.toolbar button:active,
.export__buttons button:active { transform: translateY(1px); }
.toolbar button:hover,
.export__buttons button:hover { border-color: var(--accent); }

/* toned variants */
.tone-blue {
  background: var(--tone-blue-bg);
  border-color: var(--tone-blue-bd);
  color: var(--tone-blue-tx);
}
.tone-blue:hover { box-shadow: 0 4px 16px rgba(108, 139, 255, 0.15); }

.tone-green {
  background: var(--tone-green-bg);
  border-color: var(--tone-green-bd);
  color: var(--tone-green-tx);
}
.tone-green:hover { box-shadow: 0 4px 16px rgba(61, 220, 151, 0.15); }

.tone-gray {
  background: var(--tone-gray-bg);
  border-color: var(--tone-gray-bd);
  color: var(--tone-gray-tx);
}
.tone-gray:hover { box-shadow: 0 4px 16px rgba(200, 200, 220, 0.10); }

.tone-purple {
  background: var(--tone-purple-bg);
  border-color: var(--tone-purple-bd);
  color: var(--tone-purple-tx);
}
.tone-purple:hover { box-shadow: 0 4px 16px rgba(138, 74, 255, 0.15); }

.tone-danger {
  background: var(--tone-danger-bg) !important;
  border-color: var(--tone-danger-bd) !important;
  color: var(--tone-danger-tx) !important;
}
.tone-danger:hover { 
  box-shadow: 0 4px 16px rgba(255, 107, 107, 0.25) !important; 
  border-color: #aa3333 !important;
}

/* success state with check */
button.success { 
  filter: saturate(1.05) brightness(1.02); 
  padding-right: 30px !important;
}
button.success::after {
  content: '✓';
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-weight: 900;
  color: var(--success);
  font-size: 14px;
}

.editor-wrap { 
  margin-top: 12px; 
  position: relative;
}
.editor {
  min-height: 240px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  line-height: 1.6;
  outline: none;
  white-space: pre-wrap;
  /* Ensure spaces and tabs are preserved exactly as entered */
  word-wrap: break-word;
  overflow-wrap: break-word;
  /* Prevent browser from collapsing multiple spaces */
  white-space: pre-wrap !important;
  /* Force consistent line breaks */
  -webkit-nbsp-mode: space;
  -webkit-line-break: after-white-space;
}

/* Ensure line breaks are respected in all formatted elements */
.editor br {
  display: block;
  margin: 0;
  line-height: inherit;
}
.editor:empty::before { content: attr(data-placeholder); color: var(--muted); }
.editor:focus { border-color: var(--accent); box-shadow: var(--shadow); }

.paste-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -webkit-appearance: none;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 14px;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  min-height: 44px;
  transition: border-color .15s ease, transform .05s ease, background .15s ease, box-shadow .15s ease;
  white-space: nowrap;
  box-shadow: var(--shadow);
}
.paste-btn:hover {
  border-color: var(--accent);
  transform: translate(-50%, -50%) scale(1.02);
}
.paste-btn:active {
  transform: translate(-50%, -50%) translateY(1px);
}

.editor :where(b,strong) { font-weight: 600; }
.editor s { text-decoration: line-through; }
.editor :where(code,tt) {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  background: #111623;
  color: #d6e2ff;
  padding: 2px 6px;
  border-radius: 6px;
}
.editor blockquote {
  margin: 8px 0 8px 0;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
  background: var(--panel-2);
  border-radius: 6px;
}
.editor .spoiler { 
  background: var(--panel-2); 
  color: transparent; 
  text-shadow: 0 0 8px var(--text); 
  border-radius: 6px; 
  padding: 2px 6px;
  border: 1px solid var(--border);
}
.editor .spoiler.revealed { color: var(--text); text-shadow: none; }

/* Light theme specific styles */
[data-theme="light"] .editor blockquote {
  background: #f8f9fa;
  border-left-color: var(--accent);
  color: var(--text);
}
[data-theme="light"] .editor .spoiler {
  background: #e8eaed;
  border: 1px solid #dadce0;
  text-shadow: 0 0 8px #5f6368;
}

.export { margin-top: 16px; }
.export__buttons { display: flex; gap: 12px; margin-bottom: 6px; flex-wrap: wrap; }
.status { min-height: 20px; color: var(--muted); font-size: 13px; margin: 2px 2px 8px; }
.output {
  width: 100%;
  min-height: 160px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 13px;
}

/* Light theme output */
[data-theme="light"] .output {
  background: #f8f9fa;
  color: #202124;
  border: 1px solid #dadce0;
}

/* Light theme danger buttons - no fill, only border */
[data-theme="light"] .tone-danger {
  background: transparent !important;
  color: #d93025 !important;
  border: 2px solid #d93025 !important;
}

.app__footer { margin-top: 10px; color: var(--muted); font-size: 12px; }

/* Popup formatting menu */
.popup-menu {
  position: absolute;
  z-index: 1000;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 4px;
  user-select: none;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

/* Popup menu positioning fix for mobile sticky app */
.mobile-body .popup-menu {
  position: absolute;
  /* Use absolute positioning relative to content container for mobile */
}

.mobile-body #content {
  position: relative;
  /* Ensure content container is positioned for absolute children */
}

.popup-menu__buttons {
  display: flex;
  gap: 2px;
  align-items: center;
}

.popup-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.popup-btn:hover {
  background: var(--panel-2);
  color: var(--accent);
  transform: translateY(-1px);
}

.popup-btn:active {
  transform: translateY(0);
  background: var(--accent);
  color: var(--panel);
}

.popup-btn--clear {
  color: var(--danger);
  font-weight: 700;
}

.popup-btn--clear:hover {
  background: var(--danger);
  color: var(--panel);
}

/* Responsive popup for mobile */
@media (max-width: 768px) {
  .popup-menu {
    border-radius: 12px;
    padding: 6px;
  }
  
  .popup-btn {
    min-width: 40px;
    height: 40px;
    font-size: 16px;
  }
  
  .popup-menu__buttons {
    gap: 4px;
  }
}

/* Telegram Mini App mobile fix - prevent closing on swipe down */
/* Based on official docs: https://docs.telegram-mini-apps.com/platform/sticky-app */
.mobile-body {
  overflow: hidden !important;
  height: 100vh !important;
  /* Prevent iOS bounce effect */
  -webkit-overflow-scrolling: touch;
  position: fixed;
  width: 100%;
}

.mobile-wrap {
  position: absolute !important;
  left: 0 !important;
  top: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  /* Enable momentum scrolling on iOS */
  -webkit-overflow-scrolling: touch;
}

.mobile-content {
  height: calc(100% + 1px) !important;
  /* Minimum height to ensure content is scrollable */
  min-height: calc(100vh + 1px) !important;
}

/* Additional fixes for better sticky behavior */
.mobile-body,
.mobile-body * {
  /* Prevent text selection that can interfere with swipe detection */
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Re-enable text selection for editor */
.mobile-body .editor,
.mobile-body .output,
.mobile-body input,
.mobile-body textarea {
  -webkit-user-select: text !important;
  -moz-user-select: text !important;
  -ms-user-select: text !important;
  user-select: text !important;
}

/* Responsive design */
@media (max-width: 768px) {
  .app { margin: 16px auto 40px; padding: 0 12px; }
  .title { font-size: 18px; }
  
  .toolbar { 
    gap: 8px; 
    padding: 12px;
  }
  
  .toolbar button, .export__buttons button {
    padding: 12px 16px;
    font-size: 15px;
    border-radius: 14px;
    min-height: 44px;
  }
  
  .export__buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }
  .export__buttons button { width: 100%; }
  
  .editor { min-height: 280px; padding: 14px; font-size: 16px; }
  .output { min-height: 180px; font-size: 14px; }
}

@media (max-width: 480px) {
  .app { padding: 0 8px; }
  

  
  .export__buttons {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  
  .toolbar button, .export__buttons button {
    padding: 14px 18px;
    font-size: 16px;
    border-radius: 16px;
    min-height: 48px;
  }
}

/* Telegram notice */
.telegram-notice {
  margin-top: 20px;
}

.telegram-notice__content {
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: 12px;
  padding: 16px 20px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  font-family: var(--font-family);
  position: relative;
}

.telegram-notice__content::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  border-radius: 2px 0 0 2px;
}

.telegram-notice__icon {
  font-size: 18px;
  flex-shrink: 0;
  margin-top: 1px;
}

.telegram-notice__text {
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  font-weight: 400;
}

.telegram-notice__text strong {
  font-weight: 600;
  color: var(--text-primary);
}

/* Light theme adjustments */
[data-theme="light"] .telegram-notice__content {
  background: var(--bg-secondary);
  border-color: var(--border-primary);
}

[data-theme="light"] .telegram-notice__text {
  color: var(--text-primary);
}

[data-theme="light"] .telegram-notice__text strong {
  color: var(--text-primary);
}

