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

:root {
  --primary-color: #2196F3;
  --primary-dark: #1976D2;
  --secondary-color: #4CAF50;
  --danger-color: #f44336;
  --bg-color: #f5f5f5;
  --surface-color: #ffffff;
  --text-primary: #212121;
  --text-secondary: #757575;
  --border-color: #e0e0e0;
  --shadow: 0 2px 4px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 8px rgba(0,0,0,0.15);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background: var(--bg-color);
  color: var(--text-primary);
  line-height: 1.6;
  overflow-x: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-x: hidden;
}

header {
  background: var(--surface-color);
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.menu-toggle {
  background: none;
  border: none;
  padding: 0.5rem;
  cursor: pointer;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: background 0.2s;
}

.menu-toggle:hover {
  background: var(--bg-color);
}

header h1 {
  font-size: 1.1rem;
  font-weight: 500;
  color: var(--text-primary);
}

.header-controls {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.model-selector {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--surface-color);
  cursor: pointer;
  font-size: 0.9rem;
}

.icon-btn {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 4px;
  transition: background 0.2s;
}

.icon-btn:hover {
  background: var(--bg-color);
}

.main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  overflow-x: hidden;
}

.sidebar {
  width: 250px;
  background: var(--surface-color);
  border-right: 1px solid var(--border-color);
  padding: 1.5rem;
  overflow-y: auto;
}

.sidebar-section {
  margin-bottom: 2rem;
}

.sidebar-section h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 0.75rem;
  font-weight: 600;
}

.action-btn, .view-btn {
  width: 100%;
  padding: 0.75rem 1rem;
  margin-bottom: 0.5rem;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
  text-align: left;
  font-size: 0.9rem;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.action-btn:hover, .view-btn:hover {
  background: var(--bg-color);
  border-color: var(--primary-color);
  transform: translateX(2px);
}

.action-btn:active, .view-btn:active {
  transform: translateX(0);
}

.view-btn.active {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.view-btn.active:hover {
  background: var(--primary-dark);
}

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.canvas-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface-color);
  overflow: hidden;
  display: none;
  z-index: 100;
}

.canvas-container.visible {
  display: flex;
  flex-direction: column;
}

#visualCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

.canvas-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.canvas-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 0.75rem 1rem;
  background: rgba(255,255,255,0.95);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 10;
}

.canvas-header h3 {
  font-size: 0.9rem;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.close-canvas-btn {
  background: none;
  border: none;
  font-size: 1.25rem;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 0.25rem;
  border-radius: 4px;
  transition: all 0.2s;
}

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

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface-color);
  min-height: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  -webkit-overflow-scrolling: touch;
}

.message {
  display: flex;
  max-width: 100%;
  animation: fadeIn 0.3s;
  overflow: visible;
}

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

.message.user {
  align-self: flex-end;
  max-width: 85%;
}

.message-content {
  background: transparent;
  padding: 0.5rem 0;
  word-wrap: break-word;
  overflow: visible;
  max-height: none;
  line-height: 1.6;
}

.message.user .message-content {
  background: #f0f0f0;
  color: var(--text-primary);
  padding: 0.75rem 1rem;
  border-radius: 18px;
  border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
  background: transparent;
  padding: 0;
}

.message-content h2 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 1.25rem 0 0.5rem 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.message-content h2:first-child {
  margin-top: 0;
}

.message-content p {
  margin: 0.25rem 0;
}

.message-content p:first-child {
  margin-top: 0;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content ul, .message-content ol {
  margin: 0.5rem 0;
  padding-left: 1.25rem;
}

.message-content li {
  margin: 0.25rem 0;
}

.message-content a {
  color: var(--primary-color);
  text-decoration: none;
}

.message-content a:hover {
  text-decoration: underline;
}

.message-content .external-link::after {
  content: ' ↗';
  font-size: 0.85em;
}

.message-content strong {
  font-weight: 600;
}

.message-content em {
  font-style: italic;
  color: var(--text-secondary);
}

.message-content pre {
  background: rgba(0,0,0,0.05);
  padding: 0.5rem;
  border-radius: 4px;
  overflow-x: auto;
  margin: 0.5rem 0;
}

.message-content code {
  font-family: 'Courier New', monospace;
  font-size: 0.9em;
}

.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 0.5rem;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; }
  30% { opacity: 1; }
}

