* { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  margin: 0;
  background: #f4f1ec;
  color: #2c2519;
}

header {
  background: #3c2e1e;
  color: #f4f1ec;
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

header h1 { margin: 0; font-size: 1.4rem; }

.api-key-bar {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  font-size: 0.9rem;
}

.api-key-bar input {
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  border: 1px solid #555;
  background: #fff;
  width: 18rem;
}

#key-status { font-size: 0.85rem; }
#key-status.ok { color: #9bd99b; }
#key-status.err { color: #f4a8a8; }

main {
  max-width: 1100px;
  margin: 2rem auto;
  padding: 0 1.5rem;
}

.toolbar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.toolbar input[type="search"] {
  flex: 1;
  min-width: 12rem;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  border: 1px solid #c0b8a8;
  border-radius: 4px;
}

button {
  background: #6b5536;
  color: #fff;
  border: none;
  padding: 0.55rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.95rem;
}

button:hover { background: #543f24; }
button.primary { background: #2e5a2e; }
button.primary:hover { background: #1e3d1e; }
button.danger { background: #8a2b2b; }
button.danger:hover { background: #631f1f; }

#documents-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}

.doc-card {
  background: #fff;
  border: 1px solid #d6cdb9;
  border-radius: 6px;
  padding: 1rem;
  cursor: pointer;
  transition: box-shadow 0.15s, transform 0.15s;
  position: relative;
}

.akte-header {
  display: flex;
  gap: 1rem;
  align-items: baseline;
  margin-bottom: 1rem;
}
.akte-header h2 {
  margin: 0;
  font-size: 1.2rem;
  color: #3c2e1e;
}

.akte-card {
  background: #fff;
  border: 1px solid #d6cdb9;
  border-radius: 6px;
  padding: 1.25rem 1.25rem;
  cursor: pointer;
  transition: box-shadow 0.15s, transform 0.15s;
  border-left: 6px solid #6b5536;
}
.akte-card:hover {
  box-shadow: 0 2px 8px rgba(60, 46, 30, 0.15);
  transform: translateY(-1px);
}
.akte-card h3 {
  margin: 0 0 0.4rem 0;
  font-size: 1.15rem;
  color: #3c2e1e;
}
.akte-card .akte-meta {
  font-size: 0.9rem;
  color: #6b5536;
}
.akte-card.empty-sig {
  border-left-color: #b0a890;
  background: #f7f3eb;
  font-style: italic;
}

.doc-card.selected {
  outline: 3px solid #2e5a2e;
  outline-offset: -3px;
}

.doc-card .select-checkbox {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  width: 22px;
  height: 22px;
  cursor: pointer;
  accent-color: #2e5a2e;
}

.toolbar-link {
  display: inline-flex;
  align-items: center;
  padding: 0.55rem 1rem;
  background: #fff;
  color: #6b5536;
  border: 1px solid #c0b8a8;
  border-radius: 4px;
  text-decoration: none;
  font-size: 0.95rem;
}
.toolbar-link:hover { background: #f4f1ec; }

.merge-bar {
  background: #eee2cc;
  border: 1px solid #c0b8a8;
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
  margin-bottom: 1rem;
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}
.merge-bar #merge-count {
  font-weight: 600;
  color: #543f24;
  margin-right: 0.5rem;
}

.doc-card:hover {
  box-shadow: 0 4px 14px rgba(0,0,0,0.08);
  transform: translateY(-2px);
}

.doc-card h3 {
  margin: 0 0 0.4rem 0;
  font-size: 1rem;
  color: #3c2e1e;
}

.doc-card .sig { font-family: monospace; font-size: 0.85rem; color: #6b5536; }
.doc-card .summary { font-size: 0.9rem; margin-top: 0.5rem; color: #555; }
.doc-card .tags { margin-top: 0.5rem; font-size: 0.8rem; color: #2e5a2e; }
.doc-card .badge {
  display: inline-block;
  background: #eee2cc;
  color: #6b5536;
  padding: 0.1rem 0.5rem;
  border-radius: 10px;
  font-size: 0.75rem;
  margin-right: 0.3rem;
}

#detail-section {
  background: #fff;
  border: 1px solid #d6cdb9;
  border-radius: 6px;
  padding: 1.5rem;
}

.detail-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

#back-btn { background: transparent; color: #3c2e1e; padding-left: 0; }
#back-btn:hover { background: transparent; text-decoration: underline; }

#doc-internal-id {
  font-family: monospace;
  font-size: 0.95rem;
  color: #6b5536;
  background: #eee2cc;
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
  margin-right: auto;
  margin-left: 0.5rem;
}
#doc-internal-id:empty { display: none; }

.doc-pager {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.doc-pager button {
  background: #6b5536;
  color: #fff;
  padding: 0.4rem 0.8rem;
  font-size: 1rem;
  line-height: 1;
  min-width: 2.5rem;
}

.doc-pager button:hover:not(:disabled) { background: #543f24; }

.doc-pager button:disabled {
  background: #c0b8a8;
  cursor: not-allowed;
}

#doc-position {
  font-size: 0.9rem;
  color: #6b5536;
  font-variant-numeric: tabular-nums;
  min-width: 4rem;
  text-align: center;
}

#doc-form label {
  display: block;
  margin-bottom: 0.9rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: #3c2e1e;
}

#doc-form input, #doc-form textarea {
  width: 100%;
  padding: 0.5rem 0.6rem;
  margin-top: 0.25rem;
  border: 1px solid #c0b8a8;
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: normal;
}

#doc-form textarea { resize: vertical; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.5rem 1rem;
}

.form-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

.meta {
  margin-top: 1rem;
  font-size: 0.8rem;
  color: #888;
}

.hidden { display: none !important; }

#images-section {
  margin-top: 2rem;
  border-top: 1px solid #d6cdb9;
  padding-top: 1.5rem;
}

.upload-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  align-items: center;
  flex-wrap: wrap;
}

#image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem;
}

.image-tile {
  position: relative;
  border: 1px solid #d6cdb9;
  border-radius: 4px;
  overflow: hidden;
  background: #f8f5ef;
}

.image-tile img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  display: block;
  cursor: pointer;
}

.image-tile .img-delete {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(138, 43, 43, 0.85);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  line-height: 1;
  cursor: pointer;
  font-size: 0.9rem;
}

#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #3c2e1e;
  color: #fff;
  padding: 0.7rem 1.2rem;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  z-index: 1000;
}

