:root {
  --fg: #16161f;
  --muted: #6b6b7a;
  --border: #e4e4ec;
  --bg: #ffffff;
  --bg-subtle: #f7f7fb;
  --bg-app: #f4f4fa;
  --accent: #5b4fe8;
  --accent-dark: #4638d6;
  --accent-contrast: #ffffff;
  --accent-soft: #eeecfd;
  --danger: #d9433f;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(22, 22, 31, 0.06);
  --shadow-md: 0 8px 24px rgba(22, 22, 31, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--fg);
  background: var(--bg-app);
}

#app {
  min-height: 100vh;
}

button {
  font-family: inherit;
  font-size: 0.95rem;
}

.icon {
  width: 1em;
  height: 1em;
  flex-shrink: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s, box-shadow 0.15s, transform 0.1s;
}

.btn:hover:not(:disabled) {
  box-shadow: var(--shadow-sm);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  border-color: transparent;
  color: var(--accent-contrast);
}

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

.btn-link {
  border: none;
  background: none;
  color: var(--accent-dark);
  padding: 0.3rem 0.4rem;
  text-decoration: none;
}

.btn-link:hover:not(:disabled) {
  text-decoration: underline;
  box-shadow: none;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.2rem;
  height: 2.2rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover:not(:disabled) {
  background: var(--bg-subtle);
  color: var(--fg);
}

.error {
  color: var(--danger);
  min-height: 1.2em;
  margin: 0.4rem 0;
  font-size: 0.9rem;
}

.hidden {
  display: none !important;
}

/* --- Auth view --- */

.view-auth {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
  background: radial-gradient(circle at 20% 20%, var(--accent-soft), transparent 60%),
    var(--bg-app);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  padding: 2.25rem;
  background: var(--bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}

.auth-card h1 {
  font-size: 1.4rem;
  font-weight: 800;
  margin: 0 0 1.25rem;
}

.auth-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.auth-tabs .tab {
  flex: 1;
  padding: 0.5rem;
  border: none;
  background: none;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font-weight: 500;
}

.auth-tabs .tab.active {
  color: var(--accent-dark);
  border-bottom-color: var(--accent);
  font-weight: 700;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 500;
}

.auth-form input {
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 1rem;
}

.auth-form input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* --- Projects view --- */

.view-projects {
  max-width: 1000px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem;
}

.projects-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 2rem;
}

.app-title {
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  margin: 0;
}

.app-subtitle {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

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

.new-project-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.new-project-form input {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.new-project-form .error {
  width: 100%;
}

.project-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1.25rem;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 4rem 1rem;
  color: var(--muted);
}

.project-card {
  background: var(--bg);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.15s, transform 0.15s;
}

.project-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.project-thumbnail {
  position: relative;
  height: 110px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.thumbnail-icon {
  width: 2.25rem;
  height: 2.25rem;
  color: rgba(22, 22, 31, 0.35);
}

.thumbnail-delete {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 1.9rem;
  height: 1.9rem;
  background: rgba(255, 255, 255, 0.85);
  border: none;
  opacity: 0;
  transition: opacity 0.15s;
}

.project-thumbnail:hover .thumbnail-delete {
  opacity: 1;
}

.thumbnail-delete:hover:not(:disabled) {
  color: var(--danger);
}

.project-card-body {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.project-card h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.project-status {
  font-size: 0.8rem;
  color: var(--accent-dark);
  margin: 0;
  font-weight: 600;
}

.project-status-unpublished {
  color: var(--muted);
  font-weight: 500;
}

.project-card-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

/* --- Editor view --- */

.view-editor {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.25rem;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  z-index: 2;
}

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

.editor-title {
  font-size: 1.15rem;
  font-weight: 700;
  margin: 0;
}

.editor-header-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  text-align: right;
}

/* --- Share popover --- */

.share-popover-wrap {
  position: relative;
}

.popover {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  width: 280px;
  background: var(--bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 1rem;
  text-align: left;
  z-index: 10;
}

.popover-close {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  width: 1.8rem;
  height: 1.8rem;
  border: none;
}

.popover-label {
  margin: 0 0 0.3rem;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.popover-url {
  margin: 0 0 0.75rem;
  font-size: 0.85rem;
  word-break: break-all;
}

.popover-hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.popover .btn {
  width: 100%;
  justify-content: center;
}

.editor-body {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(300px, 1fr) 2fr;
  min-height: 0;
}

.chat-pane,
.preview-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-pane {
  background: var(--bg);
  padding: 1rem;
  gap: 0.6rem;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding-bottom: 0.5rem;
}

.message {
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius);
  max-width: 90%;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.9rem;
  animation: message-in 0.15s ease-out;
}

@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message-user {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: var(--accent-contrast);
}

.message-assistant {
  align-self: flex-start;
  background: var(--bg-subtle);
}

.message-loading {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.75rem 0.95rem;
}

.loading-dot {
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  background: var(--muted);
  animation: loading-bounce 1.1s ease-in-out infinite;
}

.loading-dot:nth-child(2) {
  animation-delay: 0.15s;
}

.loading-dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes loading-bounce {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* The composer is a single card: textarea on top, a toolbar row (style
   preset + send) below it -- not a side-by-side input+button row. */
.chat-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.65rem;
}

.chat-input {
  width: 100%;
  resize: vertical;
  min-height: 4.5rem;
  padding: 0.5rem 0.6rem;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 0.92rem;
}

.chat-input:focus {
  outline: none;
}

.chat-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.style-select {
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
}

.style-select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.chat-toolbar .btn {
  margin-left: auto;
}

/* --- Preview pane: a gray "device" background so the browser-chrome frame
   around the iframe visually stands out from the rest of the editor. --- */

.preview-pane {
  background: #e6e6ee;
  padding: 1rem;
  gap: 0;
}

.browser-chrome {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
}

.browser-dots {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

.browser-dot {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
}

.browser-dot-red {
  background: #ff5f57;
}

.browser-dot-yellow {
  background: #febc2e;
}

.browser-dot-green {
  background: #28c840;
}

.address-bar {
  flex: 1;
  background: var(--bg-subtle);
  border-radius: 999px;
  padding: 0.3rem 0.9rem;
  overflow: hidden;
}

.address-bar-text {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.preview-iframe {
  flex: 1;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  background: #fff;
}

/* Mobile tab toggle: hidden entirely on wide viewports, where chat and
   preview show side by side via .editor-body's grid. */
.mobile-tabs {
  display: none;
}

/* --- Mobile breakpoint --- */
@media (max-width: 768px) {
  .editor-body {
    grid-template-columns: 1fr;
  }

  .mobile-tabs {
    display: flex;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
  }

  .mobile-tab-btn {
    flex: 1;
    padding: 0.6rem;
    border: none;
    background: var(--bg);
    color: var(--muted);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    font-weight: 500;
  }

  .mobile-tab-btn.active {
    color: var(--accent-dark);
    border-bottom-color: var(--accent);
    font-weight: 700;
  }

  .chat-pane,
  .preview-pane {
    display: none;
  }

  .chat-pane.active-pane,
  .preview-pane.active-pane {
    display: flex;
  }

  .popover {
    right: auto;
    left: 50%;
    transform: translateX(-50%);
  }
}
