/* --- CSS Variables for Theming -------------------------------------------------------------------- */
:root {
  --bg-primary: #f0f2f5;
  --bg-secondary: #ffffff;
  --bg-sidebar: #2c3e50;
  --text-primary: #333333;
  --text-secondary: #666666;
  --text-muted: #999999;
  --border-color: #cccccc;
  --accent-blue: #3498db;
  --accent-blue-hover: #0056b3;
  --accent-red: #dc3545;
  --accent-red-hover: #a71d2a;
  --accent-green: #2ecc71;
  --table-stripe: #f9f9f9;
  --shadow: rgba(0, 0, 0, 0.1);
  --modal-bg: rgba(0, 0, 0, 0.5);
}

body.dark-mode {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-sidebar: #0f0f23;
  --text-primary: #e0e0e0;
  --text-secondary: #b0b0b0;
  --text-muted: #808080;
  --border-color: #444444;
  --accent-blue: #4a9eff;
  --accent-blue-hover: #2d7dd2;
  --table-stripe: #1e1e3f;
  --shadow: rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  margin: 0;
  background: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  min-height: 100vh;
  transition: background-color 0.3s ease, color 0.3s ease;
  -webkit-tap-highlight-color: transparent;
}

.main-content {
  flex: 1;
  padding: 20px;
  padding-bottom: 80px; /* room for mobile bottom nav */
  min-width: 0;
}

/* Utility */
.hidden { display: none !important; }
.w-100 { width: 100%; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.text-muted { color: var(--text-muted); font-size: 0.9em; }
.text-center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* --- Sidebar (desktop) --- */
.sidebar {
  position: sticky;
  top: 0;
  width: 220px;
  height: 100vh;
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  padding: 20px 10px;
  flex-shrink: 0;
  gap: 8px;
}

.sidebar a { text-decoration: none; display: block; }
.sidebar-divider {
  border: none;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
  margin: 8px 0;
}
.sidebar-logo-img {
  height: 28px;
  width: 28px;
  object-fit: contain;
  vertical-align: middle;
  margin-right: 6px;
}
.sidebar-title {
  color: white;
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  margin: 8px 0 16px;
}
.sidebar-spacer { flex: 1; }
.sidebar-title-link { text-decoration: none; }
.sidebar-project-name {
  color: #aaa;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  text-align: center;
  margin: 4px 0;
}

/* --- Forms --- */
label {
  color: var(--text-primary);
  margin-bottom: 4px;
  font-weight: 500;
}

input, select, textarea {
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 16px; /* prevents iOS zoom on focus */
  transition: border-color 0.2s ease;
  font-family: inherit;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent-blue);
}

textarea { resize: vertical; min-height: 60px; }

h1 { margin: 0 0 20px; }
h2 { margin: 0 0 15px; }

/* --- Buttons --- */
.btn {
  background-color: var(--accent-blue);
  color: white;
  border: none;
  padding: 12px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  text-decoration: none;
  display: inline-block;
  text-align: center;
  min-height: 44px;
  transition: background-color 0.2s ease, transform 0.1s ease;
  font-family: inherit;
}

.btn:hover { background-color: var(--accent-blue-hover); }
.btn:active { transform: scale(0.97); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 13px; min-height: 32px; }

.blue-btn { background-color: var(--accent-blue); color: white; }
.blue-btn:hover { background-color: var(--accent-blue-hover); }
.red-btn { background-color: var(--accent-red); color: white; }
.red-btn:hover { background-color: var(--accent-red-hover); }
.green-btn { background-color: var(--accent-green); color: white; }

.sidebar-btn {
  background-color: var(--accent-blue);
  color: white;
  border: none;
  padding: 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  width: 100%;
  display: block;
  white-space: nowrap;
  min-height: 44px;
  font-family: inherit;
}

/* --- Cards / containers --- */
.card {
  background: var(--bg-secondary);
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 0 10px var(--shadow);
  margin-bottom: 20px;
}

.container {
  max-width: 1000px;
  margin: 0 auto;
}

