:root {
  --khmer-font: 'Khmer OS Battambang', 'Battambang', sans-serif;
  --paper-bg: #fcf6dc;
  --paper-line: #d9cfa8;
  --paper-margin: #e09f9f;
  --desk-bg: #e7e2d5;
  --sidebar-bg: #2b2620;
  --line-height: 38px;
}

* { box-sizing: border-box; font-family: var(--khmer-font); }
body { margin: 0; background: var(--desk-bg); color: #2b2620; height: 100vh; overflow: hidden; }
.hidden { display: none !important; }

.auth-overlay {
  position: fixed; inset: 0; background: rgba(43, 38, 32, 0.85);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.auth-card {
  background: var(--paper-bg); padding: 28px 32px; border-radius: 12px; width: 100%; max-width: 420px;
  box-shadow: 0 15px 40px rgba(0,0,0,0.35); border: 1px solid var(--paper-line);
}
.auth-card h1 { margin: 0 0 6px; font-size: 24px; text-align: center; }
.subtitle { font-size: 14px; color: #5c5346; text-align: center; margin-bottom: 18px; }
.auth-tabs { display: flex; gap: 8px; margin-bottom: 18px; }
.auth-tabs button { flex: 1; padding: 8px; border: 1px solid #cbbd8d; background: #f3ead0; cursor: pointer; border-radius: 6px; }
.auth-tabs button.active { background: var(--sidebar-bg); color: #fff; border-color: var(--sidebar-bg); }
.auth-card label { display: block; font-size: 14px; margin: 10px 0 4px; font-weight: bold; }
.auth-card input { width: 100%; padding: 10px; border: 1px solid #c5b787; border-radius: 6px; background: #fffdf5; font-size: 16px; }

.btn-primary {
  width: 100%; margin-top: 16px; padding: 10px; background: #8b6508; color: #fff; border: none; border-radius: 6px; font-size: 16px; cursor: pointer;
}
.btn-primary:hover { background: #705106; }
#qr-image { display: block; margin: 12px auto; width: 180px; height: 180px; border: 4px solid #fff; border-radius: 8px; }
.secret-text { text-align: center; font-size: 13px; background: #f2e8c9; padding: 6px; border-radius: 4px; }
.error-msg { color: #b91c1c; font-size: 14px; text-align: center; margin-top: 10px; }

.app-layout { display: flex; height: 100vh; }

/* Sidebar with transition support for collapsing */
.sidebar {
  width: 270px;
  min-width: 270px;
  background: var(--sidebar-bg);
  color: #f5efe0;
  display: flex;
  flex-direction: column;
  padding: 16px;
  transition: width 0.25s ease, padding 0.25s ease, opacity 0.2s ease;
  overflow: hidden;
}
.sidebar.collapsed {
  width: 0;
  min-width: 0;
  padding-left: 0;
  padding-right: 0;
  opacity: 0;
  pointer-events: none;
}

.sidebar-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; white-space: nowrap; }
.sidebar-header h2 { margin: 0; font-size: 20px; }
.btn-new { background: #d4a017; color: #1f1b16; border: none; padding: 6px 12px; border-radius: 6px; font-weight: bold; cursor: pointer; white-space: nowrap; }

.user-badge {
  display: flex; justify-content: space-between; align-items: center; background: rgba(255,255,255,0.08); padding: 8px 10px; border-radius: 6px; font-size: 14px; margin-bottom: 16px; white-space: nowrap;
}
.user-badge button { background: transparent; border: 1px solid #a89f91; color: #e5dec9; border-radius: 4px; padding: 2px 8px; cursor: pointer; font-size: 12px; }

.section-label { font-size: 12px; color: #b8ae9c; margin-bottom: 8px; white-space: nowrap; }
.notes-list { list-style: none; padding: 0; margin: 0; overflow-y: auto; flex: 1; white-space: nowrap; }
.notes-list li { padding: 10px 12px; border-radius: 6px; margin-bottom: 6px; cursor: pointer; background: rgba(255,255,255,0.04); }
.notes-list li:hover, .notes-list li.active { background: rgba(212, 160, 23, 0.25); border-left: 3px solid #d4a017; }
.note-item-title { font-weight: bold; font-size: 15px; display: block; overflow: hidden; text-overflow: ellipsis; }
.note-item-meta { font-size: 12px; color: #b8ae9c; }

.workspace { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.toolbar {
  background: #f7f2e4; border-bottom: 1px solid #d5ccb4; padding: 8px 16px; display: flex; flex-wrap: wrap; gap: 12px; align-items: center;
}
.tool-group { display: flex; align-items: center; gap: 6px; padding-right: 12px; border-right: 1px solid #d5ccb4; }
.tool-group:last-child { border-right: none; }
.right-tools { margin-left: auto; }
.toolbar button, .toolbar select {
  background: #fffdf7; border: 1px solid #c9be9f; border-radius: 5px; padding: 5px 10px; cursor: pointer; font-size: 14px;
}
.toolbar button:hover { background: #efe6cc; }
.toolbar input[type="color"] { width: 32px; height: 30px; border: 1px solid #c9be9f; padding: 1px; cursor: pointer; background: #fff; }
.btn-save { background: #2e7d32 !important; color: #fff; border-color: #2e7d32 !important; }
.btn-danger { color: #b91c1c; }

.desk-surface { flex: 1; overflow-y: auto; padding: 24px; display: flex; justify-content: center; }
.notebook-sheet {
  position: relative; width: 100%; max-width: 820px; min-height: 980px; background-color: var(--paper-bg);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.16); border-radius: 4px 12px 12px 4px; display: flex; flex-direction: column; padding-left: 58px;
}
.notebook-sheet::before { content: ''; position: absolute; top: 0; bottom: 0; left: 52px; width: 1px; background: var(--paper-margin); }

.binder-holes { position: absolute; left: 14px; top: 60px; bottom: 60px; display: flex; flex-direction: column; justify-content: space-between; pointer-events: none; }
.binder-holes span { width: 20px; height: 20px; border-radius: 50%; background: var(--desk-bg); box-shadow: inset 1px 2px 4px rgba(0,0,0,0.28); }

.paper-inner { flex: 1; padding: 24px 32px 24px 18px; display: flex; flex-direction: column; }
.paper-header { display: flex; align-items: center; justify-content: space-between; border-bottom: 2px solid #8c7b53; padding-bottom: 8px; margin-bottom: 6px; }
#note-title { flex: 1; border: none; background: transparent; font-size: 22px; font-weight: bold; color: var(--sidebar-bg); outline: none; }
.save-status { font-size: 12px; color: #6d624c; background: rgba(0,0,0,0.05); padding: 3px 8px; border-radius: 4px; }

.paper-editor {
  flex: 1; outline: none; font-size: 18px; line-height: var(--line-height);
  background-image: repeating-linear-gradient(to bottom, transparent 0px, transparent calc(var(--line-height) - 1px), var(--paper-line) calc(var(--line-height) - 1px), var(--paper-line) var(--line-height));
  background-attachment: local; padding-top: 2px; position: relative;
}
.paper-editor p, .paper-editor div { margin: 0; min-height: var(--line-height); }

/* Transformable Media & Embeds */
.media-wrapper {
  display: inline-block; position: relative; border: 2px dashed transparent; padding: 4px; margin: 6px 0;
  resize: both; overflow: auto; max-width: 100%; min-width: 160px; min-height: 48px; background: rgba(255, 253, 240, 0.85); border-radius: 6px; vertical-align: middle;
}
.media-wrapper:hover, .media-wrapper.selected { border-color: #b8860b; box-shadow: 0 4px 12px rgba(0,0,0,0.12); }
.media-wrapper img, .media-wrapper iframe { width: 100%; height: calc(100% - 8px); object-fit: contain; display: block; border: none; }
.media-wrapper audio { width: 100%; min-width: 240px; }

.file-attachment-card { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: #f3e8c6; border: 1px solid #cbb982; border-radius: 6px; text-decoration: none; color: #2b2620; font-size: 15px; }

.media-controls-bar { position: absolute; top: 4px; right: 4px; display: none; gap: 4px; background: rgba(43, 38, 32, 0.85); padding: 2px 6px; border-radius: 4px; z-index: 10; }
.media-wrapper:hover .media-controls-bar { display: flex; }
.media-controls-bar button { background: transparent; border: none; color: #fff; cursor: pointer; font-size: 12px; padding: 2px 4px; }

.page-controls { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 12px 24px; border-top: 1px dashed var(--paper-line); background: rgba(245, 236, 204, 0.6); }
.page-controls button { padding: 5px 12px; border: 1px solid #c5b688; background: #fffdf5; border-radius: 5px; cursor: pointer; font-size: 13px; }
.btn-add-page { background: #e8f5e9 !important; border-color: #81c784 !important; }
.btn-del-page { background: #ffebee !important; border-color: #e57373 !important; }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 900; }
.modal-box { background: var(--paper-bg); padding: 22px; border-radius: 10px; width: 90%; max-width: 460px; border: 1px solid var(--paper-line); }
.alert-box { text-align: center; }
.admin-contact-card { background: #fffdf5; border: 1px solid #d5ccb4; padding: 12px; border-radius: 6px; margin: 14px 0; font-size: 16px; }
.admin-contact-card a { color: #8b6508; font-weight: bold; text-decoration: none; }

.share-form { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.share-form input[type="text"] { padding: 8px; border: 1px solid #c5b688; border-radius: 5px; background: #fffdf5; }
.modal-list { list-style: none; padding: 0; max-height: 220px; overflow-y: auto; }
.modal-list li { display: flex; justify-content: space-between; align-items: center; padding: 8px; border-bottom: 1px solid var(--paper-line); font-size: 14px; }
.btn-close-modal { margin-top: 14px; width: 100%; padding: 8px; background: #4a4238; color: #fff; border: none; border-radius: 5px; cursor: pointer; }