/* frontend/assets/styles.css */
/* Global/base styles shared across views. View-specific rules live in styles/SIEView.css and styles/MarginView.css */

/* ---- Design tokens (fluid where useful) ---- */
:root {
  --bg: #faf8f3; /* Soft cream background */
  --card: #fefcf8; /* Warm cream card */
  --text: #0f172a;
  --muted: #64748b;
  --line: #e8e4dc; /* Warm cream border */
  --primary: #111827;
  --warn: #b45309;
  --ok: #0a7a3b;
  --err: #b00020;

  /* spacing & sizing */
  --radius-lg: 16px;
  --radius-md: 12px;
  --gap: 12px;
  --pad-card: clamp(12px, 2.5vw, 16px);

  /* fluid typography */
  --fz-base: clamp(14px, 1.3vw, 16px);
  --fz-small: clamp(12px, 1.1vw, 14px);
  --fz-h1: clamp(18px, 2.2vw, 22px);

  /* controls */
  --ctrl-height: clamp(40px, 5.2vw, 44px);

  /* container */
  --container-max: 1400px; /* Cap width for ultrawide, but wide enough for laptops */
}

[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body {
  margin:0; padding:0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial;
  font-size: var(--fz-base);
  color:var(--text); background:var(--bg);
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

/* Focus (keyboard) */
:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
  border-radius: 6px;
}

/* Topbar */
.topbar {
  position: sticky; top:0; z-index:10;
  display:grid; 
  grid-template-columns: 1fr auto 1fr;
  align-items:center;
  padding:10px 16px; background:var(--card); border-bottom:1px solid var(--line);
}
.topbar .brand {
  justify-self: start;
}
.brand { 
  font-weight:600; 
  font-size: clamp(14px, 1.6vw, 16px);
}
.brand-logo {
  justify-self: center;
  display: flex;
  align-items: center;
  height: 100%;
}
.brand-logo img { 
  height: calc(100% - 8px);
  max-height: 40px;
  width: auto; 
  vertical-align: middle;
}
.topbar .actions {
  justify-self: end;
}

/* Layout */
.wrap {
  width: 100%;
  max-width: var(--container-max);
  margin: clamp(12px, 3vw, 24px) auto;
  padding: 0 clamp(10px, 2.4vw, 24px);
}
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: var(--pad-card);
  width: 100%;
  max-width: 100%;
}
.card.inner { margin-top: 12px; }
.card.app {
  width: 100%;
  max-width: 100%;
  margin: 0;
}
.header h1 { margin: 0 0 4px; font-size: var(--fz-h1); }

/* Grid */
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}
@media (max-width: 900px) {
  .grid { grid-template-columns: 1fr; }
}

/* Fields */
.field { display:flex; flex-direction:column; gap:6px; font-size:1em; }
.field input[type="date"],
.field input[type="month"],
.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.field input[type="number"],
.field textarea,
.field select {
  border:1px solid var(--line);
  border-radius: var(--radius-md);
  padding:10px 12px;
  font-size:1em;
  min-height: var(--ctrl-height);
  width: 100%;
  background: var(--card);
}
.field select { -webkit-appearance:none; appearance:none; }
.field textarea {
  min-height: 100px;
  resize: vertical;
}

/* General input/textarea/select styles (for inputs not in .field containers) */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="search"],
textarea,
select {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  color: var(--text);
}

.checkbox { display:flex; align-items:center; gap:8px; font-size:1em; }

.row { display:flex; gap:8px; flex-wrap:wrap; }
.mt-10 { margin-top: 10px; }
.mt-16 { margin-top: 16px; }
.small { font-size: var(--fz-small); }