/* --- Action grid (the main feature) --- */
.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.action-tile {
  background: var(--bg-secondary);
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 18px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  min-height: 120px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-primary);
  box-shadow: 0 2px 8px var(--shadow);
  transition: transform 0.1s ease, box-shadow 0.2s ease, border-color 0.2s ease;
  text-align: center;
  word-break: break-word;
}

.action-tile:hover { box-shadow: 0 4px 16px var(--shadow); }
.action-tile:active { transform: scale(0.95); border-color: var(--accent-blue); }
.action-tile.flash { border-color: var(--accent-green); background: var(--accent-green); color: white; }

.action-icon {
  font-size: 2.8rem;
  line-height: 1;
}

.action-empty {
  grid-column: 1 / -1;
  text-align: center;
  padding: 40px 20px;
  background: var(--bg-secondary);
  border-radius: 10px;
  box-shadow: 0 0 10px var(--shadow);
}

/* --- Recent stamps list --- */
.recent-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.recent-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: 8px;
  box-shadow: 0 1px 4px var(--shadow);
}

.recent-icon { font-size: 1.6rem; }
.recent-meta { flex: 1; min-width: 0; }
.recent-label { font-weight: 600; }
.recent-time { font-size: 0.85em; color: var(--text-muted); }
.recent-comment { font-size: 0.85em; color: var(--text-secondary); }

/* --- Tables --- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td {
  border-bottom: 1px solid var(--border-color);
  padding: 10px 8px;
  text-align: left;
  vertical-align: middle;
}
th { background: var(--bg-primary); color: var(--text-primary); font-weight: 600; }
tr:nth-child(even) td { background: var(--table-stripe); }
th.sortable-col { cursor: pointer; user-select: none; }
th.sortable-col:hover { background: var(--table-stripe); }
th.sortable-col .sort-ind { display: inline-block; min-width: 1em; color: var(--text-muted, #888); }

/* --- Login --- */
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
}

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

.login-box {
  background: var(--bg-secondary);
  padding: 40px 30px;
  border-radius: 12px;
  box-shadow: 0 0 20px var(--shadow);
  text-align: center;
}

.login-logo { text-align: center; margin-bottom: 20px; }
.login-logo img { max-height: 120px; }
.login-box h1 { margin: 0 0 5px; font-size: 32px; }
.login-subtitle { color: var(--text-muted); margin: 0 0 25px; }

.login-error {
  background: #f8d7da; color: #721c24;
  padding: 10px; border-radius: 6px; margin-bottom: 20px;
}
body.dark-mode .login-error { background: #5a1a1a; color: #ff9999; }

.login-form { display: flex; flex-direction: column; gap: 15px; }
.login-field { display: flex; flex-direction: column; text-align: left; }
.login-field label { margin-bottom: 5px; }
.login-btn { padding: 14px; font-size: 16px; min-height: 48px; }

/* --- Toasts --- */
#toast-container {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
  width: max-content;
  max-width: 90vw;
}

.toast {
  background-color: var(--accent-green);
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px var(--shadow);
  animation: toastIn 0.2s ease, fadeOut 3s forwards;
  font-size: 14px;
  text-align: center;
}

.toast-success { background-color: var(--accent-green); }
.toast-error { background-color: var(--accent-red); }
.toast-warning { background-color: #f39c12; }
.toast-info { background-color: var(--accent-blue); }

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

@keyframes fadeOut {
  0%, 80% { opacity: 1; }
  100% { opacity: 0; }
}

/* --- Bottom nav (mobile only) --- */
.bottom-nav {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: var(--bg-sidebar);
  padding: 6px 4px;
  padding-bottom: calc(6px + env(safe-area-inset-bottom));
  z-index: 100;
  justify-content: space-around;
  box-shadow: 0 -2px 10px var(--shadow);
}

.bottom-nav a, .bottom-nav button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 8px 4px;
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.7);
  font-size: 11px;
  text-decoration: none;
  cursor: pointer;
  font-family: inherit;
  min-height: 50px;
}

.bottom-nav a.active, .bottom-nav a:hover { color: white; }
.bottom-nav-icon { font-size: 1.4rem; }

