:root {
  --bg: #e8efe6;
  --bg-deep: #d4e0d1;
  --paper: #f7faf6;
  --ink: #1a2a22;
  --muted: #5a6e63;
  --line: #b8c9bc;
  --accent: #1f6b4a;
  --accent-soft: #d5ebe0;
  --danger: #9b2c2c;
  --header: #2a3d34;
  --shadow: 0 12px 40px rgba(26, 42, 34, 0.12);
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --sans: "Manrope", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--sans);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, #cfe3d6 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #f0e6d4 0%, transparent 50%),
    var(--bg);
}

.hidden { display: none !important; }

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-panel {
  width: min(380px, 100%);
  background: var(--paper);
  padding: 28px;
  border-radius: 18px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
}
.login-brand {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--accent);
}
.field { display: grid; gap: 6px; font-size: 0.9rem; color: var(--muted); }
.field input, .sheet-title, #cell-editor-text {
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
  color: var(--ink);
}
.form-error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 0.9rem; }

.btn {
  font: inherit;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 10px;
  padding: 8px 12px;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-ghost { background: transparent; }
.btn-sm { padding: 6px 10px; font-size: 0.85rem; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  background: rgba(247, 250, 246, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand {
  font-weight: 700;
  font-size: 1.35rem;
  letter-spacing: -0.03em;
  color: var(--accent);
}
.sheet-title {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  font-size: 1.05rem;
  border: none;
  background: transparent;
  padding: 6px 8px;
}
.sheet-title:focus {
  outline: 2px solid var(--accent-soft);
  background: #fff;
  border-radius: 8px;
}
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.grid-wrap {
  overflow: auto;
  max-height: calc(100vh - 64px);
  padding: 12px;
}

.sheet {
  border-collapse: separate;
  border-spacing: 0;
  background: #fff;
  box-shadow: var(--shadow);
  border-radius: 12px;
  overflow: hidden;
  min-width: 100%;
}
.sheet th, .sheet td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  padding: 0;
}
.sheet th:last-child, .sheet td:last-child { border-right: none; }
.sheet td { height: 1px; }

.corner-cell, .row-head {
  width: 44px;
  min-width: 44px;
  background: var(--bg-deep);
  color: var(--muted);
  text-align: center;
  font-family: var(--mono);
  font-size: 0.75rem;
  user-select: none;
}
.col-head {
  position: relative;
  background: var(--header);
  color: #eef6f1;
  font-weight: 600;
  height: 36px;
  cursor: grab;
}
.col-head.dragging { opacity: 0.55; }
.col-head-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 10px;
  min-height: 36px;
}
.col-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: text;
}
.col-menu-btn {
  border: none;
  background: transparent;
  color: inherit;
  cursor: pointer;
  opacity: 0.75;
  font-size: 1rem;
  line-height: 1;
  padding: 0 2px;
}
.col-menu-btn:hover { opacity: 1; }
.col-resizer {
  position: absolute;
  top: 0;
  right: 0;
  width: 6px;
  height: 100%;
  cursor: col-resize;
}

.cell {
  min-height: 72px;
  height: 100%;
  box-sizing: border-box;
  padding: 8px 10px;
  cursor: cell;
  background: #fff;
  overflow: visible;
  user-select: none;
}
.cell:hover,
.cell.in-range,
.cell.active,
.cell.selected {
  background: #fff;
}
.cell.in-range {
  box-shadow: inset 0 0 0 9999px rgba(0, 0, 0, 0.04);
}
.cell.active,
.cell.selected {
  outline: 2px solid #333;
  outline-offset: -2px;
  position: relative;
  z-index: 1;
  box-shadow: none;
}
.cell-text {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.92rem;
  line-height: 1.35;
  margin-bottom: 6px;
}
.cell-links, .cell-media {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.media-chip img {
  max-width: 100%;
  max-height: 220px;
  border-radius: 8px;
  cursor: zoom-in;
  object-fit: contain;
  background: var(--bg-deep);
  display: block;
}
.media-chip audio, .media-chip video {
  width: 100%;
  max-width: 320px;
  max-height: 240px;
  display: block;
}
.edit-overlay {
  position: fixed;
  z-index: 80;
  margin: 0;
  padding: 6px 8px;
  border: 2px solid var(--accent);
  border-radius: 0;
  background: #fff;
  box-shadow: none;
  font: inherit;
  color: var(--ink);
  resize: none;
  outline: none;
  white-space: pre-wrap;
  word-break: break-word;
  overflow: auto;
}
.cell-link {
  color: var(--accent);
  font-size: 0.85rem;
  word-break: break-all;
}
.media-chip {
  display: grid;
  gap: 6px;
}
.media-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0;
  border-radius: 0;
  background: transparent;
  color: #1a2a22;
  text-decoration: underline;
  font-size: 0.85rem;
  word-break: break-all;
}

.ctx-menu {
  position: fixed;
  z-index: 50;
  min-width: 180px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
}
.ctx-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
}
.ctx-item:hover { background: var(--accent-soft); }
.ctx-item.danger { color: var(--danger); }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(10, 18, 14, 0.88);
  display: grid;
  place-items: center;
  padding: 24px;
}
.lightbox img {
  max-width: min(96vw, 1200px);
  max-height: 90vh;
  border-radius: 8px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
  animation: zoom-in 0.18s ease;
}
.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  border: none;
  background: transparent;
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
}
@keyframes zoom-in {
  from { transform: scale(0.94); opacity: 0.4; }
  to { transform: scale(1); opacity: 1; }
}

.cell-editor {
  position: fixed;
  z-index: 60;
  width: min(420px, calc(100vw - 24px));
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 12px;
  display: grid;
  gap: 10px;
}
.cell-editor-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; }
  .brand { font-size: 1.15rem; }
  .cell { min-height: 64px; }
}