/* Pills (presets) */
.pill {
  border: 1px solid var(--line);
  background: #f9f7f4;
  padding: 8px 12px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.pill:hover { background: #f5f2ed; }

/* Actions */
.actions { display:flex; gap:8px; align-items:flex-end; flex-wrap:wrap; }
.actions.right { justify-content:flex-end; }

/* Buttons */
.btn {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  border:1px solid var(--primary);
  cursor:pointer;
  min-height: var(--ctrl-height);
  font-size: 1em;
}
.btn.primary { background: var(--primary); color:#fff; }
.btn.secondary { background: var(--card); color: var(--primary); border:1px solid var(--line); }
.btn.subtle { background:var(--card); border:1px solid var(--line); }
.icon-btn {
  background:var(--card); border:1px solid var(--line);
  border-radius:10px; padding:6px 10px; cursor:pointer;
  min-height: calc(var(--ctrl-height) - 8px);
}

@media (max-width: 640px) {
  /* make paired action buttons stack nicely on phones */
  .actions { flex-direction: column; }
  .actions > .btn, .actions > .icon-btn { width: 100%; }
}

/* Text helpers */
.muted { color: var(--muted); }
.msg { margin-top: 10px; font-size: 0.95em; }
.msg.ok { color: var(--ok); }
.msg.error { color: var(--err); }
.hint.warn {
  background: #fff7ed; color:#7c2d12; border:1px solid #fed7aa;
  padding:12px; border-radius:var(--radius-md); margin:12px 0;
}

/* Key-value layout (becomes stacked on small screens) */
.kv {
  display: grid; grid-template-columns: 160px 1fr; gap:8px; align-items:center;
}
@media (max-width: 640px) {
  .kv { grid-template-columns: 1fr; }
}

/* Code chips */
code { background:#f5f2ed; padding:2px 6px; border-radius:6px; }

/* Tabs (mode switch) */
.tabs {
  display:flex; gap:8px; padding:8px; margin-bottom:12px; flex-wrap: wrap;
}
.tab {
  border:1px solid var(--line); background:var(--card); border-radius:999px;
  padding:8px 12px; cursor:pointer; white-space: nowrap;
}
.tab.active { background:#0f172a; color:#fff; border-color:#0f172a; }
@media (max-width: 640px) {
  .tab { flex: 1 1 auto; text-align:center; }
}

/* Modal */
.modal {
  position: fixed; inset:0; background: rgba(0,0,0,0.3);
  display:flex; align-items:center; justify-content:center;
  padding:16px;
  z-index: 1000;
}
.modal__card {
  background:var(--card); border:1px solid var(--line);
  border-radius:var(--radius-lg);
  width: min(100%, clamp(340px, 92vw, 860px));
  padding: var(--pad-card);
}
.modal__header { display:flex; align-items:center; justify-content:space-between; margin-bottom:8px; }
.list { display:flex; flex-direction:column; gap:8px; margin-bottom:8px; }
.list .item {
  border:1px solid var(--line); border-radius:var(--radius-md);
  padding:10px 12px; display:flex; gap:10px; align-items:center; justify-content:space-between;
}
.item .meta { display:flex; gap:10px; flex-wrap:wrap; }
.item .meta .tag { background:#f5f2ed; border:1px solid var(--line); padding:2px 6px; border-radius:6px; font-size:var(--fz-small); }
.item .actions { display:flex; gap:6px; }
.modal[hidden] { display: none !important; }

/* Profiles modal: fill viewport height and scroll internally when content is long */
#profileModal .modal__card {
  height: auto;
  max-height: calc(100dvh - 96px);
  overflow-y: auto;
}

/* Busy / spinner */
.busy { display:flex; align-items:center; gap:8px; margin-right:auto; }
.spinner {
  width:16px; height:16px; border-radius:50%;
  border:2px solid #e5e7eb; border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
  display: inline-block;
  vertical-align: middle;
}

/* App-wide: show spinner inside the button that triggered an action */
button.is-busy {
  position: relative;
  padding-right: 34px;
}
button.is-busy::after {
  content: '';
  position: absolute;
  right: 10px;
  top: 50%;
  width: 14px;
  height: 14px;
  margin-top: -7px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: currentColor;
  animation: spin 0.8s linear infinite;
  pointer-events: none;
}
button.is-busy .spinner {
  display: none !important;
}
button.is-busy:not(.primary)::after {
  border-color: #d7dbe3;
  border-top-color: currentColor;
}
@keyframes spin { to { transform: rotate(360deg); } }
.busy__txt { font-size:var(--fz-small); color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; }
}

/* Custom select (profile) */
.selectwrap { position: relative; width: 100%; }
.selectwrap .selectlike {
  width: 100%;
  display: flex; align-items: center; justify-content: space-between;
  min-height: var(--ctrl-height); border-radius: var(--radius-md);
}
.selectwrap .chev { margin-left: 8px; opacity: .6; }

.selectwrap .menu {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-md);
  box-shadow: 0 8px 20px rgba(0,0,0,.08);
  max-height: min(50vh, 260px);
  overflow: auto; z-index: 20;
}
.selectwrap .menu__item {
  width: 100%; text-align: left;
  padding: 10px 12px; background: none; border: 0; cursor: pointer; font-size: 1em;
}
.selectwrap .menu__item:hover { background: #f9f7f4; }

/* Hide the native select but keep it accessible for value */
.selectwrap .native { position: absolute; inset: 0; opacity: 0; pointer-events: none; }

/* Tables - responsive and full width */
.table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  overflow: hidden;
  font-size: var(--fz-base);
}

.table thead th {
  text-align: left;
  font-weight: 600;
  font-size: var(--fz-small);
  color: var(--muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: #f9f7f4;
  white-space: nowrap;
}

/* Column resize handles for resizable tables */
.column-resize-handle {
  position: absolute;
  top: 0;
  right: 0;
  width: 5px;
  height: 100%;
  cursor: col-resize;
  background: transparent;
  z-index: 10;
  user-select: none;
  transition: background 0.2s ease;
}

.column-resize-handle:hover {
  background: var(--primary-color, #3b82f6);
  opacity: 0.5;
}

.credit-detail-table {
  table-layout: fixed;
}

.credit-detail-table thead th {
  position: relative;
  user-select: none;
}

.table tbody td {
  padding: 10px 12px;
  border-bottom: 1px solid #f1f3f7;
  vertical-align: middle;
}

.table tbody tr:last-child td {
  border-bottom: none;
}

.table tbody tr:hover {
  background: #faf8f3;
}

/* Responsive table - allow horizontal scroll on small screens */
@media (max-width: 768px) {
  .table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .table thead,
  .table tbody,
  .table tr,
  .table td,
  .table th {
    display: block;
  }
  
  .table thead {
    display: none; /* Hide headers on mobile, or use a different approach */
  }
  
  .table tr {
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    margin-bottom: 8px;
    padding: 8px;
  }
  
  .table td {
    border: none;
    padding: 4px 0;
    text-align: left;
  }
  
  .table td:before {
    content: attr(data-label) ": ";
    font-weight: 600;
    color: var(--muted);
    display: inline-block;
    min-width: 100px;
  }
}

/* Better approach: keep table structure but allow horizontal scroll */
@media (max-width: 768px) {
  .table {
    display: table;
    min-width: 600px; /* Ensure table doesn't get too cramped */
  }
  
  /* Wrap table in scrollable container */
  #projectsList {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}
