* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  background: #f5f5f5;
  font-family: system-ui, -apple-system, sans-serif;
  color: #333;
}

/* ── Navigation ── */

.top-nav {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0 1.5rem;
  height: 52px;
  background: #fff;
  border-bottom: 2px solid #eee;
}

/* Calcemy brand colours */
.brand-calc { color: #009688; }
.brand-emy { color: #f7941d; }

.nav-brand {
  font-weight: 700;
  font-size: 1.1rem;
  margin-right: 1rem;
}

.nav-link {
  text-decoration: none;
  color: #666;
  font-weight: 500;
  font-size: 0.9rem;
  padding: 0.4rem 0;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

.nav-link:hover {
  color: #333;
}

.nav-link.active {
  color: #007bff;
  border-bottom-color: #007bff;
}

/* ── Main content ── */

#app {
  max-width: 1440px;
  margin: 1.5rem auto;
  padding: 0 1.5rem;
}

/* ── Notifications ── */

.notification {
  position: fixed;
  top: 64px;
  right: 1.5rem;
  min-width: 280px;
  max-width: 420px;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  z-index: 1000;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.notification.success {
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.notification.error {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.hidden {
  display: none !important;
}

/* ── Loading indicator ── */

.loading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 900;
}

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid #ddd;
  border-top-color: #007bff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* ── Tables (config list, template list) ── */

.config-table,
.template-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.config-table th,
.config-table td,
.template-table th,
.template-table td {
  text-align: left;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid #eee;
  font-size: 0.9rem;
}

.config-table th,
.template-table th {
  background: #f8f9fa;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #555;
}

.config-table tr:last-child td,
.template-table tr:last-child td {
  border-bottom: none;
}

.config-table tbody tr:hover,
.template-table tbody tr:hover {
  background: #f8f9fa;
  cursor: pointer;
}

/* ── Badges ── */

.badge-default {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #007bff;
  color: #fff;
}

.badge-draft {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #ffc107;
  color: #856404;
}

.badge-active {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #28a745;
  color: #fff;
}

.badge-archived {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #6c757d;
  color: #fff;
}

/* ── Forms ── */

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

.form-label {
  display: block;
  font-weight: 600;
  font-size: 0.875rem;
  margin-bottom: 0.35rem;
}

.form-input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 2px solid #ddd;
  border-radius: 8px;
  font-size: 0.95rem;
  background: #fff;
}

.form-input:focus {
  outline: none;
  border-color: #007bff;
}

.form-input:disabled,
.form-input[readonly] {
  background: #f0f0f0;
  color: #666;
}

/* ── Buttons ── */

.btn {
  display: inline-block;
  padding: 0.55rem 1rem;
  border: none;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-primary {
  background: #007bff;
  color: #fff;
}

.btn-primary:hover {
  background: #0069d9;
}

.btn-danger {
  background: #dc3545;
  color: #fff;
}

.btn-danger:hover {
  background: #c82333;
}

.btn-success {
  background: #28a745;
  color: #fff;
}

.btn-success:hover {
  background: #218838;
}

.btn-secondary {
  background: #6c757d;
  color: #fff;
}

.btn-secondary:hover {
  background: #5a6268;
}

.btn + .btn {
  margin-left: 0.5rem;
}

/* ── JSON editor (CodeMirror container) ── */

.cm-json-editor {
  width: 100%;
  min-height: 240px;
}

.cm-json-editor .cm-editor {
  border: 2px solid #ddd;
  border-radius: 8px;
  background: #fff;
}

.cm-json-editor .cm-editor.cm-focused {
  outline: none;
  border-color: #007bff;
}

/* Hidden textarea fallback (replaced by CodeMirror) */
.json-editor {
  width: 100%;
  min-height: 240px;
  padding: 0.75rem;
  border: 2px solid #ddd;
  border-radius: 8px;
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  resize: vertical;
  background: #fff;
  tab-size: 2;
}

.json-editor:focus {
  outline: none;
  border-color: #007bff;
}

.json-editor[readonly] {
  background: #f8f9fa;
  color: #555;
}

/* ── JSON viewer (read-only published snapshots) ── */

.json-viewer {
  width: 100%;
  padding: 0.75rem;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  background: #f8f9fa;
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
  max-height: 400px;
  overflow-y: auto;
}

/* ── Error messages ── */

.error-message {
  color: #dc3545;
  font-size: 0.8rem;
  margin-top: 0.3rem;
}

/* ── Version history ── */

.version-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.version-list li {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid #eee;
  font-size: 0.9rem;
  cursor: pointer;
}

.version-list li:hover {
  background: #f8f9fa;
}

.version-list li:last-child {
  border-bottom: none;
}

/* ── API Key Bar ── */

.api-key-bar {
  background: #fff;
  border-bottom: 2px solid #eee;
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.api-key-bar.expanded {
  padding: 1.5rem;
}

.api-key-form {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  max-width: 600px;
}

.api-key-form label {
  font-weight: 600;
  font-size: 0.875rem;
  white-space: nowrap;
}

.api-key-form input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  border: 2px solid #ddd;
  border-radius: 8px;
  font-size: 0.95rem;
  background: #fff;
  font-family: system-ui, -apple-system, sans-serif;
}

.api-key-form input:focus {
  outline: none;
  border-color: #007bff;
}

.api-key-form button {
  padding: 0.6rem 1.25rem;
  border: none;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 600;
  background: #007bff;
  color: #fff;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

.api-key-form button:hover {
  background: #0069d9;
}

/* Logout button in nav bar */
.api-key-logout {
  margin-left: auto;
  padding: 0.4rem 0.9rem;
  border: none;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  background: #6c757d;
  color: #fff;
  cursor: pointer;
  transition: background 0.15s;
}

.api-key-logout:hover {
  background: #5a6268;
}


/* ── Config form + guidance layout ── */

.config-form-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  align-items: start;
}

@media (max-width: 1440px) {
  .config-form-layout {
    grid-template-columns: 1fr;
  }
}

.config-guidance {
  position: sticky;
  top: 1rem;
  background: #f8f9fb;
  border: 1px solid #e2e6ea;
  border-radius: 10px;
  padding: 1.25rem;
  font-size: 0.85rem;
  line-height: 1.5;
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
}

.config-guidance h3 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  color: #333;
}

.config-guidance h4 {
  margin: 1rem 0 0.4rem;
  font-size: 0.85rem;
  color: #555;
}

.config-guidance ul {
  margin: 0 0 0.5rem;
  padding-left: 1.25rem;
}

.config-guidance li {
  margin-bottom: 0.3rem;
}

.config-guidance code {
  background: #e9ecef;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-size: 0.8rem;
}

.config-guidance pre {
  background: #2d2d2d;
  color: #f8f8f2;
  padding: 0.75rem;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.78rem;
  line-height: 1.4;
  margin: 0.5rem 0 0;
}

.config-guidance pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

/* ── Group checkbox list ── */

.group-checkbox-list {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 0.5rem;
  max-height: 200px;
  overflow-y: auto;
  background: #fff;
}

.group-checkbox-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 400;
}

.group-checkbox-item:hover {
  background: #f0f4ff;
}

.group-checkbox-item input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.group-id-label {
  color: #888;
  font-size: 0.8rem;
}

/* ── Form header (back + save) ── */

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}


/* ── Expression Guide ── */

.expr-guide-btn {
  display: block;
  width: 100%;
  margin-top: 1rem;
  padding: 0.6rem 1rem;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.expr-guide-btn:hover {
  background: #0069d9;
}

.expr-guide-dialog {
  border: none;
  border-radius: 12px;
  padding: 2rem;
  max-width: 900px;
  width: 90vw;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
  font-size: 0.9rem;
  line-height: 1.6;
}

.expr-guide-dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

.expr-guide-dialog h2 {
  margin: 0 0 1rem;
  font-size: 1.3rem;
}

.expr-guide-dialog h3 {
  margin: 1.5rem 0 0.5rem;
  font-size: 1rem;
  color: #333;
}

.expr-guide-dialog p {
  margin: 0 0 0.75rem;
}

.expr-guide-dialog pre {
  background: #2d2d2d;
  color: #f8f8f2;
  padding: 0.75rem 1rem;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.82rem;
  line-height: 1.5;
  margin: 0.5rem 0 1rem;
}

.expr-guide-dialog code {
  background: #e9ecef;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-size: 0.82rem;
}

.expr-guide-dialog pre code {
  background: none;
  padding: 0;
  color: inherit;
}

.expr-guide-dialog table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.5rem 0 1rem;
  font-size: 0.85rem;
}

.expr-guide-dialog th,
.expr-guide-dialog td {
  text-align: left;
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid #e2e6ea;
}

.expr-guide-dialog th {
  background: #f8f9fb;
  font-weight: 600;
}

.expr-guide-dialog ul {
  margin: 0.5rem 0;
  padding-left: 1.25rem;
}

.expr-guide-dialog li {
  margin-bottom: 0.3rem;
}

.expr-guide-dialog form {
  text-align: right;
  margin-top: 1.5rem;
}

.expr-guide-dialog form button {
  padding: 0.5rem 1.5rem;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.expr-guide-dialog form button:hover {
  background: #0069d9;
}

/* ── Confirm Dialog ── */

.confirm-dialog {
  border: none;
  border-radius: 12px;
  padding: 1.5rem;
  max-width: 420px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
}

.confirm-dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

.confirm-dialog h3 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  color: #333;
}

.confirm-dialog p {
  margin: 0 0 1.25rem;
  color: #555;
  line-height: 1.5;
}

.confirm-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.confirm-dialog-cancel {
  padding: 0.5rem 1.25rem;
  background: #f0f0f0;
  color: #333;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
}

.confirm-dialog-cancel:hover {
  background: #e0e0e0;
}

.confirm-dialog-ok {
  padding: 0.5rem 1.25rem;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.confirm-dialog-ok:hover {
  background: #0069d9;
}

/* ── Auth Form ── */

.auth-form {
  background: #f8f9fa;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.auth-form__brand {
  text-align: center;
  margin: 0 0 0.25rem;
  font-size: 2rem;
}

.auth-form__help {
  color: #888;
  font-size: 0.85rem;
  text-align: center;
  margin: 0 0 1.5rem;
}

.auth-form__form {
  width: 100%;
  max-width: 360px;
  background: #fff;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.auth-form__form label {
  font-size: 0.8rem;
  font-weight: 600;
  color: #444;
}

.auth-form__input {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1.5px solid #ddd;
  border-radius: 8px;
  font-size: 0.9rem;
  background: #fafafa;
  transition: border-color 0.15s, background 0.15s;
}

.auth-form__input:focus {
  outline: none;
  border-color: #009688;
  background: #fff;
}

.auth-form__button {
  width: 100%;
  padding: 0.7rem 1.25rem;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  background: #009688;
  color: #fff;
  cursor: pointer;
  transition: background 0.15s;
  margin-top: 0.25rem;
}

.auth-form__button:hover {
  background: #00796b;
}

.auth-form__button--disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.auth-form__error {
  color: #dc3545;
  font-size: 0.8rem;
}

.auth-form__info {
  color: #666;
  font-size: 0.85rem;
  text-align: center;
}

.auth-form__link {
  display: block;
  text-align: center;
  color: #009688;
  text-decoration: none;
  cursor: pointer;
  font-size: 0.85rem;
}

.auth-form__link:hover {
  text-decoration: underline;
}

.auth-form__spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}

/* ── Page intro descriptions ── */

.page-intro {
  color: #666;
  font-size: 0.88rem;
  line-height: 1.5;
  margin-bottom: 1.25rem;
}

/* ── Welcome Banner ── */

.welcome-banner {
  background: linear-gradient(135deg, #f0faf9 0%, #fef8f0 100%);
  border: 1px solid #e0ece9;
  border-radius: 10px;
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.5rem;
}

.welcome-banner__title {
  margin: 0 0 0.35rem;
  font-size: 1.15rem;
  font-weight: 700;
  color: #333;
}

.welcome-banner__text {
  margin: 0;
  color: #555;
  font-size: 0.9rem;
  line-height: 1.5;
}

/* ── Calculator Panel ── */

.calculator-panel {
  position: fixed;
  top: 52px;
  right: 0;
  width: 420px;
  height: calc(100vh - 52px);
  background: #f5f5f5;
  border-left: 1px solid #ddd;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.08);
  z-index: 800;
  display: flex;
  flex-direction: column;
}

.calculator-panel-header {
  background: linear-gradient(135deg, #f0faf9 0%, #fef8f0 100%);
  border-bottom: 1px solid #e0ece9;
  border-radius: 10px;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 1.5rem 0;
  flex-shrink: 0;
}

.calculator-panel-title {
  margin: 0 0 0.35rem;
  font-size: 1.15rem;
  font-weight: 700;
  color: #333;
}

.calculator-panel-desc {
  margin: 0;
  color: #555;
  font-size: 0.9rem;
  line-height: 1.5;
}

.calculator-panel iframe {
  flex: 1;
  width: 100%;
  border: none;
}

body.calculator-panel-open #app {
  max-width: 1020px;
  margin-right: 420px;
}

/* Centre both the main content and calculator panel on wide screens */
@media (min-width: 1740px) {
  body.calculator-panel-open #app {
    max-width: 1280px;
    margin-left: auto;
    margin-right: calc((100vw - 1700px) / 2 + 420px);
  }

  .calculator-panel {
    right: calc((100vw - 1700px) / 2);
  }
}

/* ── Table action buttons: always stacked ── */

.config-actions {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  align-items: flex-start;
}

.config-actions .btn {
  padding: 0.35rem 0.6rem;
  font-size: 0.75rem;
  width: 100%;
  text-align: center;
}

.config-actions .btn + .btn {
  margin-left: 0;
}

/* ── Responsive: hide calculator panel when viewport too narrow for both ── */

@media (max-width: 1024px) {
  .calculator-panel {
    display: none !important;
  }

  body.calculator-panel-open #app {
    margin-right: 0;
  }
}

/* ── Responsive: small screen (mobile/tablet) layout ── */

@media (max-width: 768px) {
  /* Nav: allow wrapping and tighter spacing */
  .top-nav {
    flex-wrap: wrap;
    height: auto;
    padding: 0.5rem 1rem;
    gap: 0.5rem 1rem;
  }

  .nav-brand {
    margin-right: auto;
  }

  .nav-link {
    font-size: 0.8rem;
  }

  /* Main content: less padding */
  #app {
    padding: 0 0.75rem;
    margin: 1rem auto;
  }

  /* Tables: tighter cell padding */
  .config-table th,
  .config-table td,
  .template-table th,
  .template-table td {
    padding: 0.5rem 0.4rem;
    font-size: 0.8rem;
  }

  .config-table th,
  .template-table th {
    font-size: 0.7rem;
  }

  /* Asset action bar: wrap buttons */
  #app > div:first-child {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  #app > div:first-child .btn {
    margin-left: 0 !important;
  }
}
