/* CSS Variables */
:root {
  --bg-primary: #0c1222;
  --bg-secondary: #111827;
  --bg-tertiary: #1e293b;
  --accent: #38bdf8;
  --accent-hover: #7dd3fc;
  --accent-muted: rgba(56, 189, 248, 0.15);
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --danger: #f87171;
  --danger-hover: #fca5a5;
  --border: #1e3a5f;
  --header-bg: #0f172a;
  --cell-hover: rgba(56, 189, 248, 0.08);
  --cell-selected: rgba(56, 189, 248, 0.2);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --radius: 12px;
  --radius-sm: 8px;
  --toolbar-height: 72px;
  --header-height: 56px;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'SF Mono', Consolas, monospace;
  --font-sans: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* Reset & Base */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

/* App Layout */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}

/* Header */
.header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, var(--bg-secondary) 0%, var(--bg-primary) 100%);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.logo {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--accent) 0%, #0ea5e9 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* Main Canvas Area */
.main {
  flex: 1;
  overflow: auto;
  position: relative;
  background: 
    radial-gradient(ellipse at 50% 0%, rgba(56, 189, 248, 0.06) 0%, transparent 50%),
    var(--bg-primary);
}

.canvas-container {
  min-height: 100%;
  padding: 16px;
  display: flex;
  justify-content: center;
}

#tableCanvas {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  touch-action: pan-x pan-y;
}

/* Floating Cell Input */
.cell-input {
  position: fixed;
  display: none;
  font-family: var(--font-mono);
  font-size: 14px;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 2px solid var(--accent);
  border-radius: var(--radius-sm);
  outline: none;
  z-index: 100;
  box-shadow: 0 0 0 4px var(--accent-muted), var(--shadow);
  min-width: 80px;
}

.cell-input:focus {
  background: var(--bg-secondary);
}

/* Bottom Toolbar */
.toolbar {
  height: var(--toolbar-height);
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 0 8px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  gap: 4px;
}

.toolbar-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 64px;
  height: 56px;
  padding: 8px 12px;
  background: transparent;
  color: var(--text-secondary);
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.toolbar-btn svg {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

.toolbar-btn:hover,
.toolbar-btn:active {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.toolbar-btn.primary {
  color: var(--accent);
}

.toolbar-btn.primary:hover,
.toolbar-btn.primary:active {
  background: var(--accent-muted);
  color: var(--accent-hover);
}

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

.toolbar-btn.danger:hover,
.toolbar-btn.danger:active {
  background: rgba(248, 113, 113, 0.15);
  color: var(--danger-hover);
}

.toolbar-btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}

/* Toast Notification */
.toast {
  position: fixed;
  bottom: calc(var(--toolbar-height) + 16px);
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  padding: 12px 24px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 1000;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast.success {
  border-color: var(--accent);
  background: linear-gradient(135deg, var(--bg-tertiary) 0%, var(--accent-muted) 100%);
}

.toast.error {
  border-color: var(--danger);
  background: linear-gradient(135deg, var(--bg-tertiary) 0%, rgba(255, 71, 87, 0.15) 100%);
}

/* Scrollbar Styling */
.main::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.main::-webkit-scrollbar-track {
  background: var(--bg-primary);
}

.main::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

.main::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Media Queries for larger screens */
@media (min-width: 768px) {
  .toolbar {
    justify-content: center;
    gap: 16px;
  }
  
  .toolbar-btn {
    flex-direction: row;
    min-width: auto;
    height: 44px;
    padding: 8px 20px;
    gap: 8px;
    font-size: 13px;
  }
  
  .toolbar-btn svg {
    width: 18px;
    height: 18px;
  }
  
  .canvas-container {
    padding: 24px;
  }
}

/* Safe area support for notched phones */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .toolbar {
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(var(--toolbar-height) + env(safe-area-inset-bottom));
  }
}

/* Loading state animation */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.loading {
  animation: pulse 1.5s ease-in-out infinite;
}

/* Turnstile CAPTCHA container */
.turnstile-container {
  position: fixed;
  bottom: calc(var(--toolbar-height) + 80px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: none;
}

.turnstile-container.show {
  display: block;
}

/* Modal / Bottom Sheet */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 2000;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  display: none;
}

.modal-overlay.show {
  display: flex;
  opacity: 1;
  animation: fadeIn 0.2s ease forwards;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-sheet {
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  max-height: 85dvh;
  background: var(--bg-secondary);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transition: transform 0.2s ease, max-height 0.2s ease;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.modal-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.modal-close {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
}

.modal-close:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.modal-close svg {
  width: 20px;
  height: 20px;
}

/* Form Styles */
.modal-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  position: relative;
}

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 15px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-input::placeholder {
  color: var(--text-muted);
}

.form-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-muted);
}

.form-textarea {
  resize: vertical;
  min-height: 80px;
}

.char-count {
  position: absolute;
  right: 8px;
  bottom: 8px;
  font-size: 11px;
  color: var(--text-muted);
  pointer-events: none;
}

.char-count.warning {
  color: #f59e0b;
}

.char-count.limit {
  color: var(--danger);
}

/* Modal Actions */
.modal-actions {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}

.btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn svg {
  width: 18px;
  height: 18px;
}

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.btn-secondary:hover {
  background: var(--border);
  color: var(--text-primary);
}

.btn-primary {
  background: var(--accent);
  color: var(--bg-primary);
}

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

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Desktop modal positioning */
@media (min-width: 768px) {
  .modal-overlay {
    align-items: center;
  }
  
  .modal-sheet {
    border-radius: var(--radius);
    margin: 20px;
  }
}