.chat-input-container {
  padding: 0.75rem 1rem;
  background: var(--surface-color);
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.input-action-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border-color);
  background: var(--surface-color);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text-secondary);
  transition: all 0.2s;
}

.input-action-btn:hover {
  background: var(--bg-color);
  color: var(--text-primary);
}

.input-wrapper {
  flex: 1;
  background: var(--bg-color);
  border-radius: 20px;
  padding: 0.5rem 1rem;
  display: flex;
  align-items: center;
}

#chatInput {
  flex: 1;
  padding: 0.5rem 0;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 1rem;
  resize: none;
  outline: none;
  line-height: 1.4;
  max-height: 120px;
}

#chatInput::placeholder {
  color: var(--text-secondary);
}

.send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--text-primary);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

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

.send-btn:disabled {
  background: var(--text-secondary);
  cursor: not-allowed;
}

.send-btn svg {
  margin-left: 2px;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--surface-color);
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
}

.modal-header {
  padding: 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1.25rem;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text-secondary);
}

.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  flex-shrink: 0;
  background: var(--surface-color);
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: var(--text-primary);
}

.form-group input[type="text"],
.form-group input[type="password"] {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 0.95rem;
}

.form-group input[type="checkbox"] {
  margin-right: 0.5rem;
}

.hidden {
  display: none !important;
}

.btn-primary {
  padding: 0.75rem 1.5rem;
  background: var(--primary-color);
  color: white;
  border: none;
  border-radius: 4px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

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

.btn-secondary {
  padding: 0.75rem 1.5rem;
  background: var(--surface-color);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

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

.install-prompt {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-color);
  padding: 1rem 1.5rem;
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  display: flex;
  gap: 1rem;
  align-items: center;
  z-index: 1000;
}

.install-prompt.hidden {
  display: none;
}

.menu-toggle {
  display: none;
  background: transparent;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.5rem;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 199;
}

.sidebar-overlay.active {
  display: block;
}


@media (max-width: 768px) {
  .menu-toggle {
    display: block;
  }

  header {
    padding: 0.75rem 1rem;
  }

  header h1 {
    font-size: 1.2rem;
  }

  .sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    bottom: 0;
    width: 280px;
    z-index: 200;
    transition: left 0.3s ease;
    box-shadow: var(--shadow-lg);
  }

  .sidebar.open {
    left: 0;
  }

  .main-container {
    flex-direction: column;
  }

  .content {
    height: calc(100vh - 60px);
  }

  .chat-messages {
    padding: 1rem;
    padding-bottom: calc(180px + env(safe-area-inset-bottom, 0));
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .message {
    overflow: visible;
    flex-shrink: 0;
  }

  .message-content {
    overflow: visible;
    max-height: none;
  }

  .chat-input-container {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface-color);
    padding: 0.5rem 0.75rem;
    padding-bottom: calc(0.5rem + env(safe-area-inset-bottom, 0));
    border-top: 1px solid var(--border-color);
  }

  .input-action-btn,
  .send-btn {
    width: 36px;
    height: 36px;
  }

  .input-wrapper {
    padding: 0.4rem 0.75rem;
  }

  #chatInput {
    font-size: 16px;
  }

  .message {
    max-width: 90%;
  }

  .message.user {
    max-width: 90%;
  }

  .model-selector {
    padding: 0.4rem 0.5rem;
    font-size: 0.8rem;
    max-width: 100px;
  }

  .modal-content {
    width: 95%;
    max-height: 90vh;
    overflow-y: auto;
  }

  .install-prompt {
    bottom: 80px;
    width: 90%;
    left: 5%;
    transform: none;
  }

  .canvas-header {
    padding: 0.5rem 0.75rem;
  }

  .canvas-header h3 {
    font-size: 0.8rem;
  }
}

@media (max-width: 480px) {
  header h1 {
    font-size: 1rem;
  }

  .chat-messages {
    padding: 0.75rem;
    gap: 0.75rem;
  }

  .message-content {
    padding: 0.6rem 0.8rem;
    font-size: 0.95rem;
  }
}

@media (max-width: 768px) and (orientation: landscape) {
  .chat-messages {
    padding-bottom: 70px;
  }
}

@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .chat-input-container {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
  }
}