#toast.error { background: #8a2b2b; }

.empty {
  text-align: center;
  color: #888;
  padding: 3rem 1rem;
  font-style: italic;
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  cursor: zoom-out;
}

.lightbox img {
  max-width: 95vw;
  max-height: 95vh;
}

.inbox-picker-panel {
  margin-top: 1rem;
  padding: 1rem;
  background: #f8f5ef;
  border: 1px solid #d6cdb9;
  border-radius: 6px;
}
.inbox-picker-header {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}
.inbox-picker-header strong { color: #3c2e1e; }
.inbox-picker-header #inbox-picker-count {
  font-size: 0.85rem;
  color: #6b5536;
  margin-right: auto;
}
#inbox-picker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.5rem;
}
.inbox-tile {
  position: relative;
  border: 3px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  background: white;
  transition: transform 0.1s;
}
.inbox-tile:hover { transform: scale(1.02); }
.inbox-tile.selected { border-color: #2e5a2e; }
.inbox-tile img {
  width: 100%;
  height: 140px;
  object-fit: cover;
  display: block;
}
.inbox-tile .label {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  background: rgba(0,0,0,0.6);
  color: white;
  padding: 0.15rem 0.4rem;
  font-size: 0.75rem;
}

body:not(.writable) .api-key-bar,
body:not(.writable) #new-doc-btn,
body:not(.writable) .toolbar-link,
body:not(.writable) .form-actions,
body:not(.writable) #images-section .upload-row,
body:not(.writable) #inbox-picker-panel,
body:not(.writable) .doc-card .select-checkbox,
body:not(.writable) .merge-bar {
  display: none !important;
}
body:not(.writable) #doc-form input,
body:not(.writable) #doc-form textarea {
  background: #faf8f3;
  cursor: text;
}
