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

html, body {
  background: var(--bg-page);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 15px;
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#app {
  display: grid;
  grid-template:
    "topbar   topbar"   var(--topbar-height)
    "sidebar  canvas"   1fr
    "prompt   prompt"   auto
    "actions  actions"  auto
    / var(--sidebar-width) 1fr;
  height: 100vh;
  gap: 0;
}

/* Top Bar */
.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--gap);
  background: var(--bg-base);
  border-bottom: 1px solid #2a2232;
  z-index: 10;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: var(--gap);
}

.topbar-logo {
  font-size: var(--fs-lg);
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.topbar-logo span {
  color: var(--text-secondary);
  font-weight: 400;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: var(--gap-sm);
}

.topbar-select {
  appearance: none;
  background: var(--bg-inset) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23999' stroke-width='1.2' fill='none'/%3E%3C/svg%3E") no-repeat right 10px center;
  border: none;
  border-radius: var(--control-radius-sm);
  box-shadow: var(--shadow-inset-sm);
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: var(--fs-xs);
  padding: 6px 28px 6px 10px;
  outline: none;
  cursor: pointer;
}

.topbar-select option {
  background: var(--bg-base);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: var(--gap);
}

.session-badge {
  display: none;
  align-items: center;
  gap: 6px;
  background: var(--accent-subtle);
  border: 1px solid var(--accent-glow);
  border-radius: 20px;
  color: var(--accent);
  font-size: var(--fs-xs);
  padding: 4px 12px;
}

.session-badge.active {
  display: flex;
}

.session-dot {
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.session-end {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0 2px;
  opacity: 0.6;
}

.session-end:hover {
  opacity: 1;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* Result action buttons */
.result-actions {
  display: flex;
  gap: var(--gap-sm);
}

.result-action-btn {
  font-size: var(--fs-xs) !important;
  padding: 6px 12px !important;
}

/* Loading timer */
.generating-overlay {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--gap);
  background: var(--bg-inset);
  border-radius: var(--control-radius);
  box-shadow: var(--shadow-inset);
  min-height: 200px;
  width: 100%;
  height: 100%;
}

.generating-timer {
  font-family: var(--font-mono);
  font-size: var(--fs-lg);
  color: var(--accent);
}

.generating-text {
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

/* Sidebar */
.sidebar {
  grid-area: sidebar;
  display: flex;
  flex-direction: column;
  background: var(--bg-base);
  border-right: 1px solid #2a2232;
  overflow: hidden;
}

.sidebar-tabs {
  padding: var(--gap-sm) var(--gap-sm) 0;
  flex-shrink: 0;
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--gap);
}

/* Canvas Area */
.canvas-area {
  grid-area: canvas;
  display: flex;
  flex-direction: column;
  padding: var(--gap);
  overflow: hidden;
}

.canvas-main {
  flex: 1;
  display: flex;
  gap: var(--gap);
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.canvas-image-wrap {
  flex: 1;
  max-width: 50%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--gap-sm);
}

.canvas-image-wrap img {
  max-width: 100%;
  max-height: calc(100% - 24px);
  object-fit: contain;
  border-radius: var(--control-radius);
  box-shadow: var(--shadow-raised);
}

.canvas-label {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* Upload Zone */
.upload-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  border: 2px dashed var(--text-muted);
  border-radius: var(--panel-radius);
  cursor: pointer;
  gap: var(--gap);
  transition: all var(--transition);
  padding: 40px;
}

.upload-zone:hover {
  border-color: var(--accent);
  background: var(--accent-subtle);
}

.upload-zone.dragging {
  border-color: var(--accent);
  background: var(--accent-subtle);
  box-shadow: 0 0 30px var(--accent-glow);
}

.upload-zone-icon {
  font-size: 48px;
  color: var(--text-muted);
  transition: color var(--transition);
}

.upload-zone:hover .upload-zone-icon {
  color: var(--accent);
}

.upload-zone-text {
  color: var(--text-secondary);
  font-size: var(--fs-base);
  text-align: center;
}

.upload-zone-text strong {
  color: var(--accent);
}

.upload-zone-hint {
  color: var(--text-muted);
  font-size: var(--fs-xs);
}

.upload-zone input[type="file"] {
  display: none;
}

/* History filmstrip */
.history-strip {
  display: flex;
  gap: var(--gap-sm);
  padding: var(--gap-sm) 0;
  overflow-x: auto;
  flex-shrink: 0;
  min-height: 72px;
}

.history-thumb {
  width: 56px;
  height: 56px;
  border-radius: var(--control-radius-sm);
  box-shadow: var(--shadow-raised-sm);
  cursor: pointer;
  flex-shrink: 0;
  object-fit: cover;
  opacity: 0.6;
  transition: all var(--transition);
}

.history-thumb:hover {
  opacity: 0.9;
  transform: scale(1.05);
}

.history-thumb.active {
  opacity: 1;
  box-shadow: 0 0 0 2px var(--accent), var(--shadow-raised-sm);
}

/* Action Bar (always visible) */
.action-bar {
  grid-area: actions;
  display: flex;
  align-items: center;
  gap: var(--gap);
  padding: var(--gap-sm) var(--gap);
  background: var(--bg-base);
  border-top: 1px solid #2a2232;
}

/* Prompt Bar (togglable) */
.prompt-bar {
  grid-area: prompt;
  display: flex;
  align-items: center;
  gap: var(--gap);
  padding: var(--gap-sm) var(--gap);
  background: var(--bg-deep);
  border-top: 1px solid #2a2232;
}

.prompt-bar.hidden {
  display: none !important;
}

.prompt-bar-input {
  flex: 1;
}

.prompt-bar textarea {
  background: var(--bg-inset);
  border: none;
  border-radius: var(--control-radius-sm);
  box-shadow: var(--shadow-inset-sm);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  height: 40px;
  outline: none;
  padding: 10px 14px;
  resize: none;
  transition: all var(--transition);
  width: 100%;
}

.prompt-bar textarea:focus {
  box-shadow: var(--shadow-inset-sm), 0 0 0 2px var(--accent-glow);
  height: 100px;
}

.prompt-bar textarea::placeholder {
  color: var(--text-muted);
}

.prompt-bar-actions {
  display: flex;
  gap: var(--gap-sm);
  flex-shrink: 0;
}

/* Control groups */
.control-group {
  margin-bottom: var(--gap);
}

.control-group-label {
  color: var(--text-secondary);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.5px;
  margin-bottom: var(--gap-sm);
  text-transform: uppercase;
}

.control-row {
  display: flex;
  align-items: center;
  gap: var(--gap-sm);
  margin-bottom: var(--gap-sm);
}

.control-label {
  color: var(--text-secondary);
  font-size: var(--fs-sm);
  min-width: 80px;
  flex-shrink: 0;
}

.control-value {
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  min-width: 50px;
  text-align: right;
}

/* Object card */
.object-card {
  background: var(--bg-base);
  border-radius: var(--control-radius);
  box-shadow: var(--shadow-raised-sm);
  margin-bottom: var(--gap);
  padding: var(--gap);
  transition: all var(--transition);
}

.object-card:hover {
  box-shadow: var(--shadow-raised);
}

.object-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--gap-sm);
}

