:root {
  --bg: #f4f7fa;
  --surface: #ffffff;
  --text: #1a2b3c;
  --muted: #5c6b7a;
  --primary: #0f3d5e;
  --primary-hover: #14527d;
  --accent: #e87722;
  --border: #d8e0e8;
  --success: #1a7f4b;
  --error: #b42318;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(15, 61, 94, 0.08);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: 'DM Sans', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
}

.header {
  background: linear-gradient(135deg, var(--primary), #1a5a85);
  color: #fff;
  padding: 1.25rem 1rem 1.5rem;
}

.header-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
}

.eyebrow {
  margin: 0 0 0.25rem;
  font-size: 0.75rem;
  opacity: 0.85;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
}

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 1rem 1rem calc(5rem + var(--safe-bottom));
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}

.form-card {
  display: grid;
  gap: 1rem;
}

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

.optional {
  font-weight: 400;
  color: var(--muted);
}

select,
textarea {
  width: 100%;
  font: inherit;
  padding: 0.85rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%235c6b7a'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2rem;
}

/* Adaptive dropdown — wraps long text (stage, element, etc.) */
.adaptive-select {
  position: relative;
  width: 100%;
}

.adaptive-select-native {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
  overflow: hidden;
}

.adaptive-select-trigger {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  line-height: 1.45;
  padding: 0.85rem 2.25rem 0.85rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%235c6b7a'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E") no-repeat right 0.75rem top 1rem;
  color: var(--text);
  cursor: pointer;
  white-space: normal;
  word-break: break-word;
  min-height: 3rem;
}

.adaptive-select-trigger.placeholder {
  color: var(--muted);
}

.adaptive-select-trigger:focus {
  outline: 2px solid rgba(15, 61, 94, 0.25);
  border-color: var(--primary);
}

.adaptive-select-panel {
  position: absolute;
  z-index: 30;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  max-height: min(60vh, 320px);
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.adaptive-select-option {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  line-height: 1.45;
  padding: 0.75rem 0.9rem;
  border: none;
  border-bottom: 1px solid #eef3f7;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  white-space: normal;
  word-break: break-word;
}

.adaptive-select-option:last-child {
  border-bottom: none;
}

.adaptive-select-option:hover,
.adaptive-select-option[aria-selected='true'] {
  background: #eef6fc;
}

.adaptive-select-option.placeholder-option {
  color: var(--muted);
  font-style: italic;
}

.adaptive-select-measurer {
  position: absolute;
  visibility: hidden;
  pointer-events: none;
  width: 100%;
  font: inherit;
  line-height: 1.45;
  padding: 0.85rem 2.25rem 0.85rem 0.9rem;
  white-space: normal;
  word-break: break-word;
}

.adaptive-select.open .adaptive-select-trigger {
  border-color: var(--primary);
}

textarea {
  resize: vertical;
  min-height: 4rem;
}

select:focus,
textarea:focus,
button:focus {
  outline: 2px solid rgba(15, 61, 94, 0.25);
  border-color: var(--primary);
}

.preview {
  background: #f8fafc;
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 1rem;
}

.preview h2 {
  margin: 0 0 0.5rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.preview-text {
  margin: 0;
  line-height: 1.5;
  font-size: 0.95rem;
  white-space: normal;
  word-break: break-word;
}

.preview-text.empty {
  color: var(--muted);
  font-style: italic;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 0.95rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
}

.btn:active {
  transform: scale(0.98);
}

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

.btn.primary:hover {
  filter: brightness(1.05);
}

.btn.secondary {
  background: #eef3f7;
  color: var(--primary);
}

.badge {
  display: inline-block;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
}

.badge.storage {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  white-space: nowrap;
}

.badge.user {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.35rem;
}

.btn-text {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.9);
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
}

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-container {
  width: 100%;
  max-width: 420px;
}

.login-card h1 {
  margin: 0 0 0.35rem;
  color: var(--text);
}

.login-brand {
  color: var(--primary);
  opacity: 1;
}

.login-sub {
  margin: 0 0 1.25rem;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.login-form {
  display: grid;
  gap: 1rem;
}

.login-btn {
  width: 100%;
}

.login-note {
  margin: 1rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
}

.login-error {
  background: #fef3f2;
  color: var(--error);
  border: 1px solid #fecdca;
  border-radius: 8px;
  padding: 0.75rem;
  margin-bottom: 1rem;
  font-size: 0.875rem;
}

input[type='email'],
input[type='password'] {
  width: 100%;
  font: inherit;
  padding: 0.85rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
}

input:focus {
  outline: 2px solid rgba(15, 61, 94, 0.25);
  border-color: var(--primary);
}

.toolbar-hint {
  margin: 0.25rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  max-width: 280px;
  line-height: 1.4;
}

.status-select {
  font: inherit;
  font-size: 0.78rem;
  padding: 0.35rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  max-width: 100%;
}

.serial-input {
  font: inherit;
  font-size: 0.78rem;
  padding: 0.35rem 0.45rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 6.5rem;
  min-width: 5rem;
}

.serial-input:focus {
  outline: 2px solid rgba(15, 61, 94, 0.25);
  border-color: var(--primary);
}

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: #fff;
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: 10;
}

.nav-item {
  flex: 1;
  text-align: center;
  padding: 0.9rem;
  text-decoration: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.9rem;
}

.nav-item.active {
  color: var(--primary);
  box-shadow: inset 0 -3px 0 var(--accent);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(4.5rem + var(--safe-bottom));
  transform: translateX(-50%) translateY(120%);
  background: var(--text);
  color: #fff;
  padding: 0.85rem 1.1rem;
  border-radius: 10px;
  opacity: 0;
  transition: transform 0.25s, opacity 0.25s;
  z-index: 20;
  max-width: calc(100vw - 2rem);
  text-align: center;
}

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

.toast.error {
  background: var(--error);
}

.toast.success {
  background: var(--success);
}

.admin-container .toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.toolbar-actions {
  display: flex;
  gap: 0.5rem;
}

.table-wrap {
  overflow-x: auto;
  padding: 0;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

th,
td {
  padding: 0.75rem 0.65rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

th {
  background: #f8fafc;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.desc-cell {
  max-width: 220px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 2rem !important;
}

.empty.error {
  color: var(--error);
}

@media (min-width: 640px) {
  .container {
    padding-top: 1.5rem;
  }

  h1 {
    font-size: 1.75rem;
  }
}