/* --- Error page --- */
.error-page {
  display: flex; justify-content: center; align-items: center;
  min-height: 100vh; padding: 20px;
}
.error-box {
  background: var(--bg-secondary);
  padding: 50px; border-radius: 10px; text-align: center;
  box-shadow: 0 0 20px var(--shadow);
}
.error-code { font-size: 100px; font-weight: bold; color: var(--accent-red); margin: 0; line-height: 1; }
.error-message { font-size: 22px; color: var(--text-secondary); margin: 20px 0 30px; }

/* --- Form layout --- */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; }
.form-group input, .form-group textarea, .form-group select { width: 100%; }
.form-actions { display: flex; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.form-actions .btn { flex: 1; min-width: 100px; }

/* --- Badges --- */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  background: var(--bg-primary);
  color: var(--text-secondary);
}
.badge-primary { background: var(--accent-blue); color: white; }
.badge-danger { background: var(--accent-red); color: white; }

.projects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.project-card {
  background: var(--bg-secondary);
  border-radius: 10px;
  padding: 20px;
  text-decoration: none;
  color: var(--text-primary);
  box-shadow: 0 2px 8px var(--shadow);
  transition: transform 0.15s ease, box-shadow 0.2s ease;
  display: block;
}

.project-card:hover { transform: translateY(-2px); box-shadow: 0 4px 16px var(--shadow); }
.project-card-active { border: 2px solid var(--accent-green, #27ae60); }
.project-card h3 { margin: 0 0 8px; color: var(--accent-blue); }
.project-card p { margin: 0 0 12px; color: var(--text-secondary); font-size: 0.9em; }

/* Drag-and-drop action list */
.drag-list { display: flex; flex-direction: column; gap: 4px; }
.drag-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--bg-primary);
  border-radius: 8px;
  border: 1px solid var(--border);
  user-select: none;
}
.drag-item.dragging { opacity: 0.3; }
.drag-placeholder {
  border: 2px dashed var(--accent-blue);
  border-radius: 8px;
  background: transparent;
}
.drag-handle {
  cursor: grab;
  font-size: 1.2rem;
  color: var(--text-secondary);
  padding: 4px;
  touch-action: none;
}
.drag-handle:active { cursor: grabbing; }
.drag-icon { font-size: 1.6rem; flex-shrink: 0; }
.drag-info { flex: 1; min-width: 0; }
.drag-label { font-weight: 600; }
.drag-comment { font-size: 0.85em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.drag-actions { display: flex; gap: 6px; flex-shrink: 0; }
@media (max-width: 600px) {
  .drag-item { gap: 8px; padding: 10px 8px; }
  .drag-actions { flex-direction: column; gap: 4px; }
}

.queue-badge {
  display: inline-block;
  background: var(--accent-red);
  color: white;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 10px;
  margin-left: 2px;
  vertical-align: middle;
}

/* --- Responsive: mobile --- */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .bottom-nav { display: flex; }
  .main-content { padding: 12px; padding-bottom: 80px; }
  .action-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .action-tile { min-height: 110px; padding: 14px 8px; }
  .action-icon { font-size: 2.4rem; }
  h1 { font-size: 24px; }
  .card { padding: 14px; }
}

@media (min-width: 769px) {
  .bottom-nav { display: none; }
}

/* iOS dark-mode form bg fix */
@supports (-webkit-touch-callout: none) {
  input, select, textarea { -webkit-appearance: none; }
}

/* Offline state */
.offline-banner {
  background: #e74c3c;
  color: #fff;
  text-align: center;
  padding: 6px 12px;
  font-size: 0.85em;
  font-weight: 600;
}
.is-offline .sidebar a,
.is-offline .sidebar button { opacity: 0.35; pointer-events: none; }
.is-offline .sidebar [data-offline-ok] { opacity: 1; pointer-events: auto; }
.is-offline .bottom-nav a { opacity: 0.35; pointer-events: none; }
.is-offline .bottom-nav [data-offline-ok] { opacity: 1; pointer-events: auto; }