.object-card-name {
  font-size: var(--fs-base);
  font-weight: 600;
  color: var(--text-primary);
}

.object-card-pos {
  font-size: var(--fs-xs);
  color: var(--text-muted);
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: 80px;
  right: var(--gap);
  display: flex;
  flex-direction: column;
  gap: var(--gap-sm);
  z-index: 1000;
}

.toast {
  background: var(--bg-raised);
  border-radius: var(--control-radius-sm);
  box-shadow: var(--shadow-raised);
  color: var(--text-primary);
  font-size: var(--fs-sm);
  padding: 12px 20px;
  animation: slideIn 0.3s ease;
  max-width: 360px;
  border-left: 3px solid var(--info);
}

.toast.error { border-left-color: var(--error); }
.toast.success { border-left-color: var(--success); }
.toast.warning { border-left-color: var(--warning); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Loading spinner */
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--bg-inset);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Footer dedication */
.footer-dedication {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  flex: 1;
  letter-spacing: 0.3px;
}

.footer-dedication ❤️ {
  color: var(--accent);
}

/* Login Overlay */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-page);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.login-overlay.hidden {
  display: none !important;
}

.login-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 48px 40px;
  width: 360px;
  text-align: center;
}

.login-logo {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.login-logo span {
  color: var(--text-secondary);
  font-weight: 400;
}

.login-subtitle {
  color: var(--text-muted);
  font-size: var(--fs-sm);
  margin-top: -10px;
}

.login-input {
  text-align: center;
  font-size: var(--fs-base) !important;
  padding: 14px !important;
}

.login-btn {
  width: 100%;
  padding: 14px !important;
  font-size: var(--fs-base) !important;
}

.login-error {
  color: var(--error);
  font-size: var(--fs-sm);
}

/* Utility */
.hidden { display: none !important; }
.flex-center { display: flex; align-items: center; justify-content: center; }
