/* -----------------------------------------------------------------------------
   SimpleDraw — Text Tool styles (textTool.css)
   Touch-optimized version with enhanced compatibility
   -------------------------------------------------------------------------- */

/* Load fonts used in the family list (safe if repeated) */
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&family=Lexend:wght@400;700&family=Patrick+Hand&display=swap");

.toolctx-text {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  /* TOUCH ENHANCEMENT: Safe area padding for notched devices */
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.toolctx-text .sel {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.toolctx-text .label {
  font: 500 0.875rem/1.1 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--ui-muted, #475569);
}

/* TOUCH ENHANCEMENT: Increased minimum height for better touch targets */
.toolctx-text .btn,
.toolctx-text .input,
.toolctx-text .select {
  height: 2.8rem;
  min-height: 44px; /* iOS minimum touch target recommendation */
  border-radius: 10px;
  border: 1px solid var(--ui-border, #e5e7eb);
  background: var(--ui-bg, #fff);
  color: var(--ui-fg, #111827);
  padding: 0 0.5rem;
  box-sizing: border-box;
  /* TOUCH ENHANCEMENT: Prevent text selection on buttons */
  -webkit-user-select: none;
  user-select: none;
  /* TOUCH ENHANCEMENT: Prevent tap highlight on mobile */
  -webkit-tap-highlight-color: transparent;
  /* TOUCH ENHANCEMENT: Improve touch responsiveness */
  touch-action: manipulation;
}

.toolctx-text .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-width: 2.8rem;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease, box-shadow 120ms ease, transform 80ms ease;
  user-select: none;
  /* TOUCH ENHANCEMENT: Prevent double-tap zoom */
  touch-action: manipulation;
}

.toolctx-text .btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  /* TOUCH ENHANCEMENT: Prevent interaction on disabled buttons */
  pointer-events: none;
}

/* TOUCH ENHANCEMENT: Hover states for devices that support hover */
@media (hover: hover) and (pointer: fine) {
  .toolctx-text .btn:hover:not(:disabled) {
    background: var(--ui-hover, #f8fafc);
    border-color: var(--ui-border-strong, #cbd5e1);
  }
}

/* TOUCH ENHANCEMENT: Add active state for better touch feedback */
.toolctx-text .btn:active:not(:disabled) {
  transform: scale(0.95);
  background: var(--ui-hover, #f0f0f0);
}

/* TOUCH ENHANCEMENT: Increased input width for easier typing on mobile */
.toolctx-text .input {
  width: 8rem;
  /* VIRTUAL KEYBOARD: Prevent zoom on iOS when focusing inputs */
  font-size: 16px;
}

.toolctx-text .select {
  width: 10.5rem;
  padding-right: 2rem;
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 1.1rem) 1rem, calc(100% - 0.75rem) 1rem;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  /* VIRTUAL KEYBOARD: Prevent zoom on iOS */
  font-size: 16px;
}

/* Font family preview */
.toolctx-text .select option { font-size: 0.95rem; }
.toolctx-text .select option[data-font="Poppins"] { font-family: "Poppins", system-ui, -apple-system, sans-serif; }
.toolctx-text .select option[data-font="Lexend"] { font-family: "Lexend", system-ui, -apple-system, sans-serif; }
.toolctx-text .select option[data-font="Patrick Hand"] { font-family: "Patrick Hand", system-ui, -apple-system, sans-serif; }

/* Numeric inputs */
.toolctx-text .num { 
  width: 5rem;
  text-align: right;
  /* VIRTUAL KEYBOARD: Prevent zoom on iOS */
  font-size: 16px;
}

/* Toggle buttons */
.toolctx-text .toggle { 
  width: 2.8rem;
  min-width: 44px;
}

.toolctx-text .toggle.on,
.toolctx-text .btn.on,
.toolctx-text .btn[aria-pressed="true"] {
  background: var(--ui-accent, #dbeafe);
  border-color: var(--ui-accent-strong, #60a5fa);
  color: var(--ui-accent-text, #1d4ed8);
}

/* Color swatch button */
.toolctx-text .swatch {
  width: 2.8rem;
  min-width: 44px;
  position: relative;
  overflow: hidden;
}
.toolctx-text .swatch::after {
  content: "";
  position: absolute;
  inset: 0.35rem;
  border-radius: 6px;
  background: var(--swatch, #111827);
}

/* Alignment group */
.toolctx-text .align-controls {
  display: inline-flex;
  gap: 0.6rem; /* Increased for better touch spacing */
}

/* TOUCH ENHANCEMENT: Larger alignment glyphs for better visibility */
.toolctx-text .align-glyph {
  width: 2.2rem;
  height: 2.2rem;
  border: 1.5px solid var(--ui-border, #cbd5e1);
  border-radius: 10px;
  background: #fff;
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.24rem;
  padding: 0.14rem 0.18rem;
  box-sizing: border-box;
  pointer-events: none;
  color: currentColor;
}

/* Thicker lines for better visibility on mobile */
.toolctx-text .align-glyph .line {
  height: 3.5px;
  background: currentColor;
  border-radius: 3px;
  display: block;
}

/* Left align: lines start at left, varying lengths */
.toolctx-text .align-glyph.left .line {
  margin-left: 0;
  margin-right: auto;
}
.toolctx-text .align-glyph.left .line:nth-child(1) { width: 90%; }
.toolctx-text .align-glyph.left .line:nth-child(2) { width: 75%; }
.toolctx-text .align-glyph.left .line:nth-child(3) { width: 85%; }

/* Center align: lines centered with varying lengths */
.toolctx-text .align-glyph.center .line {
  margin-left: auto;
  margin-right: auto;
}
.toolctx-text .align-glyph.center .line:nth-child(1) { width: 85%; }
.toolctx-text .align-glyph.center .line:nth-child(2) { width: 70%; }
.toolctx-text .align-glyph.center .line:nth-child(3) { width: 90%; }

/* Right align: lines end at right, varying lengths */
.toolctx-text .align-glyph.right .line {
  margin-left: auto;
  margin-right: 0;
}
.toolctx-text .align-glyph.right .line:nth-child(1) { width: 85%; }
.toolctx-text .align-glyph.right .line:nth-child(2) { width: 70%; }
.toolctx-text .align-glyph.right .line:nth-child(3) { width: 90%; }

/* Selected state tint for the glyph box */
.toolctx-text .align-btn.on .align-glyph,
.toolctx-text .align-btn[aria-pressed="true"] .align-glyph {
  border-color: var(--ui-accent-strong, #60a5fa);
  background: var(--ui-accent, #dbeafe);
}

/* TOUCH ENHANCEMENT: Improved alignment button sizing */
.toolctx-text .align-btn {
  min-width: 2.8rem;
  width: 2.8rem;
  height: 2.8rem;
  min-height: 44px;
  font-size: 1rem;
  transition: all 0.15s ease;
  padding: 0;
  touch-action: manipulation;
}

@media (hover: hover) and (pointer: fine) {
  .toolctx-text .align-btn:hover {
    background: var(--ui-hover, #f3f4f6);
    border-color: var(--ui-border-strong, #cbd5e1);
  }
}

/* TOUCH ENHANCEMENT: Better active state for touch feedback */
.toolctx-text .align-btn:active {
  transform: scale(0.95);
}

.toolctx-text .align-btn.on,
.toolctx-text .align-btn[aria-pressed="true"] {
  background: var(--ui-accent, #dbeafe);
  border-color: var(--ui-accent-strong, #60a5fa);
  color: var(--ui-accent-text, #1d4ed8);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* TOUCH ENHANCEMENT: Larger focus outline for better visibility */
.toolctx-text .align-btn:focus-visible {
  outline: 3px solid var(--ui-focus, #3b82f6);
  outline-offset: 2px;
}

/* Text decoration controls */
.toolctx-text .dec-controls { 
  display: inline-flex; 
  gap: 0.6rem; /* Increased for touch */
}
.toolctx-text .dec-controls .btn { 
  width: 2.8rem;
  min-width: 2.8rem;
  min-height: 44px;
}

/* Simple typographic glyphs */
.toolctx-text .ico-i,
.toolctx-text .ico-u,
.toolctx-text .ico-s {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-weight: 700;
  font-size: 1.1rem;
  line-height: 1;
  user-select: none;
  pointer-events: none;
}
.toolctx-text .ico-i::before { content: "I"; font-style: italic; }
.toolctx-text .ico-u::before { content: "U"; text-decoration: underline; text-underline-offset: 2px; }
.toolctx-text .ico-s::before { content: "S"; text-decoration: line-through; text-decoration-thickness: 2px; }

/* Divider dots between groups */
.toolctx-text .dot {
  width: 4px;
  height: 4px;
  background: #cbd5e1;
  border-radius: 999px;
  display: inline-block;
}

/* TOUCH ENHANCEMENT: Responsive adjustments optimized for touch */
@media (max-width: 980px) {
  .toolctx-text { gap: 0.6rem; }
  .toolctx-text .label { display: none; }
  .toolctx-text .select { width: 9.5rem; }
  .toolctx-text .input { width: 6.5rem; }
  .toolctx-text .align-controls { gap: 0.5rem; }
}

@media (max-width: 720px) {
  .toolctx-text { gap: 0.5rem; }
  .toolctx-text .select { width: 8.5rem; }
  .toolctx-text .input { width: 5.5rem; }
  .toolctx-text .btn,
  .toolctx-text .select,
  .toolctx-text .input {
    height: 2.6rem;
    min-height: 44px;
    border-radius: 9px;
    font-size: 16px;
  }
  .toolctx-text .align-btn {
    width: 2.6rem;
    min-width: 2.6rem;
    height: 2.6rem;
    min-height: 44px;
  }
  .toolctx-text .align-glyph {
    width: 2.0rem; 
    height: 2.0rem;
  }
  .toolctx-text .align-controls { gap: 0.4rem; }
}

@media (max-width: 560px) {
  .toolctx-text { gap: 0.45rem; }
  .toolctx-text .select { width: 7.5rem; }
  .toolctx-text .input { width: 5rem; }
  .toolctx-text .btn,
  .toolctx-text .select,
  .toolctx-text .input {
    height: 2.5rem;
    min-height: 44px;
    border-radius: 8px;
    font-size: 16px;
  }
  .toolctx-text .align-btn {
    width: 2.5rem;
    min-width: 2.5rem;
    height: 2.5rem;
    min-height: 44px;
    font-size: 0.95rem;
  }
  .toolctx-text .align-glyph {
    width: 1.9rem; 
    height: 1.9rem;
  }
  .toolctx-text .align-controls { gap: 0.35rem; }
}

/* High-contrast mode */
@media (forced-colors: active) {
  .toolctx-text .btn,
  .toolctx-text .select,
  .toolctx-text .input {
    border: 1px solid ButtonText;
    background: Canvas;
    color: ButtonText;
  }
  .toolctx-text .btn:hover {
    background: CanvasText;
    color: Canvas;
  }
  .toolctx-text .align-btn.on,
  .toolctx-text .align-btn[aria-pressed="true"] {
    border-width: 3px;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .toolctx-text .align-btn { transition: none; }
  .toolctx-text .btn { transition: none; }
}

/* TOUCH ENHANCEMENT: Custom dropdown styles optimized for touch */
.custom-font-dropdown {
  position: relative;
  min-width: 200px;
  max-width: 300px;
}

.custom-select-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px; /* Increased padding for touch */
  min-height: 44px;
  background: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  font-size: 16px; /* VIRTUAL KEYBOARD: Prevent zoom */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

@media (hover: hover) and (pointer: fine) {
  .custom-select-trigger:hover {
    border-color: #999;
    background: #f9f9f9;
  }
}

/* TOUCH ENHANCEMENT: Better active state */
.custom-select-trigger:active {
  background: #f0f0f0;
  transform: scale(0.98);
}

.custom-select-trigger:focus-visible {
  outline: 3px solid #0066cc;
  outline-offset: 2px;
}

/* Focus indicator for keyboard-navigable font options (WCAG 2.4.11) */
.custom-font-option:focus-visible {
  outline: 3px solid #0066cc;
  outline-offset: -3px;
  background-color: #e8f0fe;
}

.selected-font-display {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dropdown-arrow {
  margin-left: 8px;
  font-size: 14px; /* Larger for touch */
  color: #666;
  transition: transform 0.2s;
}

.custom-select-trigger[aria-expanded="true"] .dropdown-arrow {
  transform: rotate(180deg);
}

.custom-options-container {
  position: absolute;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  z-index: 1000;
  max-height: 400px;
  overflow: hidden;
}

.custom-options-scroll {
  max-height: 400px;
  overflow-y: auto;
  padding: 4px 0;
  /* TOUCH ENHANCEMENT: Smooth scrolling on touch devices */
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* TOUCH ENHANCEMENT: Larger touch targets for font options */
.custom-font-option {
  padding: 16px 18px; /* Increased from 14px */
  min-height: 48px; /* Increased from 44px */
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.1s;
  display: flex;
  align-items: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

@media (hover: hover) and (pointer: fine) {
  .custom-font-option:hover {
    background-color: #e8f0fe;
  }
}

/* TOUCH ENHANCEMENT: Better active state */
.custom-font-option:active {
  background-color: #d0e4ff;
}

.custom-font-option.selected {
  background-color: #d0e4ff;
  font-weight: 500;
}

.custom-font-option.separator {
  padding: 10px 18px;
  min-height: auto;
  font-size: 12px;
  font-weight: 600;
  color: #666;
  background: #f5f5f5;
  cursor: default;
  font-style: italic;
  font-family: system-ui, sans-serif !important;
}

.custom-font-option.separator:hover {
  background: #f5f5f5;
}

.custom-font-option.manual-add {
  color: #0066cc;
  font-style: italic;
  font-family: system-ui, sans-serif !important;
  border-top: 1px solid #ddd;
  margin-top: 4px;
}

/* TOUCH ENHANCEMENT: Larger scrollbar for touch devices */
.custom-options-scroll::-webkit-scrollbar {
  width: 14px; /* Increased from 12px */
}

.custom-options-scroll::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.custom-options-scroll::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 7px;
  min-height: 48px; /* Minimum thumb size for touch */
  border: 3px solid #f1f1f1; /* Padding around thumb */
}

.custom-options-scroll::-webkit-scrollbar-thumb:hover {
  background: #555;
}

/* TOUCH ENHANCEMENT: Screen reader only class for accessibility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* VIRTUAL KEYBOARD: Hidden input bridge - completely invisible but functional */
#sd-text-keyboard-bridge {
  position: absolute !important;
  left: -9999px !important;
  width: 1px !important;
  height: 1px !important;
  opacity: 0.01 !important;
  pointer-events: none !important;
  font-size: 16px !important; /* Prevent zoom on iOS */
  border: none !important;
  padding: 0 !important;
  margin: 0 !important;
  z-index: -1 !important;
}

/* VIRTUAL KEYBOARD: Adjust layout when keyboard is visible */
@supports (height: 100dvh) {
  /* Use dynamic viewport units if available */
  .toolctx-text {
    max-height: 100dvh;
  }
}

/* VIRTUAL KEYBOARD: Smooth transitions for keyboard appearance */
@media (max-width: 720px) {
  body {
    transition: padding-bottom 0.3s ease;
  }
  
  /* Add padding when virtual keyboard might be visible */
  body.virtual-keyboard-visible {
    padding-bottom: 40vh;
  }
}

/* TOUCH ENHANCEMENT: Improve caret visibility on touch devices */
.text-caret {
  pointer-events: none;
  /* Ensure caret is always visible above other elements */
  z-index: 1000;
}

/* TOUCH ENHANCEMENT: Better visual feedback for text editing on touch */
@media (pointer: coarse) {
  .text-caret {
    stroke-width: 2.5px !important;
    stroke: #0066cc !important;
  }
}

/* VIRTUAL KEYBOARD: Ensure text remains visible when keyboard shows */
@supports (height: 100dvh) {
  #stage {
    min-height: 100dvh;
    max-height: 100dvh;
  }
}

/* TOUCH ENHANCEMENT: Optimize font dropdown for touch on small screens */
@media (max-width: 560px) {
  .custom-font-dropdown {
    min-width: 150px;
    max-width: 250px;
  }
  
  .custom-select-trigger {
    padding: 10px 12px;
    font-size: 15px;
  }
  
  .custom-font-option {
    padding: 14px 16px;
    min-height: 44px;
    font-size: 15px;
  }
}

/* TOUCH ENHANCEMENT: Improve touch targets on very small screens */
@media (max-width: 400px) {
  .toolctx-text .btn,
  .toolctx-text .select,
  .toolctx-text .input {
    min-height: 48px; /* Larger for very small screens */
  }
  
  .toolctx-text .align-btn {
    min-height: 48px;
    width: 2.8rem;
    min-width: 2.8rem;
  }
}